SecretKeyUtil.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.sooka.utils;
  2. import sun.misc.BASE64Decoder;
  3. import sun.misc.BASE64Encoder;
  4. import javax.crypto.Cipher;
  5. import javax.crypto.KeyGenerator;
  6. import javax.crypto.SecretKey;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import java.security.SecureRandom;
  9. /**
  10. * 秘钥生成器工具类
  11. *
  12. * @author lei_wang
  13. */
  14. public class SecretKeyUtil {
  15. /**
  16. * 加密
  17. * 1.构造**生成器
  18. * 2.根据ecnodeRules规则初始化**生成器
  19. * 3.产生**
  20. * 4.创建和初始化密码器
  21. * 5.内容加密
  22. * 6.返回字符串
  23. */
  24. public static String AESEncode(String encodeRules, String content) {
  25. try {
  26. //1.构造**生成器,指定为AES算法,不区分大小写
  27. KeyGenerator aes = KeyGenerator.getInstance("AES");
  28. //2.根据ecnodeRules规则初始化**生成器
  29. //生成一个128位的随机源,根据传入的字节数组
  30. aes.init(128, new SecureRandom(encodeRules.getBytes()));
  31. //3.产生原始对称**
  32. SecretKey original_key = aes.generateKey();
  33. //4.获得原始对称**的字节数组
  34. byte[] raw = original_key.getEncoded();
  35. //5.根据字节数组生成AES**
  36. SecretKey key = new SecretKeySpec(raw, "AES");
  37. //6.根据指定算法AES自成密码器
  38. Cipher cipher = Cipher.getInstance("AES");
  39. //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY
  40. cipher.init(Cipher.ENCRYPT_MODE, key);
  41. //8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
  42. byte[] byte_encode = content.getBytes("utf-8");
  43. //9.根据密码器的初始化方式--加密:将数据加密
  44. byte[] byte_AES = cipher.doFinal(byte_encode);
  45. //10.将加密后的数据转换为字符串
  46. //这里用Base64Encoder中会找不到包
  47. //解决办法:
  48. //在项目的Build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。
  49. String AES_encode = new String(new BASE64Encoder().encode(byte_AES));
  50. //11.将字符串返回
  51. return AES_encode;
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. //如果有错就返加nulll
  56. return null;
  57. }
  58. /**
  59. * 解密
  60. * 解密过程:
  61. * 1.同加密1-4步
  62. * 2.将加密后的字符串反纺成byte[]数组
  63. * 3.将加密内容解密
  64. */
  65. public static String AESDncode(String encodeRules, String content) {
  66. try {
  67. //1.构造**生成器,指定为AES算法,不区分大小写
  68. KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
  69. //2.根据ecnodeRules规则初始化**生成器
  70. //生成一个128位的随机源,根据传入的字节数组
  71. keyGenerator.init(128, new SecureRandom(encodeRules.getBytes()));
  72. //3.产生原始对称**
  73. SecretKey original_key = keyGenerator.generateKey();
  74. //4.获得原始对称**的字节数组
  75. byte[] raw = original_key.getEncoded();
  76. //5.根据字节数组生成AES**
  77. SecretKey key = new SecretKeySpec(raw, "AES");
  78. //6.根据指定算法AES自成密码器
  79. Cipher cipher = Cipher.getInstance("AES");
  80. //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
  81. cipher.init(Cipher.DECRYPT_MODE, key);
  82. //8.将加密并编码后的内容解码成字节数组
  83. byte[] byte_content = new BASE64Decoder().decodeBuffer(content);
  84. /*
  85. * 解密
  86. */
  87. byte[] byte_decode = cipher.doFinal(byte_content);
  88. String AES_decode = new String(byte_decode, "utf-8");
  89. return AES_decode;
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. }
  93. //如果有错就返加nulll
  94. return null;
  95. }
  96. public static void main(String[] args) {
  97. /*
  98. * 加密
  99. */
  100. String str = SecretKeyUtil.AESEncode("3fc674da58", "123/政数局/2022-10-20 - 2022-10-29/b29c94d72ee74c6d94b573d90020ea59");
  101. System.out.println("加密后的密文是:" + str);
  102. /*
  103. * 解密
  104. */
  105. System.out.println("解密后的明文是:" + SecretKeyUtil.AESDncode("3fc674da58", str));
  106. }
  107. }