Java加密算法DES

x33g5p2x  于2021-12-25 转载在 其他  
字(2.6k)|赞(0)|评价(0)|浏览(491)

标准的文档地址(click me)

密钥加密也称为对称加密,速度快,但加密和解密的钥匙必须相同,只有通信双方才能知道钥匙

  1. package com.stone.security;
  2. import java.security.Key;
  3. import java.security.SecureRandom;
  4. import javax.crypto.Cipher;
  5. import javax.crypto.KeyGenerator;
  6. import javax.crypto.SecretKey;
  7. import javax.crypto.SecretKeyFactory;
  8. import javax.crypto.spec.DESKeySpec;
  9. import javax.crypto.spec.IvParameterSpec;
  10. /**
  11. * DES 算法 1972美国IBM研制,对称加密算法
  12. * @author stone
  13. * @date 2014-03-10 04:47:05
  14. */
  15. public class DES {
  16. // 算法名称
  17. public static final String KEY_ALGORITHM = "DES";
  18. // 算法名称/加密模式/填充方式
  19. public static final String CIPHER_ALGORITHM_ECB = "DES/ECB/PKCS5Padding";
  20. public static final String CIPHER_ALGORITHM_CBC = "DES/CBC/PKCS5Padding";
  21. public static void main(String[] args) throws Exception {
  22. /*
  23. * 使用 ECB mode
  24. * 密钥生成器 生成密钥
  25. * ECB mode cannot use IV
  26. */
  27. byte[] key = generateKey();
  28. byte[] encrypt = encrypt("胃炎F#*(x)".getBytes(), key);
  29. System.out.println(new String(decrypt(encrypt, key)));
  30. /*
  31. * 使用CBC mode
  32. * 使用密钥工厂生成密钥,加密 解密
  33. * iv: DES in CBC mode and RSA ciphers with OAEP encoding operation.
  34. */
  35. DESKeySpec dks = new DESKeySpec(generateKey());
  36. SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
  37. SecretKey secretKey = factory.generateSecret(dks);
  38. Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
  39. cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(getIV()));
  40. byte[] enc = cipher.doFinal("胃炎A%F#*(x)".getBytes()); //加密
  41. cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(getIV()));
  42. byte[] dec = cipher.doFinal(enc); // 解密
  43. System.out.println(new String(dec));
  44. }
  45. static byte[] getIV() {
  46. String iv = "asdfivh7"; //IV length: must be 8 bytes long
  47. return iv.getBytes();
  48. }
  49. /**
  50. * 生成密钥
  51. *
  52. * @return
  53. * @throws Exception
  54. */
  55. private static byte[] generateKey() throws Exception {
  56. KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
  57. keyGenerator.init(56); //des 必须是56, 此初始方法不必须调用
  58. SecretKey secretKey = keyGenerator.generateKey();
  59. return secretKey.getEncoded();
  60. }
  61. /**
  62. * 还原密钥
  63. *
  64. * @param key
  65. * @return
  66. * @throws Exception
  67. */
  68. private static Key toKey(byte[] key) throws Exception {
  69. DESKeySpec des = new DESKeySpec(key);
  70. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
  71. SecretKey secretKey = keyFactory.generateSecret(des);
  72. return secretKey;
  73. }
  74. /**
  75. * 加密
  76. * @param data 原文
  77. * @param key
  78. * @return 密文
  79. * @throws Exception
  80. */
  81. public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
  82. Key k = toKey(key);
  83. Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
  84. cipher.init(Cipher.ENCRYPT_MODE, k, new SecureRandom());
  85. return cipher.doFinal(data);
  86. }
  87. /**
  88. * 解密
  89. * @param data 密文
  90. * @param key
  91. * @return 明文、原文
  92. * @throws Exception
  93. */
  94. public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
  95. Key k = toKey(key);
  96. Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
  97. cipher.init(Cipher.DECRYPT_MODE, k, new SecureRandom());
  98. return cipher.doFinal(data);
  99. }
  100. }

相关文章