在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

详解DES加密算法及在Java程序中的使用示例

2016-7-29 15:41| 发布者: zhangjf| 查看: 639| 评论: 0

摘要: DES加密算法 DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1976年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),随后在国际上广泛流传开来。 DES算法的入口参数 ...

DES加密算法
DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1976年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),随后在国际上广泛流传开来。
DES算法的入口参数有三个:Key、Data、Mode。其中Key为7个字节共56位,是DES算法的工作密钥;Data为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。
DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是56位,其算法主要分为两步:
1)初始置换
其功能是把输入的64位数据块按位重新组合,并把输出分为L0、R0两部分,每部分各长32位,其置换规则为将输入的第58位换到第一位,第50位换到第2位……依此类推,最后一位是原来的第7位。L0、R0则是换位输出后的两部分,L0是输出的左32位,R0是右32位,例:设置换前的输入值为D1D2D3……D64,则经过初始置换后的结果为:L0=D58D50……D8;R0=D57D49……D7。
其置换规则见下表:

  1. 58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,
  2. 62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,
  3. 57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,
  4. 61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7,
复制代码

2)逆置换
经过16次迭代运算后,得到L16、R16,将此作为输入,进行逆置换,逆置换正好是初始置换的逆运算,由此即得到密文输出。
此算法是对称加密算法体系中的代表,在计算机网络系统中广泛使用.

Java基本实现

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

Java三重DES实现:

  1. package com.stone.security;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.KeyGenerator;
  4. import javax.crypto.SecretKey;
  5. import javax.crypto.SecretKeyFactory;
  6. import javax.crypto.spec.DESedeKeySpec;
  7. import javax.crypto.spec.IvParameterSpec;
  8. /**
  9. * 三重加密 3DES也作 Triple DES,
  10. */
  11. public class TripleDES {
  12. // 算法名称
  13. public static final String KEY_ALGORITHM = "DESede";
  14. // 算法名称/加密模式/填充方式
  15. public static final String CIPHER_ALGORITHM_ECB = "DESede/ECB/PKCS5Padding";
  16. public static final String CIPHER_ALGORITHM_CBC = "DESede/CBC/PKCS5Padding";
  17. private KeyGenerator keyGen;
  18. private SecretKey secretKey;
  19. private SecretKey secretKey2;
  20. private Cipher cipher;
  21. private static byte[] encryptData;
  22. public static void main(String[] args) throws Exception {
  23. TripleDES tripleDES = new TripleDES("ECB");
  24. tripleDES.encrypt("sau8jzxlcvm,'123`98(*^&%^^JCB ZX>>A<S<}}{");
  25. System.out.println("加密后:" + new String(encryptData));
  26. System.out.println("解密后:"+ new String(tripleDES.decrypt(encryptData)));
  27. tripleDES = new TripleDES("CBC");
  28. tripleDES.encrypt2("sau8jzxlc DQV#><?|vm,'123`98(*^&%^^JCB ZX>>A<S<}}{");
  29. System.out.println("加密后:" + new String(encryptData));
  30. System.out.println("解密后:"+ new String(tripleDES.decrypt2(encryptData)));
  31. }
  32. public TripleDES(String mode) throws Exception {
  33. if ("ECB".equals(mode)) {
  34. // cipher = Cipher.getInstance(KEY_ALGORITHM);
  35. cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
  36. keyGen = KeyGenerator.getInstance(KEY_ALGORITHM);
  37. secretKey = keyGen.generateKey();
  38. } else if("CBC".equals(mode)) {
  39. cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
  40. keyGen = KeyGenerator.getInstance(KEY_ALGORITHM);
  41. DESedeKeySpec spec = new DESedeKeySpec(keyGen.generateKey().getEncoded());
  42. secretKey2 = SecretKeyFactory.getInstance(KEY_ALGORITHM).generateSecret(spec);
  43. }
  44. }
  45. /**
  46. * 加密
  47. * @param str
  48. * @return
  49. * @throws Exception
  50. */
  51. public byte[] encrypt(String str) throws Exception {
  52. cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  53. return encryptData = cipher.doFinal(str.getBytes());
  54. }
  55. /**
  56. * 解密
  57. * @param encrypt
  58. * @return
  59. * @throws Exception
  60. */
  61. public byte[] decrypt(byte[] encrypt) throws Exception {
  62. cipher.init(Cipher.DECRYPT_MODE, secretKey);
  63. return encryptData = cipher.doFinal(encrypt);
  64. }
  65. byte[] getIV() {
  66. return "administ".getBytes();
  67. }
  68. /**
  69. * 加密
  70. * @param str
  71. * @return
  72. * @throws Exception
  73. */
  74. public byte[] encrypt2(String str) throws Exception {
  75. cipher.init(Cipher.ENCRYPT_MODE, secretKey2, new IvParameterSpec(getIV()));
  76. return encryptData = cipher.doFinal(str.getBytes());
  77. }
  78. /**
  79. * 解密
  80. * @param encrypt
  81. * @return
  82. * @throws Exception
  83. */
  84. public byte[] decrypt2(byte[] encrypt) throws Exception {
  85. cipher.init(Cipher.DECRYPT_MODE, secretKey2, new IvParameterSpec(getIV()));
  86. return encryptData = cipher.doFinal(encrypt);
  87. }
  88. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 10:24

Copyright 2015-2025 djqfx

返回顶部