897540fe2693e3633717c2ae67d1dead12740064.svn-base 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package org.jeecg.common.util.encryption;
  2. import org.apache.shiro.codec.Base64;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.spec.IvParameterSpec;
  5. import javax.crypto.spec.SecretKeySpec;
  6. /**
  7. * AES 加密
  8. */
  9. public class AesEncryptUtil {
  10. //使用AES-128-CBC加密模式,key需要为16位,key和iv可以相同!
  11. private static String KEY = EncryptedString.key;
  12. private static String IV = EncryptedString.iv;
  13. /**
  14. * 加密方法
  15. * @param data 要加密的数据
  16. * @param key 加密key
  17. * @param iv 加密iv
  18. * @return 加密的结果
  19. * @throws Exception
  20. */
  21. public static String encrypt(String data, String key, String iv) throws Exception {
  22. try {
  23. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//"算法/模式/补码方式"NoPadding PkcsPadding
  24. int blockSize = cipher.getBlockSize();
  25. byte[] dataBytes = data.getBytes();
  26. int plaintextLength = dataBytes.length;
  27. if (plaintextLength % blockSize != 0) {
  28. plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
  29. }
  30. byte[] plaintext = new byte[plaintextLength];
  31. System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
  32. SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
  33. IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
  34. cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
  35. byte[] encrypted = cipher.doFinal(plaintext);
  36. return Base64.encodeToString(encrypted);
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. return null;
  40. }
  41. }
  42. /**
  43. * 解密方法
  44. * @param data 要解密的数据
  45. * @param key 解密key
  46. * @param iv 解密iv
  47. * @return 解密的结果
  48. * @throws Exception
  49. */
  50. public static String desEncrypt(String data, String key, String iv) throws Exception {
  51. try {
  52. byte[] encrypted1 = Base64.decode(data);
  53. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  54. SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
  55. IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
  56. cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
  57. byte[] original = cipher.doFinal(encrypted1);
  58. String originalString = new String(original);
  59. return originalString;
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. return null;
  63. }
  64. }
  65. /**
  66. * 使用默认的key和iv加密
  67. * @param data
  68. * @return
  69. * @throws Exception
  70. */
  71. public static String encrypt(String data) throws Exception {
  72. return encrypt(data, KEY, IV);
  73. }
  74. /**
  75. * 使用默认的key和iv解密
  76. * @param data
  77. * @return
  78. * @throws Exception
  79. */
  80. public static String desEncrypt(String data) throws Exception {
  81. return desEncrypt(data, KEY, IV);
  82. }
  83. // /**
  84. // * 测试
  85. // */
  86. // public static void main(String args[]) throws Exception {
  87. // String test1 = "sa";
  88. // String test =new String(test1.getBytes(),"UTF-8");
  89. // String data = null;
  90. // String key = KEY;
  91. // String iv = IV;
  92. // // /g2wzfqvMOeazgtsUVbq1kmJawROa6mcRAzwG1/GeJ4=
  93. // data = encrypt(test, key, iv);
  94. // System.out.println("数据:"+test);
  95. // System.out.println("加密:"+data);
  96. // String jiemi =desEncrypt(data, key, iv).trim();
  97. // System.out.println("解密:"+jiemi);
  98. // }
  99. }