8ea938902076578cf7ddc8a49364cfb3a09f9bd2.svn-base 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.toolkit.DES;
  2. import java.security.*;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.SecretKey;
  5. import javax.crypto.SecretKeyFactory;
  6. import javax.crypto.spec.DESKeySpec;
  7. /**
  8. * 实现DES加密和解密的过程
  9. *
  10. * @author Liudong
  11. */
  12. public class DESCipher{
  13. private final static String DES = "DES";
  14. /**
  15. * 加密
  16. *
  17. * @param src
  18. * 数据源
  19. * @param key
  20. * 密钥,长度必须是8的倍数
  21. * @return 返回加密后的数据
  22. * @throws Exception
  23. */
  24. public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
  25. // DES算法要求有一个可信任的随机数源
  26. SecureRandom sr = new SecureRandom();
  27. // 从原始密匙数据创建DESKeySpec对象
  28. DESKeySpec dks = new DESKeySpec(key);
  29. // 创建一个密匙工厂,然后用它把DESKeySpec转换成
  30. // 一个SecretKey对象
  31. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
  32. SecretKey securekey = keyFactory.generateSecret(dks);
  33. // Cipher对象实际完成加密操作
  34. Cipher cipher = Cipher.getInstance(DES);
  35. // 用密匙初始化Cipher对象
  36. cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
  37. // 现在,获取数据并加密
  38. // 正式执行加密操作
  39. return cipher.doFinal(src);
  40. }
  41. /**
  42. * 解密
  43. *
  44. * @param src
  45. * 数据源
  46. * @param key
  47. * 密钥,长度必须是8的倍数
  48. * @return 返回解密后的原始数据
  49. * @throws Exception
  50. */
  51. public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
  52. // DES算法要求有一个可信任的随机数源
  53. SecureRandom sr = new SecureRandom();
  54. // 从原始密匙数据创建一个DESKeySpec对象
  55. DESKeySpec dks = new DESKeySpec(key);
  56. // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
  57. // 一个SecretKey对象
  58. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
  59. SecretKey securekey = keyFactory.generateSecret(dks);
  60. // Cipher对象实际完成解密操作
  61. Cipher cipher = Cipher.getInstance(DES);
  62. // 用密匙初始化Cipher对象
  63. cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
  64. // 现在,获取数据并解密
  65. // 正式执行解密操作
  66. return cipher.doFinal(src);
  67. }
  68. /**
  69. * 二进制转字符串
  70. *
  71. * @param b
  72. * @return
  73. */
  74. @SuppressWarnings("unused")
  75. private static String byte2hex(byte[] b) {
  76. String hs = "";
  77. String stmp = "";
  78. for (int n = 0; n < b.length; n++) {
  79. stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
  80. if (stmp.length() == 1)
  81. hs = hs + "0" + stmp;
  82. else
  83. hs = hs + stmp;
  84. if (n < b.length - 1)
  85. hs = hs + ":";
  86. }
  87. return hs.toUpperCase();
  88. }
  89. /**
  90. * 测试入口
  91. *
  92. * @param args
  93. */
  94. public static void main(String[] args) throws Exception {
  95. byte[] key = "Hello123".getBytes();
  96. byte[] src = "你好,中国".getBytes();
  97. byte[] des = encrypt(src, key);
  98. des = decrypt(des, key);
  99. }
  100. }