android平台下的aes加密和java平台…

实现Android和java互相加解密

 

完美支持中文

 

跨平台这种实现

还是一个原则

不要对参数采用默认实现

否则难以互通

 

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

 

 

[java]  view plain copy
  1. public static final String VIPARA "0102030405060708" 
  2. public static final String bm "GBK" 

[java]  view plain copy
  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从网上找的工具类

 

[java]  view plain copy
  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.         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.   
  17.             int end len 3 
  18.             int start;  
  19.             int 0 
  20.   
  21.             while (i <= end)  
  22.                 int ((((intdata[i]) 0x0ff<< 16 
  23.                         ((((intdata[i 1]) 0x0ff<< 8 
  24.                         (((intdata[i 2]) 0x0ff);  
  25.   
  26.                 buf.append(legalChars[(d >> 1863]);  
  27.                 buf.append(legalChars[(d >> 1263]);  
  28.                 buf.append(legalChars[(d >> 663]);  
  29.                 buf.append(legalChars[d 63]);  
  30.   
  31.                 += 3 
  32.   
  33.                 if (n++ >= 14 
  34.                     0 
  35.                     buf.append(");  
  36.                  
  37.              
  38.   
  39.             if (i == start len 2 
  40.                 int ((((intdata[i]) 0x0ff<< 16 
  41.                         ((((intdata[i 1]) 255<< 8);  
  42.   
  43.                 buf.append(legalChars[(d >> 1863]);  
  44.                 buf.append(legalChars[(d >> 1263]);  
  45.                 buf.append(legalChars[(d >> 663]);  
  46.                 buf.append("=");  
  47.             else if (i == start len 1 
  48.                 int (((intdata[i]) 0x0ff<< 16 
  49.   
  50.                 buf.append(legalChars[(d >> 1863]);  
  51.                 buf.append(legalChars[(d >> 1263]);  
  52.                 buf.append("==");  
  53.              
  54.   
  55.             return buf.toString();  
  56.          
  57.   
  58.         private static int decode(char c)  
  59.             if (c >= 'A' && <= 'Z' 
  60.                 return ((intc) 65 
  61.             else if (c >= 'a' && <= 'z' 
  62.                 return ((intc) 97 26 
  63.             else if (c >= '0' && <= '9' 
  64.                 return ((intc) 48 26 26 
  65.             else  
  66.                 switch (c)  
  67.                 case '+' 
  68.                     return 62 
  69.                 case '/' 
  70.                     return 63 
  71.                 case '=' 
  72.                     return 0 
  73.                 default 
  74.                     throw new RuntimeException("unexpected code: " c);  
  75.                  
  76.          
  77.   
  78.           
  79.   
  80.         public static byte[] decode(String s)  
  81.   
  82.             ByteArrayOutputStream bos new ByteArrayOutputStream();  
  83.             try  
  84.                 decode(s, bos);  
  85.             catch (IOException e)  
  86.                 throw new RuntimeException();  
  87.              
  88.             byte[] decodedBytes bos.toByteArray();  
  89.             try  
  90.                 bos.close();  
  91.                 bos null 
  92.             catch (IOException ex)  
  93.                 System.err.println("Error while decoding BASE64: " ex.toString());  
  94.              
  95.             return decodedBytes;  
  96.          
  97.   
  98.         private static void decode(String s, OutputStream os) throws IOException  
  99.             int 0 
  100.   
  101.             int len s.length();  
  102.   
  103.             while (true 
  104.                 while (i len && s.charAt(i) <= ' 
  105.                     i++;  
  106.   
  107.                 if (i == len)  
  108.                     break 
  109.   
  110.                 int tri (decode(s.charAt(i)) << 18 
  111.                         (decode(s.charAt(i 1)) << 12 
  112.                         (decode(s.charAt(i 2)) << 6 
  113.                         (decode(s.charAt(i 3)));  
  114.   
  115.                 os.write((tri >> 16255);  
  116.                 if (s.charAt(i 2== '=' 
  117.                     break 
  118.                 os.write((tri >> 8255);  
  119.                 if (s.charAt(i 3== '=' 
  120.                     break 
  121.                 os.write(tri 255);  
  122.   
  123.                 += 4 
  124.              
  125.          
  126.           
  127. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值