对注册码生成的浅谈.

最近为了手头的一个小项目,需要生成一个注册码.并且要求注册码中包含日期信息.

java 代码
  1. package mypackage;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileInputStream;   
  5. import java.io.FileOutputStream;   
  6. import java.io.InputStream;   
  7. import java.io.OutputStream;   
  8. import java.security.*;   
  9. import java.text.DateFormat;   
  10. import java.text.ParseException;   
  11. import java.util.Date;   
  12. import java.util.Properties;   
  13. import java.util.Random;   
  14.   
  15. import javax.crypto.Cipher;   
  16. import javax.crypto.SecretKey;   
  17. import javax.crypto.SecretKeyFactory;   
  18. import javax.crypto.spec.DESKeySpec;   
  19.   
  20. /**  
  21.  * 字符串工具集合  
  22.  *   
  23.  * @author Liudong  
  24.  */  
  25. public class StringTest {   
  26.   
  27.     private static final String PASSWORD_CRYPT_KEY = ".?'_=1dt";// 密钥   
  28.   
  29.     private final static String DES = "DES";//DES算法名称   
  30.   
  31.     private String realKey;// 真实注册码   
  32.   
  33.     /**  
  34.      *   
  35.      * StringTest的构造器.  
  36.      *   
  37.      * @param arg  
  38.      */  
  39.     public StringTest(boolean arg) {   
  40.         Random r = new Random();   
  41.         if(!arg){   
  42.             realKey = r.nextLong()+":"+String.valueOf(System.currentTimeMillis());   
  43.         } else{   
  44.             DateFormat df = DateFormat.getDateInstance();   
  45.             Date d = null;   
  46.             try {   
  47.                 d = df.parse("1981-06-27 00:00:02");   
  48.             } catch (ParseException e) {   
  49.                 e.printStackTrace();   
  50.             }   
  51.             realKey = r.nextLong()+":"+String.valueOf(d.getTime());   
  52.         }   
  53.   
  54.     }   
  55.   
  56.     /**  
  57.      * 加密  
  58.      *   
  59.      * @param src  
  60.      *            数据源  
  61.      * @param key  
  62.      *            密钥,长度必须是8的倍数  
  63.      * @return 返回加密后的数据  
  64.      * @throws Exception  
  65.      */  
  66.     public static byte[] encrypt(byte[] src, byte[] key) throws Exception {   
  67.         // DES算法要求有一个可信任的随机数源   
  68.         SecureRandom sr = new SecureRandom();   
  69.         // 从原始密匙数据创建DESKeySpec对象   
  70.         DESKeySpec dks = new DESKeySpec(key);   
  71.         // 创建一个密匙工厂,然后用它把DESKeySpec转换成   
  72.         // 一个SecretKey对象   
  73.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);   
  74.         SecretKey securekey = keyFactory.generateSecret(dks);   
  75.         // Cipher对象实际完成加密操作   
  76.         Cipher cipher = Cipher.getInstance(DES);   
  77.         // 用密匙初始化Cipher对象   
  78.         cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);   
  79.         // 现在,获取数据并加密   
  80.         // 正式执行加密操作   
  81.         return cipher.doFinal(src);   
  82.     }   
  83.   
  84.     /**  
  85.      * 解密  
  86.      *   
  87.      * @param src  
  88.      *            数据源  
  89.      * @param key  
  90.      *            密钥,长度必须是8的倍数  
  91.      * @return 返回解密后的原始数据  
  92.      * @throws Exception  
  93.      */  
  94.     public static byte[] decrypt(byte[] src, byte[] key) throws Exception {   
  95.         // DES算法要求有一个可信任的随机数源   
  96.         SecureRandom sr = new SecureRandom();   
  97.         // 从原始密匙数据创建一个DESKeySpec对象   
  98.         DESKeySpec dks = new DESKeySpec(key);   
  99.         // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成   
  100.         // 一个SecretKey对象   
  101.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);   
  102.         SecretKey securekey = keyFactory.generateSecret(dks);   
  103.         // Cipher对象实际完成解密操作   
  104.         Cipher cipher = Cipher.getInstance(DES);   
  105.         // 用密匙初始化Cipher对象   
  106.         cipher.init(Cipher.DECRYPT_MODE, securekey, sr);   
  107.         // 现在,获取数据并解密   
  108.         // 正式执行解密操作   
  109.         return cipher.doFinal(src);   
  110.     }   
  111.   
  112.     /**  
  113.      *   
  114.      * 二行制转字符串  
  115.      *   
  116.      * @param b  
  117.      *   
  118.      * @return  
  119.      *   
  120.      */  
  121.   
  122.     public static String byte2hex(byte[] b) {   
  123.   
  124.         String hs = "";   
  125.   
  126.         String stmp = "";   
  127.   
  128.         for (int n = 0; n < b.length; n++) {   
  129.   
  130.             stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));   
  131.             if (stmp.length() == 1)   
  132.                 hs = hs + "0" + stmp;   
  133.             else  
  134.                 hs = hs + stmp;   
  135.         }   
  136.   
  137.         return hs.toUpperCase();   
  138.   
  139.     }   
  140.   
  141.     /**  
  142.      *   
  143.      * 功能:字符串二行制  
  144.      *   
  145.      * @param b  
  146.      * @return  
  147.      *   
  148.      */  
  149.     public static byte[] hex2byte(byte[] b) {   
  150.   
  151.         if ((b.length % 2) != 0)   
  152.             throw new IllegalArgumentException("长度不是偶数");   
  153.         byte[] b2 = new byte[b.length / 2];   
  154.         for (int n = 0; n < b.length; n += 2) {   
  155.             String item = new String(b, n, 2);   
  156.             b2[n / 2] = (byte) Integer.parseInt(item, 16);   
  157.         }   
  158.         return b2;   
  159.     }   
  160.   
  161.     /*  
  162.      * (non-Javadoc)  
  163.      *   
  164.      * @see java.lang.Object#toString()  
  165.      */  
  166.     @Override  
  167.     public String toString() {   
  168.   
  169.         byte[] b = null;   
  170.         try {   
  171.             b = encrypt(realKey.getBytes(), PASSWORD_CRYPT_KEY.getBytes());   
  172.         } catch (Exception e) {   
  173.   
  174.             e.printStackTrace();   
  175.         }   
  176.         String encryptKey = byte2hex(b);   
  177.         return encryptKey;   
  178.     }   
  179.   
  180.     public static void main(String[] args) throws Exception {   
  181.            
  182.         /*  
  183.          * 1.生成注册码  
  184.          */  
  185.         String data = new StringTest(false).toString();   
  186.         System.out.println("未注册注册码:"+data);   
  187.         System.out.println("未注册注册码长度:"+data.length());   
  188.         /*  
  189.          * 2.写入属性文件  
  190.          */  
  191.            
  192.         Properties pro = null;   
  193.            
  194.         File file = new File("d:\\key.properties");   
  195.         if(file.exists()){//文件已存在   
  196.             InputStream in = new FileInputStream("d:\\key.properties");   
  197.             pro = new Properties();   
  198.             pro.load(in);   
  199.             pro.setProperty("key.data", data);   
  200.             OutputStream out = new FileOutputStream("d:\\key.properties");   
  201.             pro.store(out, "key");   
  202.         } else{//文件不存在   
  203.             pro = new Properties();   
  204.             pro.setProperty("key.data", data);   
  205.             OutputStream out = new FileOutputStream("d:\\key.properties");   
  206.             pro.store(out, "key");   
  207.         }   
  208.         /*  
  209.          * 3.解密注册码  
  210.          */  
  211.         String decryptKey = new String(decrypt(hex2byte(data.getBytes()), PASSWORD_CRYPT_KEY   
  212.                 .getBytes()));   
  213.         System.out.println("解密后注册码:" + decryptKey);   
  214.         Date d = new Date();   
  215.         d.setTime(Long.valueOf(decryptKey.substring(   
  216.                 decryptKey.lastIndexOf(":") + 1, decryptKey.length())));   
  217.         DateFormat df = DateFormat.getDateInstance();   
  218.         System.out.println("解密后有效信息:"+df.format(d));   
  219.     }   
  220. }   

 

这里采用了DES算法进行加密.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值