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. * @param key
  77. * @param data
  78. * @return
  79. * @throws Exception
  80. */
  81. public static String decodeValue(String key, String data) {
  82. byte[] datas;
  83. String value = null;
  84. try {
  85. if (System.getProperty("os.name") != null
  86. && (System.getProperty("os.name").equalsIgnoreCase("sunos") || System
  87. .getProperty("os.name").equalsIgnoreCase("linux"))) {
  88. datas = decode(key, Base64.decode(data));
  89. } else {
  90. datas = decode(key, Base64.decode(data));
  91. }
  92.  
  93. value = new String(datas);
  94. } catch (Exception e) {
  95. value = "";
  96. }
  97. return value;
  98. }
  99.  
  100. }

 

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. * @param key
  70. * @param data
  71. * @return
  72. * @throws Exception
  73. */
  74. public static String decodeValue(String key, String data) {
  75. byte[] datas;
  76. String value = null;
  77. try {
  78. if (System.getProperty("os.name") != null
  79. && (System.getProperty("os.name").equalsIgnoreCase("sunos") || System
  80. .getProperty("os.name").equalsIgnoreCase("linux"))) {
  81. datas = decode(key, Base64.decode(data, 3));
  82. } else {
  83. datas = decode(key, Base64.decode(data, 3));
  84. }
  85.  
  86. value = new String(datas);
  87. } catch (Exception e) {
  88. value = "";
  89. }
  90. return value;
  91. }
  92. }
测试(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();
}

 

参考:

Android平台和java平台 DES加密解密互通程序及其不能互通的原因

http://hi.baidu.com/hpyfei/item/ca990cff5ce7b217ff358263 

Android与PHP互通的DES加密解密

http://www.linuxidc.com/Linux/2011-08/41866.htm

Android客户端与服务器端通过DES加密认证