AES加密,解密封装。以及解密异常解决
来源:互联网 发布:施耐庵子孙 哑巴 知乎 编辑:程序博客网 时间:2024/05/29 11:11
1.类的封装
public class Encryption {
private final static String HEX = "0123456789ABCDEF";
private static String seed = "zhou"; //加密,解密时需要的密码
/**
* aes加密时调用
* 密码
* @param cleartext 加密内容
* @return
* @throws Exception
*/
public static String encrypt(String cleartext)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
/**
* aes解密时调用
*
* @param
* @param encrypted
* @return
* @throws Exception
*/
public static String decrypt( 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 {
//创建AESkey的生产者
KeyGenerator kgen = KeyGenerator.getInstance("AES");
//加入第二个参数后解决了,解密时pad block corrupted的异常
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);
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);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}
public 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;
}
public 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 static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
}
2.参考链接
http://blog.csdn.net/wujxiaoz/article/details/9036981
http://stackoverflow.com/questions/13389870/android-4-2-broke-my-aes-encrypt-decrypt-code
- AES加密,解密封装。以及解密异常解决
- AES加密解密源码示例,以及解决:aes在tomcat解密中文乱码,在控制台解密中文不是乱码
- AES加密以及解密的方法
- 加密解密。(aes, java)
- AES加密解密类
- AES 加密/解密备忘
- java,AES加密,解密
- AES 加密解密 c#
- AES加密解密程序。
- AES加密和解密
- java AES加密解密
- java AES加密解密
- AES加密解密 应用
- AES 加密解密API
- php AES加密解密
- AES加密解密
- AES加密与解密
- java AES加密解密
- LibEvent中文帮助文档
- 开源分布式后台毫秒服务引擎全解析:引擎架构、RPC、灰度……
- POJ2531
- 2016最新淘宝客申请高佣金以及分析抓包详情
- java编程题练习
- AES加密,解密封装。以及解密异常解决
- Python中的正则表达式教程
- EF外键表/视图必须有外键否则被设置为可读的解决方案
- Spring自动扫描和管理bean
- KSVD算法原理简述
- C调试方法之--------Gprofile
- Java生成二维码
- Express cookie-parser
- tomcat的service.bat注册成服务不能调用系统证书,而手动启动start.bat却可以