fa7522d00db53dba0889afa657ce3a80c8fbb3a4.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.yihu.utils;
  2. import java.security.InvalidKeyException;
  3. import java.security.SecureRandom;
  4. import javax.crypto.spec.DESKeySpec;
  5. import javax.crypto.SecretKeyFactory;
  6. import javax.crypto.SecretKey;
  7. import javax.crypto.Cipher;
  8. import org.apache.commons.codec.binary.Base64;
  9. /**
  10. * 用于数字ID的加密和解密函数 by lch20150609
  11. */
  12. public class SecretUtil {
  13. private final static String password = "zkzlcom1yihu67890123";
  14. private final static byte[] passwordByte = password.getBytes();
  15. private static Cipher cipher = null;
  16. private static Cipher desCipher = null;
  17. private static String DefaultEncoding="UTF-8";
  18. static{
  19. SecureRandom random = new SecureRandom();
  20. DESKeySpec desKey;
  21. try {
  22. desKey = new DESKeySpec(passwordByte);
  23. // 创建一个密匙工厂,然后用它把DESKeySpec转换成
  24. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  25. SecretKey securekey = keyFactory.generateSecret(desKey);
  26. // Cipher对象实际完成加密操作
  27. cipher = Cipher.getInstance("DES");
  28. // 用密匙初始化Cipher对象
  29. cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
  30. // DES算法要求有一个可信任的随机数源
  31. SecureRandom desRandom = new SecureRandom();
  32. // 创建一个DESKeySpec对象
  33. DESKeySpec desDesKey = new DESKeySpec(passwordByte);
  34. // 创建一个密匙工厂
  35. SecretKeyFactory desKeyFactory = SecretKeyFactory.getInstance("DES");
  36. // 将DESKeySpec对象转换成SecretKey对象
  37. SecretKey desSecurekey = keyFactory.generateSecret(desDesKey);
  38. // Cipher对象实际完成解密操作
  39. desCipher = Cipher.getInstance("DES");
  40. // 用密匙初始化Cipher对象
  41. desCipher.init(Cipher.DECRYPT_MODE, desSecurekey, desRandom);
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. // 测试
  47. public static void main(String args[]) throws Exception {
  48. // 待加密内容
  49. String str = "Apm1234567890_";
  50. String result = SecretUtil.encrypt(str);
  51. System.out.println("加密后:" + new String(result));
  52. // 直接将如上内容解密
  53. String decryResult = SecretUtil.decrypt(result);
  54. System.out.println("解密后:" +decryResult);
  55. }
  56. /**
  57. * 加密
  58. *
  59. * @param 需要加密的字符串
  60. * @return 加密之后的字符串
  61. */
  62. public static String encrypt(String str) {
  63. return encrypt(str,SecretUtil.DefaultEncoding);
  64. }
  65. /**
  66. * 解密
  67. * @param 需要解密的字符串
  68. * @return 解密之后的字符串
  69. */
  70. public static String decrypt(String src) {
  71. return decrypt(src,SecretUtil.DefaultEncoding);
  72. }
  73. /**
  74. * 加密
  75. *
  76. * @param 需要加密的字符串
  77. * @return 加密之后的字符串
  78. */
  79. public static String encrypt(String str,String encoding) {
  80. try {
  81. // 现在,获取数据并加密
  82. // 正式执行加密操作
  83. byte[] datasource=str.getBytes(encoding);
  84. byte[] result=cipher.doFinal(datasource);
  85. return new String(new Base64().encode(result),encoding);
  86. } catch (Throwable e) {
  87. e.printStackTrace();
  88. }
  89. return null;
  90. }
  91. /**
  92. * 解密
  93. * @param 需要解密的字符串
  94. * @return 解密之后的字符串
  95. */
  96. public static String decrypt(String src,String encoding) {
  97. try{
  98. Base64 decoder = new Base64();
  99. byte[] b =decoder.decode(src);
  100. // 真正开始解密操作
  101. return new String(desCipher.doFinal(b),encoding);
  102. }catch(Exception e){
  103. e.printStackTrace();
  104. }
  105. return null;
  106. }
  107. }