DES加密共有四种模式:电子密码本模式(ECB)、加密分组链接模式(CBC)、加密反馈模式(CFB)和输出反馈模式(OFB)。
CBC模式加密:
1. import java.security.Key;
2. import java.security.spec.AlgorithmParameterSpec;
3. import javax.crypto.Cipher;
4. import javax.crypto.SecretKeyFactory;
5. import javax.crypto.spec.DESKeySpec;
6. import javax.crypto.spec.IvParameterSpec;
7. import com.sun.org.apache.xml.internal.security.utils.Base64;
8.
9. public class DesCbcUtil {
10. public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
11.
12. /**
13. * 加密
14. * @param data待加密字符串
15. * @param key加密私钥,长度不能够小于8位
16. * @return 加密后的字节数组,一般结合Base64编码使用
17. * @throws CryptException异常
18. */
19. public static String encode(String key, String data) throws Exception {
20. return encode(key, data.getBytes());
21. }
22.
23. /**
24. * 加密
25. * @param data待加密字符串
26. * @param key加密私钥,长度不能够小于8位
27. * @return 加密后的字节数组,一般结合Base64编码使用
28. * @throws CryptException异常
29. */
30. public static String encode(String key, byte[] data) throws Exception {
31. try {
32. byte[] ivbyte = { 1, 2, 3, 4, 5, 6, 7, 8 };
33. DESKeySpec dks = new DESKeySpec(key.getBytes());
34.
35. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
36. // key的长度不能够小于8位字节
37. Key secretKey = keyFactory.generateSecret(dks);
38. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
39. IvParameterSpec iv = new IvParameterSpec(ivbyte);
40. AlgorithmParameterSpec paramSpec = iv;
41. cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
42.
43. byte[] bytes = cipher.doFinal(data);
44.
45. return Base64.encode(bytes);
46. } catch (Exception e) {
47. throw new Exception(e);
48. }
49. }
50.
51. /**
52. * 解密
53. *@param data待解密字符串
54. * @param key解密私钥,长度不能够小于8位
55. * @return 解密后的字节数组
56. * @throws Exception异常
57. */
58. public static byte[] decode(String key, byte[] data) throws Exception {
59. try {
60. DESKeySpec dks = new DESKeySpec(key.getBytes());
61. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
62. // key的长度不能够小于8位字节
63. Key secretKey = keyFactory.generateSecret(dks);
64. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
65. IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
66. AlgorithmParameterSpec paramSpec = iv;
67. cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
68. return cipher.doFinal(data);
69. } catch (Exception e) {
70. throw new Exception(e);
71. }
72. }
73.
74. /**
75. * 获取编码后的值
76. *
77. * @param key
78. * @param data
79. * @return
80. * @throws Exception
81. */
82. public static String decodeValue(String key, String data) {
83. byte[] datas;
84. String value = null;
85. try {
86. if (System.getProperty("os.name") != null
87. && (System.getProperty("os.name").equalsIgnoreCase("sunos") || System
88. .getProperty("os.name").equalsIgnoreCase("linux"))) {
89. datas = decode(key, Base64.decode(data));
90. } else {
91. datas = decode(key, Base64.decode(data));
92. }
93.
94. value = new String(datas);
95. } catch (Exception e) {
96. value = "";
97. }
98. return value;
99. }
100.
101. }
ECB模式加密:
1. import javax.crypto.Cipher;
2. import javax.crypto.SecretKeyFactory;
3. import javax.crypto.spec.DESKeySpec;
4. import android.util.Base64;
5.
6. public class DesEcbUtil {
7.
8. public static final String ALGORITHM_DES = "DES/ECB/PKCS5Padding";
9.
10. /**
11. * 加密
12. * @param data待加密字符串
13. * @param key加密私钥,长度不能够小于8位
14. * @return 加密后的字节数组,一般结合Base64编码使用
15. * @throws CryptException异常
16. */
17. public static String encode(String key, String data) throws Exception {
18. return encode(key, data.getBytes());
19. }
20.
21. /**
22. * 加密
23. * @param data待加密字符串
24. * @param key加密私钥,长度不能够小于8位
25. * @return 加密后的字节数组,一般结合Base64编码使用
26. * @throws CryptException异常
27. */
28. public static String encode(String key, byte[] data) throws Exception {
29. try {
30. DESKeySpec dks = new DESKeySpec(key.getBytes());
31.
32. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
33. // key的长度不能够小于8位字节
34. Key secretKey = keyFactory.generateSecret(dks);
35. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
36. cipher.init(Cipher.ENCRYPT_MODE, secretKey);
37.
38. byte[] bytes = cipher.doFinal(data);
39.
40. return Base64.encodeToString(bytes, 3);
41. } catch (Exception e) {
42. throw new Exception(e);
43. }
44. }
45.
46. /**
47. * 解密
48. * @param data待解密字符串
49. * @param key解密私钥,长度不能够小于8位
50. * @return 解密后的字节数组
51. * @throws Exception异常
52. */
53. public static byte[] decode(String key, byte[] data) throws Exception {
54. try {
55. DESKeySpec dks = new DESKeySpec(key.getBytes());
56. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
57. // key的长度不能够小于8位字节
58. Key secretKey = keyFactory.generateSecret(dks);
59. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
60. cipher.init(Cipher.DECRYPT_MODE, secretKey);
61. return cipher.doFinal(data);
62. } catch (Exception e) {
63. throw new Exception(e);
64. }
65. }
66.
67. /**
68. * 获取编码后的值
69. *
70. * @param key
71. * @param data
72. * @return
73. * @throws Exception
74. */
75. public static String decodeValue(String key, String data) {
76. byte[] datas;
77. String value = null;
78. try {
79. if (System.getProperty("os.name") != null
80. && (System.getProperty("os.name").equalsIgnoreCase("sunos") || System
81. .getProperty("os.name").equalsIgnoreCase("linux"))) {
82. datas = decode(key, Base64.decode(data, 3));
83. } else {
84. datas = decode(key, Base64.decode(data, 3));
85. }
86.
87. value = new String(datas);
88. } catch (Exception e) {
89. value = "";
90. }
91. return value;
92. }
93. }
测试(CBC模式的测试和ECB的一样):
try {
//待加密内容url
String str = "";
String pwdKey = "moshapp";
String pwdKeyMD5 = MD5Util.encode(pwdKey);
System.out.println("pwdKeyMD5:" + pwdKeyMD5);
String encodeStr = DesEcbUtil.encode(pwdKeyMD5, str);
String decodeStr = DesEcbUtil.decodeValue(pwdKeyMD5, encodeStr);
System.out.println("加密前的字符串:" + str);
System.out.println("加密后的字符串:" + encodeStr);
System.out.println("解密后的字符串:" + decodeStr);
// URL转义
final String encodedURL = URLEncoder.encode(encodeStr, "UTF-8");
//
URL urlStr = new URL(encodedURL);
System.out.println("转义后的字符串:" + encodedURL);
} catch (Exception e) {
e.printStackTrace();
}