AesUtil.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.fdkankan.sign;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.spec.IvParameterSpec;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import java.util.Base64;
  6. import java.util.Random;
  7. import java.util.UUID;
  8. /**
  9. * AES 128bit 加密解密工具类,用于密码加密
  10. */
  11. public class AesUtil {
  12. //使用AES-128-CBC加密模式,key需要为16位,key和iv可以相同!
  13. public static String key = "0000000563613487";
  14. public static String iv = "vyno4ve9shdq937e";
  15. /**
  16. * 加密方法
  17. * @param data 要加密的数据
  18. * @param key 加密key
  19. * @param iv 加密iv
  20. * @return 加密的结果
  21. * @throws Exception
  22. */
  23. public static String encrypt(String data, String key, String iv) throws Exception{
  24. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//"算法/模式/补码方式"
  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.getEncoder().encodeToString(encrypted); //通过Base64转码返回
  38. }
  39. public static String encrypt(String data) throws Exception{
  40. return encrypt(data,key,iv);
  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. byte[] encrypted1 = Base64.getDecoder().decode(data);
  52. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  53. SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
  54. IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
  55. cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); //使用密钥初始化,设置为解密模式
  56. byte[] original = cipher.doFinal(encrypted1); //执行操作
  57. return new String(original).trim();
  58. }
  59. public static String desEncrypt(String data) throws Exception{
  60. return desEncrypt(data,key,iv);
  61. }
  62. /**
  63. * 用于生成一组16位随机数 key
  64. * @return
  65. */
  66. public static String getRandomStringKey() {
  67. int hashCodeValue = UUID.randomUUID().hashCode();
  68. if(hashCodeValue < 0) hashCodeValue = -hashCodeValue;
  69. return String.format("%016d",hashCodeValue);//左边补0,16位,进制(d,x)
  70. }
  71. /**
  72. * 用于生成16位的随机数 iv
  73. * @return
  74. */
  75. public static String getRandomStringIv(){
  76. String base = "abcdefghijklmnopqrstuvwxyz0123456789";
  77. Random random=new Random();
  78. StringBuffer key = new StringBuffer();
  79. for(int i=0;i<16;i++){
  80. int keyNumber = random.nextInt(base.length());
  81. key.append(base.charAt(keyNumber));
  82. }
  83. return key.toString();
  84. }
  85. /**
  86. * 测试
  87. */
  88. public static void main(String args[]) throws Exception {
  89. String data = "Aa111111";
  90. // String key = getRandomStringKey(); //0000001210830863
  91. // String iv = getRandomStringIv(); //1xo1ub7m2rsz92ev
  92. System.out.println(key);
  93. System.out.println(iv);
  94. String enData = encrypt(data, key, iv);
  95. System.out.println(enData);
  96. System.out.println(desEncrypt("vvEsbkugGPaFjGJeZ6MHK2joojv0juIMeUoa\\/oJMUi8=", key, iv));
  97. }
  98. }