public class AESHelper {
/** 算法/模式/填充 **/
private static final String CipherMode = "AES/ECB/PKCS5Padding";
/** 创建密钥 **/
private static SecretKeySpec createKey(String password) {
byte[] data = null;
if (password == null) {
password = "";
}
StringBuffer sb = new StringBuffer(32);
sb.append(password);
while (sb.length() < 32) {
sb.append("0");
}
if (sb.length() > 32) {
sb.setLength(32);
}
try {
data = sb.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new SecretKeySpec(data, "AES");
}
/** 加密字节数据 **/
public static byte[] encrypt(byte[] content, String password) {
try {
SecretKeySpec key = createKey(password);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** 加密(结果为16进制字符串) **/
public static String encrypt(String content, String password) {
byte[] data = null;
try {
data = content.getBytes("UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
data = encrypt(data, password);
String result = byte2hex(data);
return result;
}
/** 解密字节数组 **/
public static byte[] decrypt(byte[] content, String password) {
try {
SecretKeySpec key = createKey(password);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** 解密16进制的字符串为字符串 **/
public static String decrypt(String content, String password) {
byte[] data = null;
try {
data = hex2byte(content);
} catch (Exception e) {
e.printStackTrace();
}
data = decrypt(data, password);
if (data == null)
return null;
String result = null;
try {
result = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
/** 字节数组转成16进制字符串 **/
public static String byte2hex(byte[] b) { // 一个字节的数,
StringBuffer sb = new StringBuffer(b.length * 2);
String tmp = "";
for (int n = 0; n < b.length; n++) {
// 整数转成十六进制表示
tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
}
return sb.toString().toUpperCase(); // 转成大写
}
/** 将hex字符串转换成字节数组 **/
private static byte[] hex2byte(String inputString) {
if (inputString == null || inputString.length() < 2) {
return new byte[0];
}
inputString = inputString.toLowerCase();
int l = inputString.length() / 2;
byte[] result = new byte[l];
for (int i = 0; i < l; ++i) {
String tmp = inputString.substring(2 * i, 2 * i + 2);
result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
}
return result;
}
}
分享到:
相关推荐
C 语言实现aes加解密
根据AES加解密的原理,利用C语言编写了能实现文件加密功能的AES加解密代码。
lua aes 加解密
用C语言实现的AES加密解密算法,用C语言实现的AES加密解密算法
AES加解密源码,可供大家在动态库或源码中使用。
VB.NET实现AES加密解密代码,可直接使用
' 功能: AES加密解密 password<==>hwWpiegaWDeE8kTlb/5BQg== , 8705a989e81a583784f244e56ffe4142 ' [Str] 预备转换的byte ' [StrKey] 16位的密钥 ' [StrIv] 16位的偏移 ' [Encrypt] 加密(缺省值)还是解密 ' ...
基于c++builder 10.2 调用 两个delphi AES加解密源代码实现c++下的aes加解密,实现特别简单,不光支持字符串加解密,还支持数据流、文件加解密,特别方便。我的博客有详细使用方法说明。...
2.领域:AES加解密。 3.内容:AES加解密算法的matlab仿真。 S_box=Complete_Sbox(); M_data= { '00' '00' '00' '00'; '00' '00' '00' '00'; '00' '00' '00' '00'; '00' '00' '00' '26'; } Cipher_key={'2b' '...
本文实例为大家分享了python实现AES加密解密的具体代码,供大家参考,具体内容如下 (1)对于AES加密解密相关知识 (2)实现的功能就是输入0-16个字符,然后经过AES的加密解密最后可以得到原先的输入,运行的结果...
c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案
vb6.0下AES加解密模块,可修改后做软件注册等,本加密程序可以加密字符和加密文件,当然也可解密,对学习AES算法当然是挺有参考意义的。可在多种场合中都可应用.
AES加密解密轮转算法工具,最大支持256bit长度的密钥,明文支持HEX和字串。可供学习设计参考。
是标准的AES算法,支持在线AES加解密网站互解。 本源码从CSDN一位前辈的源码基础上做了更改,增加支持加密返回BASE64,更加完善。在此感谢原作者: http://download.csdn.net/download/qiaohaidong/9477264 需要注意...
MFC开发的 aes加解密与mac计算,输入的秘钥,明文,密文长度正确时,右边字体变绿色
1. 在深入理解AES加密/解密算法理论的基础上,设计一个AES加密/解密软件系统; 2. 完成CBC和ECB模式加密解密
java语言实现AES加解密
基于matlab实现AES加解密和基于AES的cmac
支持 CFB, OFB, CBC, ECB 模式,支持256字节以下任意长度数据加解密,也可以放开成任意长度数据加解密。 其中:AES.cpp 中为算法的实现代码;Test.cpp为测试程序
用Velirog语言实现AES加密解密,可在FPGA上实现