原因:

将加密/解密的seed 和 加密内容顺序放反.  decrypt(String seed, String encrypted)

附上AES解密/加密代码(android开发):

package com.carspeak.client.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; /**
* AES 加密解密
*
* @author huqiang
*
*/
public class AESUtil { public static String encrypt(String seed, String cleartext)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
} public static String decrypt(String seed, String encrypted)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
} private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
} private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(
new byte[cipher.getBlockSize()]));
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
} private static byte[] decrypt(byte[] raw, byte[] encrypted)
throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(
new byte[cipher.getBlockSize()]));
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
} private static String toHex(String txt) {
return toHex(txt.getBytes());
} private static String fromHex(String hex) {
return new String(toByte(hex));
} private static byte[] toByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
16).byteValue();
return result;
} private static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
} private final static String HEX = "0123456789ABCDEF"; private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
} }

aes 解密出现 java.lang.NumberFormatException: Invalid int: "ch"的更多相关文章

  1. Android Integer.parseInt java.lang.NumberFormatException: Invalid int解决方法

    解决方法: http获取的字符串minutes去空字符串处理minutes.replaceAll("\\D+","").replaceAll("\r& ...

  2. NumberFormatException: Invalid int类型不匹配异常——使用SQL数据库查询语句select * from blacknumber order by _id desc limit ?,20;出现

    异常:类型不匹配 05-06 08:12:38.151: E/AndroidRuntime(14904): java.lang.NumberFormatException: Invalid int: ...

  3. Java—恶心的java.lang.NumberFormatException解决

    项目中要把十六进制字符串转化为十进制, 用到了到了Integer.parseInt(str1.trim(), 16):这个是不是后抛出java.lang.NumberFormatException异常 ...

  4. LoadRunner ERROR: java.lang.NumberFormatException

    Loadrunner中使用lr_xml_get_values()获取服务端返回的字符串LcsId,LcsId为double,需要将该值转换为 int 后传入下一次请求中. 报错如下:Error is ...

  5. java.lang.NumberFormatException: For input string: "1608020001 " 错误

    错误: java.lang.NumberFormatException: For input string: "1608020001 "    at java.lang.Numbe ...

  6. Exception in thread “main” com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String

    String json="A valid json"; Job job = new Gson().fromJson(json, Job.class); Exception in t ...

  7. java.lang.RuntimeException: Invalid action class configuration that references an unknown class named [xxxAction]。

    java.lang.RuntimeException: Invalid action class configuration that references an unknown class name ...

  8. 页面提交错误,页面间参数传递java.lang.NumberFormatException: null

    多次出现这样的错误,在点击一个按钮触发提交整个页面的事件时,总是报错,不止一次出现这样的错误了. 出现这种问题的分析: 1 我们从这个问题的本身来看,java.lang.NumberFormatExc ...

  9. 【报错】java.lang.RuntimeException: Invalid action class configuration that references an unknown class named [xxxAction]

    java.lang.RuntimeException: Invalid action class configuration that references an unknown class name ...

随机推荐

  1. springmvc的一个小例子学习(一)

    个人觉得,学框架最好的 方法无外乎两个:一个是实践这个框架,真实的去用它,比如spring框架,先搭一个简单的spring项目,然后一步一步得去丰富它,从中学到spring框架的精髓和知识:另外一个就 ...

  2. ASP流程控制语句

    ASP流程控制语句 1.if...then语句 单行: if 条件 then 语句 多行: if 条件 then elseif 条件 then 语句 elseif 条件 then 语句 else en ...

  3. SharedPreferences保存用户登录信息

    UI界面:

  4. HTML之背景颜色的改变

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 【原创】Tomcat集群环境下对session进行外部缓存的方法(1)

    BJJC网改版, 计划将应用部署在tomcat集群上,集群的部署方案为Apache+Tomcat6,连接件为mod_jk,其中开启了session复制和粘性session.计划节点数为3个. 到这,或 ...

  6. 【Unity3D实战】摇摆直升机开发实战(一)

    [Unity3D实战]摇摆直升机开发实战(一) 1.点击[Assets],创建[Sprites]和[Resources]文件夹,然后将所需要的素材导入[Sprites]文件夹中. 2.找到[Sprit ...

  7. C++学习——类的继承

    公有继承(public).私有继承(private).保护继承(protected)是常用的三种继承方式. 1. 公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时, ...

  8. 已有数据表的Mysql字符编码修改

    Mysql字符集修改应该如何实现呢?下面就为您详细介绍已用数据表的Mysql字符集修改方法,希望对您学习Mysql字符集方面能有所启迪. 环境:在应用开始阶段没有正确的设置字符集,在运行一段时间以后才 ...

  9. GCD Block

    GCD (Grand Central Dispatch) 是Apple公司开发的一种技术,它旨在优化多核环境中的并发操作并取代传统多线程的编程模式. 在Mac OS X 10.6和IOS 4.0之后开 ...

  10. 火狐谷歌浏览器Json查看插件

    1.搜: Firefox的JSON插件 参考: Chrome/FireFox浏览器下处理JSON的插件_Bruce_新浪博客 JSONView :: Firefox 附加组件 但是后来去发现没用: 打 ...