曾今写的一个生成某产品licence脚本
曾今写的一个生成某产品licence脚本。
import javax.crypto.Cipher; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * @author Bohr Qiu email: qiuboboy@qq.com */ public class Test { static byte[] desKeyData = { 83, 69, 67, 77, 2, 33, 64, 35 }; static byte[] md5KeyData = { 83, 73, 78, 79, 86, 65, 33, 64, 35 }; static String productName = "eshop"; static String version = "1.0"; static String instalDate = "2008-12-31"; static String ip = ""; /** * @param args */ public static void main(String[] args) throws Exception { String key = generateKey("2010-12-03"); decode("Cki3dFHnV3LeGI9ZGW0nzsykApE43i2WexPXryCOgbw="); System.out.println(generatelicense(key)); } public static byte[] Subbytes(byte[] Bytes, int begin, int len) { byte[] resutl = new byte[len]; int n = 0; for (int i = begin; i < begin + len; ++n) { resutl[n] = Bytes[i]; ++i; } return resutl; } // licence的解密过程 public static void decode(String key) throws Exception { BASE64Decoder b64d = new BASE64Decoder(); byte[] key64 = b64d.decodeBuffer(key);// 对key值进行base64解码 // 根据des密钥构造新的密钥 SecretKeySpec secretKey = new SecretKeySpec(desKeyData, "DES"); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES"); // DECRYPT_MODE=2,为解密模式 cipher.init(2, secretKey); // 正式执行解密操作 // 这是对key值进行base64解码,然后在用desKeyData生成的密码对解码的结果进行解密 byte[] decrypted = cipher.doFinal(key64); // 对解密的结果取最后10位,这就是到期时间 String endData = new String(Subbytes(decrypted, decrypted.length - 10, 10)); System.out.println("到期时间:" + endData); // 对解密的结果取除开最后10位的其他位 byte[] bCheckMD5 = Subbytes(decrypted, 0, decrypted.length - 10); // 构造一个md5密钥 SecretKeySpec secretKeyMD5 = new SecretKeySpec(md5KeyData, "HmacMD5"); Mac mac = Mac.getInstance("HmacMD5"); mac.init(secretKeyMD5); String Str = productName + " " + version + " " + ip + new String(md5KeyData) + " " + instalDate + " " + endData; byte[] result = mac.doFinal(Str.getBytes()); String bCheckMD5Str = new String(bCheckMD5); String resultStr = new String(result); if (bCheckMD5Str.equals(resultStr)) { System.out.println("此license可用"); } else { System.out.println("此licens不可用"); } } /** * 生成key * * @param endData * 到期时间 格式为YYYY-MM-DD * @return key * @throws Exception */ public static String generateKey(String endData) throws Exception { SecretKeySpec secretKeyMD5 = new SecretKeySpec(md5KeyData, "HmacMD5"); Mac mac = Mac.getInstance("HmacMD5"); mac.init(secretKeyMD5); String Str = productName + " " + version + " " + ip + new String(md5KeyData) + " " + instalDate + " " + endData; byte[] result = mac.doFinal(Str.getBytes()); byte[] tmp = endData.getBytes(); byte[] re = new byte[tmp.length + result.length]; for (int i = 0; i < tmp.length + result.length; i++) { if (i < result.length) { re[i] = result[i]; } else { re[i] = tmp[i - result.length]; } } SecretKeySpec secretKey = new SecretKeySpec(desKeyData, "DES"); Cipher cipher = Cipher.getInstance("DES"); // DECRYPT_MODE=1,为加密模式 cipher.init(1, secretKey); byte[] decrypted = cipher.doFinal(re); BASE64Encoder encode = new BASE64Encoder(); return encode.encode(decrypted); } public static String generatelicense(String key) { return "--------------------\n" + "Product_NAME=eshop\n" + "Version=1.0\n" + "Install_Date=2008-12-31\n" + "Key=" + key; } }
这个产品应该没有用了,所以贴出来,如果还在用,请贵公司升级安全机制。