知识点

相关文章

更多

最近更新

更多

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

2019-03-28 13:48|来源: 网络

实现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. }  

相关问答

更多
  • java开发在linux平台下没有什么问题, 国外大部分开发都是在mac os或者是linux平台下开发的, 因为java是跨平台所以在任何平台都可以开发, 也可以运行。
  • Linux的程序图标好像是单独配置吧,
  • 可以通过ODBC与MDB文件连接,下面的文章有LINUX下的ODBC安装、配置、以及C语言调用方面的资料: http://www-128.ibm.com/developerworks/cn/linux/database/odbc/index.html
  • Linux是一种自由和开放源代码的类UNIX操作系统。该操作系统的内核由林纳斯·托瓦兹在1991年10月5日首次发布。,在加上用户空间的应用程序之后,成为Linux操作系统。Linux也是自由软件和开放源代码软件发展中最著名的例子。只要遵循GNU通用公共许可证,任何个人和机构都可以自由地使用Linux的所有底层源代码,也可以自由地修改和再发布。大多数Linux系统还包括像提供GUI界面的X Window之类的程序。除了一部分专家之外,大多数人都是直接使用Linux发布版,而不是自己选择每一样组件或自行设置。 ...
  • Oracle公司宣称在Linux下安装Oracle9i数据库至少要有512MB的内存和至少1GB或者两倍内存大小的交换空间,对于系统内存大于2GB 的服务器,交换空间可以介于2GB—4GB之间。   如果是为了在一台仅有256M内存的普通PC机上试用Oracle9i,在分配了1GB左右的交换空间的情况下,也可以正常运行Oracle数据库。   要检查内存空间,登录进入Linux,在命令行方式(bash环境)下执行如下命令: grep MemTotal /proc/meminfo   要检查交换空间,在命令行 ...
  • 建议你照一本书看看。给你个连接吧。这个题目很简单的。http://linux.vbird.org/这上面有很多的资料,关于建站的。
  • 做安卓开发软件环境:Eclipse+ADT 硬件环境:电脑 服务器:单机的app不需要,需要的网上租 服务器的软件环境和电脑网站一样:服务器tomcat,JBoss,WebSphere,WebLogic,Resin。数据库:MySQL,Oracle,SqlServer 手机端:SQLite
  • 检查你的Qt安装的时候是否选择了bluez的支持。 如果你用的Linux系统默认Qt很可能没有安装,针对你的Linux版本,上网找找具体怎么添加bluez模块的支持。 Qt蓝牙支持安装:http://doc.qt.nokia.com/qtextended4.4/bluetooth.html 大概也就是自己编译的时候使用-bluetooth选项等。 Qt的蓝牙开发教程:http://doc.qt.nokia.com/qtextended4.4/bluetooth-bluetoothservice.html 我 ...
  • 显卡安装: 一、下载驱动程序 首先要找到显卡for Linux的驱动程序。现在绝大多数的3D显卡都已有了for Linux的驱动程序,可到各显卡厂商的主页或Linux的相关站点上去寻找。在安装显卡时,服务器根据显卡的情况来加载不同的显示模块,如果显示模块加载不正确,显卡就不能正常显示。 二、装载磁盘驱动器 Linux需要装载磁盘驱动器才能读取文件。启动Linux后,在字符界面下输入“mount -t vfat /dev/hda1 /mnt/winc”命令,将C盘装载到Linux下。需要注意的是,如果下载的是 ...