AesUtil.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.imed.costaccount.common.util;
  2. import org.apache.commons.codec.binary.Base64;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.spec.IvParameterSpec;
  5. import javax.crypto.spec.SecretKeySpec;
  6. /**
  7. * @author :lilei
  8. * @date :Created in 2020/1/9 14:35
  9. * @description:AES
  10. */
  11. public class AesUtil {
  12. // 密钥
  13. public static String key = "&qsh9li1KxZa9la@";
  14. private static String charset = "utf-8";
  15. // 偏移量
  16. private static int offset = 16;
  17. // 加密器类型:加密算法为AES,加密模式为CBC,补码方式为PKCS5Padding
  18. private static String transformation = "AES/CBC/PKCS5Padding";
  19. // 算法类型:用于指定生成AES的密钥
  20. private static String algorithm = "AES";
  21. /**
  22. * 加密
  23. *
  24. * @param content
  25. * @return
  26. */
  27. public static String encrypt(String content) {
  28. return encrypt(content, key);
  29. }
  30. /**
  31. * 解密
  32. *
  33. * @param content
  34. * @return
  35. */
  36. public static String decrypt(String content) {
  37. return decrypt(content, key);
  38. }
  39. /**
  40. * 加密
  41. *
  42. * @param content 需要加密的内容
  43. * @param key 加密密码
  44. * @return
  45. */
  46. private static String encrypt(String content, String key) {
  47. try {
  48. //构造密钥
  49. SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
  50. //创建初始向量iv用于指定密钥偏移量(可自行指定但必须为128位),因为AES是分组加密,下一组的iv就用上一组加密的密文来充当
  51. IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
  52. //创建AES加密器
  53. Cipher cipher = Cipher.getInstance(transformation);
  54. byte[] byteContent = content.getBytes(charset);
  55. //使用加密器的加密模式
  56. cipher.init(Cipher.ENCRYPT_MODE, skey, iv);
  57. // 加密
  58. byte[] result = cipher.doFinal(byteContent);
  59. //使用BASE64对加密后的二进制数组进行编码
  60. return new Base64().encodeToString(result);
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. return null;
  65. }
  66. /**
  67. * AES(256)解密
  68. *
  69. * @param content 待解密内容
  70. * @param key 解密密钥
  71. * @return 解密之后
  72. * @throws Exception
  73. */
  74. private static String decrypt(String content, String key) {
  75. try {
  76. SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
  77. IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
  78. Cipher cipher = Cipher.getInstance(transformation);
  79. //解密时使用加密器的解密模式
  80. cipher.init(Cipher.DECRYPT_MODE, skey, iv);// 初始化
  81. byte[] result = cipher.doFinal(new Base64().decode(content));
  82. return new String(result); // 解密
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. }
  86. return null;
  87. }
  88. public static void main(String[] args) {
  89. String s = "admin";
  90. String encryptResultStr = encrypt(s);
  91. // 加密
  92. System.out.println("加密前:" + s);
  93. System.out.println("加密后:" + encryptResultStr);
  94. // 解密
  95. System.out.println("解密后:" + decrypt(encryptResultStr));
  96. }
  97. }