Android DES加密的CBC模式加密解密和ECB模式加密解密

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. public class DesCbcUtil {
  9. public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";

  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. * @param data 待加密字符串
  23. * @param key 加密私钥,长度不能够小于8位
  24. * @return 加密后的字节数组,一般结合Base64编码使用
  25. * @throws CryptException 异常
  26. */
  27. public static String encode(String key, byte[] data) throws Exception {
  28. try { 
  29. byte[] ivbyte = { 1, 2, 3, 4, 5, 6, 7, 8 }; 
  30. DESKeySpec dks = new DESKeySpec(key.getBytes());

  31. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  32. // key的长度不能够小于8位字节
  33. Key secretKey = keyFactory.generateSecret(dks);
  34. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
  35. IvParameterSpec iv = new IvParameterSpec(ivbyte);
  36. AlgorithmParameterSpec paramSpec = iv;
  37. cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);

  38. byte[] bytes = cipher.doFinal(data);

  39. return Base64.encode(bytes);
  40. } catch (Exception e) {
  41. throw new Exception(e);
  42. }
  43. }

  44. /**
  45. * 解密
  46. @param data 待解密字符串
  47. * @param key 解密私钥,长度不能够小于8位
  48. * @return 解密后的字节数组
  49. * @throws Exception 异常
  50. */
  51. public static byte[] decode(String key, byte[] data) throws Exception {
  52. try {
  53. DESKeySpec dks = new DESKeySpec(key.getBytes());
  54. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  55. // key的长度不能够小于8位字节
  56. Key secretKey = keyFactory.generateSecret(dks);
  57. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
  58. IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
  59. AlgorithmParameterSpec paramSpec = iv;
  60. cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
  61. return cipher.doFinal(data);
  62. } catch (Exception e) {
  63. throw new Exception(e);
  64. }
  65. }

  66. /**
  67. * 获取编码后的值
  68. * @param key
  69. * @param data
  70. * @return
  71. * @throws Exception
  72. */
  73. public static String decodeValue(String key, String data) {
  74. byte[] datas;
  75. String value = null;
  76. try {
  77. if (System.getProperty("os.name") != null
  78. && (System.getProperty("os.name").equalsIgnoreCase("sunos") || System
  79. .getProperty("os.name").equalsIgnoreCase("linux"))) {
  80. datas = decode(key, Base64.decode(data));
  81. } else {
  82. datas = decode(key, Base64.decode(data));
  83. }

  84. value = new String(datas);
  85. } catch (Exception e) {
  86. value = "";
  87. }
  88. return value;
  89. }

  90. }

 

ECB模式加密:

  1. import javax.crypto.Cipher;
  2. import javax.crypto.SecretKeyFactory;
  3. import javax.crypto.spec.DESKeySpec;
  4. import android.util.Base64;

  5. public class DesEcbUtil {

  6. public static final String ALGORITHM_DES = "DES/ECB/PKCS5Padding";

  7. /**
  8. * 加密
  9. * @param data 待加密字符串
  10. * @param key 加密私钥,长度不能够小于8位
  11. * @return 加密后的字节数组,一般结合Base64编码使用
  12. * @throws CryptException  异常
  13. */
  14. public static String encode(String key, String data) throws Exception {
  15. return encode(key, data.getBytes());
  16. }

  17. /**
  18. * 加密
  19. * @param data 待加密字符串
  20. * @param key 加密私钥,长度不能够小于8位
  21. * @return 加密后的字节数组,一般结合Base64编码使用
  22. * @throws CryptException 异常
  23. */
  24. public static String encode(String key, byte[] data) throws Exception {
  25. try {
  26. DESKeySpec dks = new DESKeySpec(key.getBytes());

  27. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  28. // key的长度不能够小于8位字节
  29. Key secretKey = keyFactory.generateSecret(dks);
  30. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
  31. cipher.init(Cipher.ENCRYPT_MODE, secretKey);

  32. byte[] bytes = cipher.doFinal(data);

  33. return Base64.encodeToString(bytes, 3);
  34. } catch (Exception e) {
  35. throw new Exception(e);
  36. }
  37. }

  38. /**
  39. * 解密
  40. * @param data 待解密字符串
  41. * @param key 解密私钥,长度不能够小于8位
  42. * @return 解密后的字节数组
  43. * @throws Exception 异常
  44. */
  45. public static byte[] decode(String key, byte[] data) throws Exception {
  46. try {
  47. DESKeySpec dks = new DESKeySpec(key.getBytes());
  48. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  49. // key的长度不能够小于8位字节
  50. Key secretKey = keyFactory.generateSecret(dks);
  51. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
  52. cipher.init(Cipher.DECRYPT_MODE, secretKey);
  53. return cipher.doFinal(data);
  54. } catch (Exception e) {
  55. throw new Exception(e);
  56. }
  57. }

  58. /**
  59. * 获取编码后的值
  60. * @param key
  61. * @param data
  62. * @return
  63. * @throws Exception
  64. */
  65. public static String decodeValue(String key, String data) {
  66. byte[] datas;
  67. String value = null;
  68. try {
  69. if (System.getProperty("os.name") != null
  70. && (System.getProperty("os.name").equalsIgnoreCase("sunos") || System
  71. .getProperty("os.name").equalsIgnoreCase("linux"))) {
  72. datas = decode(key, Base64.decode(data, 3));
  73. } else {
  74. datas = decode(key, Base64.decode(data, 3));
  75. }

  76. value = new String(datas);
  77. } catch (Exception e) {
  78. value = "";
  79. }
  80. return value;
  81. }
  82. }
测试(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();
}

 

参考:

http://blog.csdn.net/randyjiawenjie/article/details/6617225

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

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

Android与PHP互通的DES加密解密

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

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


 

posted on 2013-08-30 19:13  you Richer  阅读(1041)  评论(0编辑  收藏  举报