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. public static String encode(String key, String data) throws Exception {
  11. return encode(key, data.getBytes());
  12. }
  13. public static String encode(String key, byte[] data) throws Exception {
  14. try {
  15. byte[] ivbyte = { 1, 2, 3, 4, 5, 6, 7, 8 };
  16. DESKeySpec dks = new DESKeySpec(key.getBytes());
  17. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  18. // key的长度不能够小于8位字节
  19. Key secretKey = keyFactory.generateSecret(dks);
  20. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
  21. IvParameterSpec iv = new IvParameterSpec(ivbyte);
  22. AlgorithmParameterSpec paramSpec = iv;
  23. cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
  24. byte[] bytes = cipher.doFinal(data);
  25. return Base64.encode(bytes);
  26. } catch (Exception e) {
  27. throw new Exception(e);
  28. }
  29. }
  30. public static byte[] decode(String key, byte[] data) throws Exception {
  31. try {
  32. DESKeySpec dks = new DESKeySpec(key.getBytes());
  33. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  34. // key的长度不能够小于8位字节
  35. Key secretKey = keyFactory.generateSecret(dks);
  36. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
  37. IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
  38. AlgorithmParameterSpec paramSpec = iv;
  39. cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
  40. return cipher.doFinal(data);
  41. } catch (Exception e) {
  42. throw new Exception(e);
  43. }
  44. }
  45. public static String decodeValue(String key, String data) {
  46. byte[] datas;
  47. String value = null;
  48. try {
  49. if (System.getProperty("os.name") != null
  50. && (System.getProperty("os.name").equalsIgnoreCase("sunos") || System
  51. .getProperty("os.name").equalsIgnoreCase("linux"))) {
  52. datas = decode(key, Base64.decode(data));
  53. } else {
  54. datas = decode(key, Base64.decode(data));
  55. }
  56. value = new String(datas);
  57. } catch (Exception e) {
  58. value = "";
  59. }
  60. return value;
  61. }
  62. }
复制代码
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. public static String encode(String key, String data) throws Exception {
  8. return encode(key, data.getBytes());
  9. }
  10. public static String encode(String key, byte[] data) throws Exception {
  11. try {
  12. DESKeySpec dks = new DESKeySpec(key.getBytes());
  13. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  14. // key的长度不能够小于8位字节
  15. Key secretKey = keyFactory.generateSecret(dks);
  16. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
  17. cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  18. byte[] bytes = cipher.doFinal(data);
  19. return Base64.encodeToString(bytes, 3);
  20. } catch (Exception e) {
  21. throw new Exception(e);
  22. }
  23. }
  24. public static byte[] decode(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.DECRYPT_MODE, secretKey);
  32. return cipher.doFinal(data);
  33. } catch (Exception e) {
  34. throw new Exception(e);
  35. }
  36. }
  37. public static String decodeValue(String key, String data) {
  38. byte[] datas;
  39. String value = null;
  40. try {
  41. if (System.getProperty("os.name") != null
  42. && (System.getProperty("os.name").equalsIgnoreCase("sunos") || System
  43. .getProperty("os.name").equalsIgnoreCase("linux"))) {
  44. datas = decode(key, Base64.decode(data, 3));
  45. } else {
  46. datas = decode(key, Base64.decode(data, 3));
  47. }
  48. value = new String(datas);
  49. } catch (Exception e) {
  50. value = "";
  51. }
  52. return value;
  53. }
  54. }
  55. 测试(CBC模式的测试和ECB的一样):
  56. try {
  57. //待加密内容url
  58. String str = "";
  59. String pwdKey = "moshapp";
  60. String pwdKeyMD5 = MD5Util.encode(pwdKey);
  61. System.out.println("pwdKeyMD5:" + pwdKeyMD5);
  62. String encodeStr = DesEcbUtil.encode(pwdKeyMD5, str);
  63. String decodeStr = DesEcbUtil.decodeValue(pwdKeyMD5, encodeStr);
  64. System.out.println("加密前的字符串:" + str);
  65. System.out.println("加密后的字符串:" + encodeStr);
  66. System.out.println("解密后的字符串:" + decodeStr);
  67. // URL转义
  68. final String encodedURL = URLEncoder.encode(encodeStr, "UTF-8");
  69. // URL urlStr = new URL(encodedURL);
  70. System.out.println("转义后的字符串:" + encodedURL);
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }

你可能感兴趣的:(开发知识点小汇编)