shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.walker.security.admin;
 
import java.io.File;
import java.util.Map;
 
public abstract class AbstractLisenceGenerator implements LisenceGenerator {
 
//    private static final String RECORD_FILE_PREFIX = "d:/logs/walker_sn_";
//    private static final String RECORD_FILE_SUFFIX = ".txt";
    
    private Map<String, Object> keyMap = null;
    
    private String secretKey = null;
    private String publicKey = null;
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // update code 
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private GenerateCoder generateCoder = new GenerateCoder();
    
    public AbstractLisenceGenerator(){
        try {
            keyMap = RSAUtils.genKeyPair();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Error();
        }
    }
    
    @Override
    public String getSecretKey() {
        if(secretKey != null) return secretKey;
        String s = null;
        try{
            s = keyMap == null ? null : RSAUtils.getPrivateKey(keyMap);
        } catch(Exception e){
            throw new RuntimeException(null,e);
        }
        if(s == null || s.equals(""))
            throw new RuntimeException("not found my spider!");
        secretKey = s;
        return secretKey;
    }
 
    @Override
    public String getPublicKey() {
        if(publicKey == null){
            try{
                publicKey = RSAUtils.getPublicKey(keyMap);
            } catch(Exception e){
                throw new RuntimeException(null,e);
            }
            if(publicKey == null || publicKey.equals("")){
                throw new RuntimeException("not found my spider!");
            }
        }
        return publicKey;
    }
 
    @Override
    public byte[] generate(String message) throws Exception {
//        Assert.isTrue(StringUtils.isNotEmpty(message));
        if(message == null || message.equals(""))
            throw new IllegalArgumentException();
        String[] texts = RSAUtils.toArray(message);
        if(texts.length != 5)
            throw new IllegalArgumentException();
        return generateCoder.getFileContent(Integer.parseInt(texts[1]), texts[2].toString()
                , Long.parseLong(texts[3]), Long.parseLong(texts[4]));
    }
    
    @Override
    public Object generate(String message, File destFile) throws Exception {
        if(message == null || message.equals("")){
            throw new IllegalArgumentException();
        }
//        Assert.notNull(destFile);
        
//        String publicKey = getPublicKey();
//        assert (publicKey != null && !publicKey.equals(""));
//        
//        // 加密内容
//        byte[] encryptData = RSAUtils.encryptByPrivateKey(message.toString().getBytes(), getSecretKey());
//        ByteBuffer result = ByteBuffer.allocate(encryptData.length + 2 + publicKey.length());
//        result.put(encryptData);
//        result.put("\n".getBytes());
//        result.put(publicKey.getBytes());
//        result.flip();
        
        //
        if(message == null || message.equals(""))
            throw new IllegalArgumentException();
        String[] texts = RSAUtils.toArray(message);
        if(texts.length != 5)
            throw new IllegalArgumentException();
        generateCoder.writeLisenceFile(texts[0].toString()
                , Integer.parseInt(texts[1]), texts[2].toString()
                , Long.parseLong(texts[3]), Long.parseLong(texts[4]), destFile);
        return null;
    }
    
//    /**
//     * 记录生成的协议文件
//     * @param message
//     */
//    void recordLisence(String message){
//        File destFile = new File(RECORD_FILE_PREFIX + message + RECORD_FILE_SUFFIX);
//        StringBuilder content = new StringBuilder();
//        content.append(message);
//        content.append("\r\n");
//        content.append("secretKey: ");
//        content.append(getSecretKey());
//        content.append("\r\n");
//        content.append("publicKey: ");
//        content.append(getPublicKey());
//        content.append("\r\n");
//        
//        try {
//            FileUtils.writeTxtFile(content.toString(), destFile);
//            System.out.println("----------------> 生成文件: " + destFile);
//        } catch (Exception e) {
//            throw new NestedRuntimeException("write lisence record file failed!", e);
//        }
//    }
    
    @Override
    public void print() {
        // TODO Auto-generated method stub
        try{
            StringBuilder sb = new StringBuilder();
            sb.append("[lisenceGenerator: secretKey = ");
            sb.append(getSecretKey());
            sb.append(", publicKey = ");
            sb.append(getPublicKey());
            sb.append("]");
            System.out.println(sb);
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * 产生真正的协议对象,文件、对象、数据库等格式,子类预留实现。
     * @param encryptData 加密过的协议数据
     * @return
     */
    protected abstract Object generateObject(byte[] encryptData);
    
    private class GenerateCoder {
//        private String pk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCL4fwI6s3zdK0RIDQ0o9xQ7Lq4CoETtOKbDRSCwdCeQ5PrWys4tlenCGOds5SAkOrXFt7wOt0Z33PlAMXpLxAq1gDp/vlOLPPxTfm7S6mUhGNrYOv/0FXQ3rWa7G/UZaXlybn7WLcr2yFaJbwSxxKWa/WuNc1wyCii1S854ZvGBQIDAQAB";
        private String sk = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIvh/AjqzfN0rREgNDSj3FDsurgKgRO04psNFILB0J5Dk+tbKzi2V6cIY52zlICQ6tcW3vA63Rnfc+UAxekvECrWAOn++U4s8/FN+btLqZSEY2tg6//QVdDetZrsb9RlpeXJuftYtyvbIVolvBLHEpZr9a41zXDIKKLVLznhm8YFAgMBAAECgYBBauEMZosEdSdO4AmqKIurSNqVoUv2JFzpeDeEYxGmQiJWKh/PeO8LDZBQuyAS9DLvfiGqpUyrgeXYTgFAaSMcw+Xw8+MKvQXlPy+6joP4wZgXdU0LqLCY+tQGlGl/k8saWwRgT3VqqqB3NnuNE5eqvXlNWxWiL3hbKklus1jrQQJBAMrQ5GNQwV2DqIgwtPLOi3OBne/dqf7WEh2DlJZBi86+qJfmkJ9u6EiTTrwK+gkavBtG6JxAquhNQeE9LTApnjUCQQCwkFzLV/mrcRm/Hru9QhzwxHWRj2zy8v0+3F1zdMWEdpxfN2DnBgsO5CDSM7PB6arQAozdjqIh9IgUY/ioMcKRAkAqp2oQ9Q/lseXE1rGHiybK4QytQKoa3TysFlsuipRzo6djYusxjKqvcMOTSp+xQH0lI3GSQwtulajwTORvB6GtAkAQ3Xk/oyOiEKeY7eKY7Vu+U8W3JbAOdM9j1cDHanwDbyD4p12GM7mSro/EBFRDTYKXY+b4sssh2y20ZOg3iKlBAkEAiAmNOldhGlqPfvRkm2fCYCMYcTrsi6Qduz8n82ChuJughXUbBWvVZ0YwvpPUAAX2uhteMP6ZdN3+BK7xSrdyAQ==";
        
//        /**
//         * 写入ip类型授权信息
//         * @param customId
//         * @param address
//         */
//        public void writeLisenceFile(String customId, String address){
//            doWriteLisenceFile(customId, 0, address, 0, 0);
//        }
//        
//        public void writeLisenceFile(String customId, long start, long end){
//            doWriteLisenceFile(customId, 0, "", start, end);
//        }
        public byte[] getFileContent(int type, String address, long start, long end){
            try {
                return RSAUtils.encryptByPrivateKey(getFileData(type, address, start, end).getBytes(), sk);
            } catch (Exception e) {
                throw new Error(e);
            }
        }
        
        public void writeLisenceFile(String customId, int type
                , String address, long start, long end, File destFile){
            doWriteLisenceFile(customId, type, address, start, end, destFile);
        }
        
        private void doWriteLisenceFile(String customId, int type
                , String address, long start, long end, File destFile){
//            File destFile = new File(RECORD_FILE_PREFIX + customId + ".bin");
            String content = getFileData(type, address, start, end);
            try {
                byte[] encryptData = RSAUtils.encryptByPrivateKey(content.getBytes(), sk);
                FileCopyUtils.copy(encryptData, destFile);
            } catch (Exception e) {
                throw new Error(e);
            }
        }
        
        private String getFileData(int type, String address, long start, long end){
            StringBuilder content = new StringBuilder();
//            content.append("license_type=");
            content.append(type);
            content.append("\r\n");
//            content.append("mac_address=");
            content.append(address);
            content.append("\r\n");
//            content.append("start=");
            content.append(start);
            content.append("\r\n");
//            content.append("end=");
            content.append(end);
            return content.toString();
        }
    }
}