Java加密算法Triple DES

x33g5p2x  于2021-12-25 转载在 其他  
字(2.7k)|赞(0)|评价(0)|浏览(401)
  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. * @author stone
  12. * @date 2014-03-10 02:14:37
  13. */
  14. public class TripleDES {
  15. // 算法名称
  16. public static final String KEY_ALGORITHM = "DESede";
  17. // 算法名称/加密模式/填充方式
  18. public static final String CIPHER_ALGORITHM_ECB = "DESede/ECB/PKCS5Padding";
  19. public static final String CIPHER_ALGORITHM_CBC = "DESede/CBC/PKCS5Padding";
  20. private KeyGenerator keyGen;
  21. private SecretKey secretKey;
  22. private SecretKey secretKey2;
  23. private Cipher cipher;
  24. private static byte[] encryptData;
  25. public static void main(String[] args) throws Exception {
  26. TripleDES tripleDES = new TripleDES("ECB");
  27. tripleDES.encrypt("sau8jzxlcvm,'123`98(*^&%^^JCB ZX>>A<S<}}{");
  28. System.out.println("加密后:" + new String(encryptData));
  29. System.out.println("解密后:"+ new String(tripleDES.decrypt(encryptData)));
  30. tripleDES = new TripleDES("CBC");
  31. tripleDES.encrypt2("sau8jzxlc DQV#><«|vm,'123`98(*^&%^^JCB ZX>>A<S<}}{");
  32. System.out.println("加密后:" + new String(encryptData));
  33. System.out.println("解密后:"+ new String(tripleDES.decrypt2(encryptData)));
  34. }
  35. public TripleDES(String mode) throws Exception {
  36. if ("ECB".equals(mode)) {
  37. // cipher = Cipher.getInstance(KEY_ALGORITHM);
  38. cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
  39. keyGen = KeyGenerator.getInstance(KEY_ALGORITHM);
  40. secretKey = keyGen.generateKey();
  41. } else if("CBC".equals(mode)) {
  42. cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
  43. keyGen = KeyGenerator.getInstance(KEY_ALGORITHM);
  44. DESedeKeySpec spec = new DESedeKeySpec(keyGen.generateKey().getEncoded());
  45. secretKey2 = SecretKeyFactory.getInstance(KEY_ALGORITHM).generateSecret(spec);
  46. }
  47. }
  48. /**
  49. * 加密
  50. * @param str
  51. * @return
  52. * @throws Exception
  53. */
  54. public byte[] encrypt(String str) throws Exception {
  55. cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  56. return encryptData = cipher.doFinal(str.getBytes());
  57. }
  58. /**
  59. * 解密
  60. * @param encrypt
  61. * @return
  62. * @throws Exception
  63. */
  64. public byte[] decrypt(byte[] encrypt) throws Exception {
  65. cipher.init(Cipher.DECRYPT_MODE, secretKey);
  66. return encryptData = cipher.doFinal(encrypt);
  67. }
  68. byte[] getIV() {
  69. return "administ".getBytes();
  70. }
  71. /**
  72. * 加密
  73. * @param str
  74. * @return
  75. * @throws Exception
  76. */
  77. public byte[] encrypt2(String str) throws Exception {
  78. cipher.init(Cipher.ENCRYPT_MODE, secretKey2, new IvParameterSpec(getIV()));
  79. return encryptData = cipher.doFinal(str.getBytes());
  80. }
  81. /**
  82. * 解密
  83. * @param encrypt
  84. * @return
  85. * @throws Exception
  86. */
  87. public byte[] decrypt2(byte[] encrypt) throws Exception {
  88. cipher.init(Cipher.DECRYPT_MODE, secretKey2, new IvParameterSpec(getIV()));
  89. return encryptData = cipher.doFinal(encrypt);
  90. }
  91. }

相关文章