歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android平台下的AES加密和Java平台下的運行結果不同的解決辦法

Android平台下的AES加密和Java平台下的運行結果不同的解決辦法

日期:2017/3/1 10:23:48   编辑:Linux編程

實現Android和Java互相加解密

完美支持中文

跨平台這種實現

還是一個原則

不要對參數采用默認實現

否則難以互通

核心函數如下,Android和java均如此

  1. public static final String VIPARA = "0102030405060708";
  2. public static final String bm = "GBK";

  1. public static String encrypt(String dataPassword, String cleartext)
  2. throws Exception {
  3. IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
  4. SecretKeySpec key = new SecretKeySpec(dataPassword.getBytes(), "AES");
  5. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  6. cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
  7. byte[] encryptedData = cipher.doFinal(cleartext.getBytes(bm));
  8. return Base64.encode(encryptedData);
  9. }
  10. public static String decrypt(String dataPassword, String encrypted)
  11. throws Exception {
  12. byte[] byteMi = Base64.decode(encrypted);
  13. IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
  14. SecretKeySpec key = new SecretKeySpec(dataPassword.getBytes(), "AES");
  15. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  16. cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
  17. byte[] decryptedData = cipher.doFinal(byteMi);
  18. return new String(decryptedData,bm);
  19. }

Base64從網上找的工具類

  1. package com.bao;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. public class Base64 {
  6. private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
  7. /**
  8. * data[]進行編碼
  9. * @param data
  10. * @return
  11. */
  12. public static String encode(byte[] data) {
  13. int start = 0;
  14. int len = data.length;
  15. StringBuffer buf = new StringBuffer(data.length * 3 / 2);
  16. int end = len - 3;
  17. int i = start;
  18. int n = 0;
  19. while (i <= end) {
  20. int d = ((((int) data[i]) & 0x0ff) << 16)
  21. | ((((int) data[i + 1]) & 0x0ff) << 8)
  22. | (((int) data[i + 2]) & 0x0ff);
  23. buf.append(legalChars[(d >> 18) & 63]);
  24. buf.append(legalChars[(d >> 12) & 63]);
  25. buf.append(legalChars[(d >> 6) & 63]);
  26. buf.append(legalChars[d & 63]);
  27. i += 3;
  28. if (n++ >= 14) {
  29. n = 0;
  30. buf.append(" ");
  31. }
  32. }
  33. if (i == start + len - 2) {
  34. int d = ((((int) data[i]) & 0x0ff) << 16)
  35. | ((((int) data[i + 1]) & 255) << 8);
  36. buf.append(legalChars[(d >> 18) & 63]);
  37. buf.append(legalChars[(d >> 12) & 63]);
  38. buf.append(legalChars[(d >> 6) & 63]);
  39. buf.append("=");
  40. } else if (i == start + len - 1) {
  41. int d = (((int) data[i]) & 0x0ff) << 16;
  42. buf.append(legalChars[(d >> 18) & 63]);
  43. buf.append(legalChars[(d >> 12) & 63]);
  44. buf.append("==");
  45. }
  46. return buf.toString();
  47. }
  48. private static int decode(char c) {
  49. if (c >= 'A' && c <= 'Z')
  50. return ((int) c) - 65;
  51. else if (c >= 'a' && c <= 'z')
  52. return ((int) c) - 97 + 26;
  53. else if (c >= '0' && c <= '9')
  54. return ((int) c) - 48 + 26 + 26;
  55. else
  56. switch (c) {
  57. case '+':
  58. return 62;
  59. case '/':
  60. return 63;
  61. case '=':
  62. return 0;
  63. default:
  64. throw new RuntimeException("unexpected code: " + c);
  65. }
  66. }
  67. /**
  68. * Decodes the given Base64 encoded String to a new byte array. The byte
  69. * array holding the decoded data is returned.
  70. */
  71. public static byte[] decode(String s) {
  72. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  73. try {
  74. decode(s, bos);
  75. } catch (IOException e) {
  76. throw new RuntimeException();
  77. }
  78. byte[] decodedBytes = bos.toByteArray();
  79. try {
  80. bos.close();
  81. bos = null;
  82. } catch (IOException ex) {
  83. System.err.println("Error while decoding BASE64: " + ex.toString());
  84. }
  85. return decodedBytes;
  86. }
  87. private static void decode(String s, OutputStream os) throws IOException {
  88. int i = 0;
  89. int len = s.length();
  90. while (true) {
  91. while (i < len && s.charAt(i) <= ' ')
  92. i++;
  93. if (i == len)
  94. break;
  95. int tri = (decode(s.charAt(i)) << 18)
  96. + (decode(s.charAt(i + 1)) << 12)
  97. + (decode(s.charAt(i + 2)) << 6)
  98. + (decode(s.charAt(i + 3)));
  99. os.write((tri >> 16) & 255);
  100. if (s.charAt(i + 2) == '=')
  101. break;
  102. os.write((tri >> 8) & 255);
  103. if (s.charAt(i + 3) == '=')
  104. break;
  105. os.write(tri & 255);
  106. i += 4;
  107. }
  108. }
  109. }
Copyright © Linux教程網 All Rights Reserved