AesEncryptUtil.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.ruixuan.common.utils.encryption;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.spec.IvParameterSpec;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import java.io.UnsupportedEncodingException;
  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. *
  16. * @param data 要加密的数据
  17. * @param key 加密key
  18. * @param iv 加密iv
  19. * @return 加密的结果
  20. * @throws Exception
  21. */
  22. /*public static String encrypt(String data, String key, String iv) {
  23. try {
  24. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//"算法/模式/补码方式"NoPadding PkcsPadding
  25. int blockSize = cipher.getBlockSize();
  26. byte[] dataBytes = data.getBytes();
  27. int plaintextLength = dataBytes.length;
  28. if (plaintextLength % blockSize != 0) {
  29. plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
  30. }
  31. byte[] plaintext = new byte[plaintextLength];
  32. System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
  33. SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
  34. IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
  35. cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
  36. byte[] encrypted = cipher.doFinal(plaintext);
  37. return Base64.encodeToString(encrypted);
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. return null;
  41. }
  42. }*/
  43. /**
  44. * 解密方法
  45. *
  46. * @param data 要解密的数据
  47. * @param key 解密key
  48. * @param iv 解密iv
  49. * @return 解密的结果
  50. * @throws Exception
  51. */
  52. /*public static String desEncrypt(String data, String key, String iv) {
  53. try {
  54. byte[] encrypted1 = Base64.decode(data);
  55. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  56. SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
  57. IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
  58. cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
  59. byte[] original = cipher.doFinal(encrypted1);
  60. String originalString = new String(original);
  61. return originalString;
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. return null;
  65. }
  66. }*/
  67. private AesEncryptUtil() {
  68. }
  69. /**
  70. * 使用默认的key和iv加密
  71. *
  72. * @param data
  73. * @return
  74. * @throws Exception
  75. */
  76. /*public static String encrypt(String data) {
  77. return encrypt(data, EncryptedString.key, EncryptedString.iv);
  78. }*/
  79. /**
  80. * 使用默认的key和iv解密
  81. *
  82. * @param data
  83. * @return
  84. */
  85. /*public static String desEncrypt(String data) {
  86. return desEncrypt(data, EncryptedString.key, EncryptedString.iv);
  87. }*/
  88. public static String getUrlDecoderString(String str) {
  89. String result = "";
  90. if (null == str) {
  91. return "";
  92. }
  93. try {
  94. result = java.net.URLDecoder.decode(str, "UTF-8");
  95. } catch (UnsupportedEncodingException e) {
  96. }
  97. return result;
  98. }
  99. }