Des3Util.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.example.utils;
  2. /**
  3. * @category Des3
  4. *
  5. * @author DSJ
  6. * @version 1.0
  7. * @since 1.0
  8. */
  9. import javax.crypto.Cipher;
  10. import javax.crypto.SecretKeyFactory;
  11. import javax.crypto.spec.DESedeKeySpec;
  12. import javax.crypto.spec.IvParameterSpec;
  13. import java.security.Key;
  14. public class Des3Util {
  15. private final static byte[] SKEYIV = { 'c', '@', 0, '%', 'I', '$', '&', 'm' };
  16. /**
  17. * ECB加密,不要IV
  18. * @param key 密钥
  19. * @param data 明文
  20. * @return Base64编码的密文
  21. * @throws Exception
  22. */
  23. public static byte[] des3EncodeECB(byte[] key, byte[] data)
  24. throws Exception {
  25. Key deskey = null;
  26. DESedeKeySpec spec = new DESedeKeySpec(key);
  27. SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
  28. deskey = keyfactory.generateSecret(spec);
  29. Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
  30. cipher.init(Cipher.ENCRYPT_MODE, deskey);
  31. byte[] bOut = cipher.doFinal(data);
  32. return bOut;
  33. }
  34. /**
  35. * ECB解密,不要IV
  36. * @param key 密钥
  37. * @param data Base64编码的密文
  38. * @return 明文
  39. * @throws Exception
  40. */
  41. public static byte[] ees3DecodeECB(byte[] key, byte[] data)
  42. throws Exception {
  43. Key deskey = null;
  44. DESedeKeySpec spec = new DESedeKeySpec(key);
  45. SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
  46. deskey = keyfactory.generateSecret(spec);
  47. Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
  48. cipher.init(Cipher.DECRYPT_MODE, deskey);
  49. byte[] bOut = cipher.doFinal(data);
  50. return bOut;
  51. }
  52. /**
  53. * CBC加密
  54. * @param key 密钥
  55. * @param keyiv IV
  56. * @param data 明文
  57. * @return Base64编码的密文
  58. * @throws Exception
  59. */
  60. public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
  61. throws Exception {
  62. Key deskey = null;
  63. DESedeKeySpec spec = new DESedeKeySpec(key);
  64. SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
  65. deskey = keyfactory.generateSecret(spec);
  66. Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
  67. IvParameterSpec ips = new IvParameterSpec(keyiv);
  68. cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
  69. byte[] bOut = cipher.doFinal(data);
  70. return bOut;
  71. }
  72. /**
  73. * CBC解密
  74. * @param key 密钥
  75. * @param keyiv IV
  76. * @param data Base64编码的密文
  77. * @return 明文
  78. * @throws Exception
  79. */
  80. public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data)
  81. throws Exception {
  82. Key deskey = null;
  83. DESedeKeySpec spec = new DESedeKeySpec(key);
  84. SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
  85. deskey = keyfactory.generateSecret(spec);
  86. Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
  87. IvParameterSpec ips = new IvParameterSpec(keyiv);
  88. cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
  89. byte[] bOut = cipher.doFinal(data);
  90. return bOut;
  91. }
  92. /**
  93. * CBC 加密
  94. * @param key
  95. * @param data
  96. * @return
  97. * @throws Exception
  98. *
  99. */
  100. public static String des3EncodeCBCString(String key, String data) throws Exception{
  101. byte[] bBuf = des3EncodeCBC(key.getBytes("UTF-8"), SKEYIV, data.getBytes("UTF-8") );
  102. return Hex.parseByte2HexStr(bBuf);
  103. }
  104. /**
  105. * CBC 解密
  106. * @param key
  107. * @param data
  108. * @return
  109. * @throws Exception
  110. *
  111. */
  112. public static String des3DecodeCBCString(String key, String data) throws Exception{
  113. byte[] bdata = Hex.parseHexStr2Byte(data);
  114. byte[] bBuf = des3DecodeCBC(key.getBytes("UTF-8"), SKEYIV, bdata );
  115. return new String(bBuf, "UTF-8"); //Hex.encodeHexStr(bBuf);
  116. }
  117. }