DesUtil.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.kxs.adminap.util;
  2. import java.nio.charset.StandardCharsets;
  3. import java.security.Key;
  4. import java.security.SecureRandom;
  5. import java.util.Base64;
  6. import javax.crypto.Cipher;
  7. import javax.crypto.KeyGenerator;
  8. import javax.crypto.SecretKeyFactory;
  9. import javax.crypto.spec.DESedeKeySpec;
  10. import javax.crypto.spec.IvParameterSpec;
  11. public class DesUtil {
  12. public static Key setKey(String strKey) {
  13. Key key = null;
  14. try {
  15. KeyGenerator generator = KeyGenerator.getInstance("DES");
  16. generator.init(new SecureRandom(strKey.getBytes())); // 根据参数生成key
  17. key = generator.generateKey();
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. return key;
  22. }
  23. /**
  24. * @param source
  25. * 编码内容
  26. * @param key
  27. * 密钥
  28. * @param charSet
  29. * 编码格式
  30. * @return
  31. */
  32. public static String encrypt(String source, String key, String charSet) {
  33. String encrypt = null;
  34. try {
  35. byte[] ret = encrypt(source.getBytes(charSet), key);
  36. encrypt = new String(Base64.getEncoder().encode(ret));
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. encrypt = null;
  40. }
  41. return encrypt;
  42. }
  43. /**
  44. * @param encryptedData
  45. * 解码内容
  46. * @param key
  47. * 密钥
  48. * @param charSet
  49. * 编码格式
  50. * @return
  51. */
  52. public static String decrypt(String encryptedData, String key, String charSet) {
  53. // String descryptedData = null;
  54. // try {
  55. // byte[] ret = descrypt(Base64.getDecoder().decode(encryptedData.getBytes(StandardCharsets.UTF_8)), key);
  56. // descryptedData = new String(ret, charSet);
  57. // } catch (Exception e) {
  58. // e.printStackTrace();
  59. // descryptedData = null;
  60. // }
  61. // return descryptedData;
  62. String result = "";
  63. try {
  64. Key desKey = setKey(key);
  65. Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
  66. cipher.init(Cipher.DECRYPT_MODE, desKey);
  67. byte[] base64Byte = Base64.getDecoder().decode(encryptedData);
  68. byte[] decryptData = cipher.doFinal(base64Byte);
  69. result = new String(decryptData, StandardCharsets.UTF_8);
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. }
  73. return result;
  74. }
  75. private static byte[] encrypt(byte[] primaryData, String key) {
  76. Key desKey = setKey(key);
  77. try {
  78. Cipher cipher = Cipher.getInstance("DES"); // Cipher对象实际完成加密操作
  79. cipher.init(Cipher.ENCRYPT_MODE, desKey); // 用密钥初始化Cipher对象(加密)
  80. return cipher.doFinal(primaryData);
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. return null;
  84. }
  85. }
  86. private static byte[] descrypt(byte[] encryptedData, String key) {
  87. Key desKey = setKey(key);
  88. try {
  89. Cipher cipher = Cipher.getInstance("DES"); // Cipher对象实际完成解密操作
  90. cipher.init(Cipher.DECRYPT_MODE, desKey); // 用密钥初始化Cipher对象(解密)
  91. return cipher.doFinal(encryptedData);
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. return null;
  95. }
  96. }
  97. }