java AES 解密中文乱码的问题?

lei55022033 2009-09-02 11:24:44
public class AES {

private static final String KEY = "zyxwvutsr9876123";

// public static void main(String[] args) throws Exception {
// /*
// 加密用的Key
// 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定
// */
// String cKey = "1234567890abcDEF";
// //需要加密的字串
// String cSrc = "nihao";
// //加密
// long lStart = System.currentTimeMillis();
// String enString = AES.Encrypt(cSrc, cKey);
// System.out.println("加密后的字串是:" + enString);
// long lUseTime = System.currentTimeMillis() - lStart;
// System.out.println("加密耗时:" + lUseTime + "毫秒");
// //解密
// lStart = System.currentTimeMillis();
// String DeString = AES.Decrypt(enString, cKey);
// System.out.println("解密后的字串是:" + DeString);
// lUseTime = System.currentTimeMillis() - lStart;
// System.out.println("解密耗时:" + lUseTime + "毫秒");
// }
public String Decrypt(String sSrc) throws Exception {
try {
// //判断Key是否正确
// if (sKey == null) {
// System.out.print("Key为空null");
// return null;
// }
// //判断Key是否为16位
// if (sKey.length() != 16) {
// System.out.print("Key长度不是16位");
// return null;
// }
byte[] raw = KEY.getBytes("UTF8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = hex2byte(sSrc);
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
//判断Key是否正确
public String Encrypt(String sSrc) throws Exception {
// if (sKey == null) {
// System.out.print("Key为空null");
// return null;
// }
// //判断Key是否为16位
// if (sKey.length() != 16) {
// System.out.print("Key长度不是16位");
// return null;
// }
byte[] raw = KEY.getBytes("UTF8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes());
return byte2hex(encrypted).toLowerCase();
}
public byte[] hex2byte(String strhex) {
if (strhex == null) {
return null;
}
int l = strhex.length();
if (l % 2 == 1) {
return null;
}
byte[] b = new byte[l / 2];
for (int i = 0; i != l / 2; i++) {
b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2), 16);
}
return b;
}
public String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
}


如果是中文的话,解密就会是乱码?如何解决?
...全文
790 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
junsheng100 2011-10-31
  • 打赏
  • 举报
回复
楼主把解决的方案贴出来一下,大家参考参考
lei55022033 2009-09-02
  • 打赏
  • 举报
回复
我错了,不是算法的问题。
原文:https://github.com/yangchenjava/com.yangc.utils cache EhCacheUtils - 基于ehcache的工具类 LruCacheUtils - 基于LinkedHashMap实现LRU缓存的工具类 MemcachedUtils - 基于memcached的工具类 RedisUtils - 基于redis的工具类,与redis的集群配置无缝结合 db JdbcUtils - 操作jdbc的工具类 MongodbUtils - 操作mongodb的工具类 email EmailUtils - 邮件工具类,支持发送带附件的邮件 encryption AesUtils - 实现AES加密解密 Base64Utils - 实现Base64加密解密 Md5Utils - 获取字符串或文件的md5 excel ReadExcel2003 - 以model方式读2003版Excel(大数据) ReadExcel2007 - 以sax方式读2007版Excel(大数据) WriteExcel - 写Excel image CaptchaUtils - 生成验证码 ImageUtils - 图片压缩、截图 QRCodeUtils - 生成二维码、解析二维码 io SerializeUtils - 序列化、反序列化对象 ZipUtils - 压缩、解压文件 json JsonUtils - json格式转换 lang CharsetDetectorUtils - 获取文本文件编码格式 ChineseCalendar - 农历日历 ConvertUtils - 高低字节转换 DateUtils - 日期工具类 HtmlFilterUtils - 过滤html标签 JsoupUtils - 基于jsoup过滤html标签 MoneyUtils - 获取大写金额 NumberUtils - 数字工具类 PinyinUtils - 汉字转拼音 media MediaUtils - 基于ffmpeg,qtfaststart,yamdi的多媒体工具类 net AttachmentUtils - HTTP文件下载防止中文码 FastDFSUtils - 操作FastDFS的工具类 FtpUtils - 操作FTP的工具类(基于sun自家的包,jdk7以后不建议使用) FtpUtilsApache - 基于apache操作FTP的工具类 HttpUtils - 发送HTTP请求 IpUtils - 获取IP SFtpUtils - 操作SFTP的工具类 prop PropertiesUtils - 操作properties配置文件

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧