DesUtil.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.miyzh.utils;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.SecretKey;
  4. import javax.crypto.SecretKeyFactory;
  5. import javax.crypto.spec.DESKeySpec;
  6. import java.security.SecureRandom;
  7. /**
  8. * Created by Administrator on 2016/8/31.
  9. */
  10. public class DesUtil {
  11. /** 加密、解密key. */
  12. private static final String PASSWORD_CRYPT_KEY = "kEHrDooxWHCWtfeSxvDvgqZq";
  13. /** 加密算法,可用 DES,DESede,Blowfish. */
  14. private final static String ALGORITHM = "DES";
  15. public static void main(String[] args) throws Exception {
  16. String md5Password = "/ysbj/31.htm";
  17. String str = DesUtil.encrypt(md5Password);
  18. System.out.println("str: " + str);
  19. str = DesUtil.decrypt(str);
  20. System.out.println("str: " + str);
  21. }
  22. /**
  23. * 对数据进行DES加密.
  24. * @param data 待进行DES加密的数据
  25. * @return 返回经过DES加密后的数据
  26. * @throws Exception
  27. * @author zhursh
  28. */
  29. public final static String decrypt(String data) throws Exception {
  30. return new String(decrypt(hex2byte(data.getBytes()), PASSWORD_CRYPT_KEY.getBytes()));
  31. }
  32. /**
  33. * 对用DES加密过的数据进行解密.
  34. * @param data DES加密数据
  35. * @return 返回解密后的数据
  36. * @throws Exception
  37. * @author zhursh
  38. */
  39. public final static String encrypt(String data) throws Exception {
  40. return byte2hex(encrypt(data.getBytes(), PASSWORD_CRYPT_KEY.getBytes()));
  41. }
  42. /**
  43. * 用指定的key对数据进行DES加密.
  44. * @param data 待加密的数据
  45. * @param key DES加密的key
  46. * @return 返回DES加密后的数据
  47. * @throws Exception
  48. * @author zhursh
  49. */
  50. private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
  51. // DES算法要求有一个可信任的随机数源
  52. SecureRandom sr = new SecureRandom();
  53. // 从原始密匙数据创建DESKeySpec对象
  54. DESKeySpec dks = new DESKeySpec(key);
  55. // 创建一个密匙工厂,然后用它把DESKeySpec转换成
  56. // 一个SecretKey对象
  57. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  58. SecretKey securekey = keyFactory.generateSecret(dks);
  59. // Cipher对象实际完成加密操作
  60. Cipher cipher = Cipher.getInstance(ALGORITHM);
  61. // 用密匙初始化Cipher对象
  62. cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
  63. // 现在,获取数据并加密
  64. // 正式执行加密操作
  65. return cipher.doFinal(data);
  66. }
  67. /**
  68. * 用指定的key对数据进行DES解密.
  69. * @param data 待解密的数据
  70. * @param key DES解密的key
  71. * @return 返回DES解密后的数据
  72. * @throws Exception
  73. * @author zhursh
  74. */
  75. private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
  76. // DES算法要求有一个可信任的随机数源
  77. SecureRandom sr = new SecureRandom();
  78. // 从原始密匙数据创建一个DESKeySpec对象
  79. DESKeySpec dks = new DESKeySpec(key);
  80. // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
  81. // 一个SecretKey对象
  82. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  83. SecretKey securekey = keyFactory.generateSecret(dks);
  84. // Cipher对象实际完成解密操作
  85. Cipher cipher = Cipher.getInstance(ALGORITHM);
  86. // 用密匙初始化Cipher对象
  87. cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
  88. // 现在,获取数据并解密
  89. // 正式执行解密操作
  90. return cipher.doFinal(data);
  91. }
  92. public static byte[] hex2byte(byte[] b) {
  93. if ((b.length % 2) != 0) throw new IllegalArgumentException("长度不是偶数");
  94. byte[] b2 = new byte[b.length / 2];
  95. for (int n = 0; n < b.length; n += 2) {
  96. String item = new String(b, n, 2);
  97. b2[n / 2] = (byte) Integer.parseInt(item, 16);
  98. }
  99. return b2;
  100. }
  101. public static String byte2hex(byte[] b) {
  102. String hs = "";
  103. String stmp = "";
  104. for (int n = 0; n < b.length; n++) {
  105. stmp = (Integer.toHexString(b[n] & 0XFF));
  106. if (stmp.length() == 1)
  107. hs = hs + "0" + stmp;
  108. else
  109. hs = hs + stmp;
  110. }
  111. return hs.toUpperCase();
  112. }
  113. }