Android平台下的AES加密和Java平台下的运行结果不同的解决办法


实现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.   
  9.     return Base64.encode(encryptedData);  
  10. }  
  11.   
  12. public static String decrypt(String dataPassword, String encrypted)  
  13.         throws Exception {  
  14.     byte[] byteMi = Base64.decode(encrypted);  
  15.     IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());  
  16.     SecretKeySpec key = new SecretKeySpec(dataPassword.getBytes(), "AES");  
  17.     Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");  
  18.     cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);  
  19.     byte[] decryptedData = cipher.doFinal(byteMi);  
  20.   
  21.     return new String(decryptedData,bm);  
  22. }  

Base64从网上找的工具类

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

相关内容