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();
|
}
|
}
|
}
|