当前位置:   article > 正文

加解密工具类_aes加解密工具类

aes加解密工具类

一、Aes加解密工具类

  1. public class Aes {
  2. /***
  3. * key和iv值可以随机生成
  4. */
  5. private static String KEY = "123456789";
  6. private static String IV = "123456789";
  7. /***
  8. * 加密
  9. * @param data 要加密的数据
  10. * @return encrypt
  11. */
  12. public static String encrypt(String data){
  13. return encrypt(data, KEY, IV);
  14. }
  15. /***
  16. * param data 需要解密的数据
  17. * 调用desEncrypt()方法
  18. */
  19. public static String desEncrypt(String data){
  20. return desEncrypt(data, KEY, IV);
  21. }
  22. /**
  23. * 加密方法
  24. * @param data 要加密的数据
  25. * @param key 加密key
  26. * @param iv 加密iv
  27. * @return 加密的结果
  28. */
  29. private static String encrypt(String data, String key, String iv){
  30. try {
  31. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  32. int blockSize = cipher.getBlockSize();
  33. byte[] dataBytes = data.getBytes();
  34. int plaintextLength = dataBytes.length;
  35. if (plaintextLength % blockSize != 0) {
  36. plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
  37. }
  38. byte[] plaintext = new byte[plaintextLength];
  39. System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
  40. SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
  41. IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
  42. cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
  43. byte[] encrypted = cipher.doFinal(plaintext);
  44. return new Base64().encodeToString(encrypted);
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. return null;
  48. }
  49. }
  50. /**
  51. * 解密方法
  52. * @param data 要解密的数据
  53. * @param key 解密key
  54. * @param iv 解密iv
  55. * @return 解密的结果
  56. */
  57. private static String desEncrypt(String data, String key, String iv){
  58. try {
  59. byte[] encrypted1 = new Base64().decode(data);
  60. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  61. SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
  62. IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
  63. cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
  64. byte[] original = cipher.doFinal(encrypted1);
  65. return new String(original).trim();
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. return null;
  69. }
  70. }

二、Md5盐值加密

  1. public class Md5Util {
  2. /**
  3. * 普通MD5
  4. * @return
  5. */
  6. public static String MD5(String input) {
  7. MessageDigest md5 = null;
  8. try {
  9. md5 = MessageDigest.getInstance("MD5");
  10. } catch (NoSuchAlgorithmException e) {
  11. return "check jdk";
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. return "";
  15. }
  16. char[] charArray = input.toCharArray();
  17. byte[] byteArray = new byte[charArray.length];
  18. for (int i = 0; i < charArray.length; i++) {
  19. byteArray[i] = (byte) charArray[i];
  20. }
  21. byte[] md5Bytes = md5.digest(byteArray);
  22. StringBuffer hexValue = new StringBuffer();
  23. for (int i = 0; i < md5Bytes.length; i++) {
  24. int val = ((int) md5Bytes[i]) & 0xff;
  25. if (val < 16) {
  26. hexValue.append("0");
  27. }
  28. hexValue.append(Integer.toHexString(val));
  29. }
  30. return hexValue.toString();
  31. }
  32. /**
  33. * 加盐MD5
  34. * @param password
  35. * @return
  36. */
  37. public static String generate(String password) {
  38. Random r = new Random();
  39. StringBuilder sb = new StringBuilder(16);
  40. sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
  41. int len = sb.length();
  42. if (len < 16) {
  43. for (int i = 0; i < 16 - len; i++) {
  44. sb.append("0");
  45. }
  46. }
  47. String salt = sb.toString();
  48. password = md5Hex(password + salt);
  49. char[] cs = new char[48];
  50. for (int i = 0; i < 48; i += 3) {
  51. cs[i] = password.charAt(i / 3 * 2);
  52. char c = salt.charAt(i / 3);
  53. cs[i + 1] = c;
  54. cs[i + 2] = password.charAt(i / 3 * 2 + 1);
  55. }
  56. return new String(cs);
  57. }
  58. /**
  59. * 校验加盐后是否和原文一致
  60. * @param password
  61. * @param md5
  62. * @return
  63. */
  64. public static boolean verify(String password, String md5) {
  65. char[] cs1 = new char[32];
  66. char[] cs2 = new char[16];
  67. for (int i = 0; i < 48; i += 3) {
  68. cs1[i / 3 * 2] = md5.charAt(i);
  69. cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);
  70. cs2[i / 3] = md5.charAt(i + 1);
  71. }
  72. String salt = new String(cs2);
  73. return md5Hex(password + salt).equals(new String(cs1));
  74. }
  75. /**
  76. * 获取十六进制字符串形式的MD5摘要
  77. */
  78. private static String md5Hex(String src) {
  79. try {
  80. MessageDigest md5 = MessageDigest.getInstance("MD5");
  81. byte[] bs = md5.digest(src.getBytes());
  82. return new String(Hex.encode(bs));
  83. } catch (Exception e) {
  84. return null;
  85. }
  86. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/511709
推荐阅读
相关标签
  

闽ICP备14008679号