JavaAES256加密解密实现
不说别的,直接上代码:
import it.sauronsoftware.base64.Base64;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
pto.Cipher;
pto.KeyGenerator;
pto.SecretKey;
java源代码加密pto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
* java实现AES256加密解密
* 依赖说明:
* bcprov-jdk15-133.jar:PKCS7Padding
* javabase64-1.3.1.jar:base64
* local_policy.jar 和 US_export_policy.jar需添加到%JAVE_HOME%\jre\lib\security中(lib中版本适合jdk1.7)
*/
public class AES256 {
public static byte[] encrypt(String content, String password) {
try {
//"AES":请求的密钥算法的标准名称
KeyGenerator kgen = Instance("AES");
//256:密钥⽣成参数;securerandom:密钥⽣成器的随机源
SecureRandom securerandom = new SecureRandom(tohash256Deal(password));
kgen.init(256, securerandom);
//⽣成秘密(对称)密钥
SecretKey secretKey = ateKey();
//返回基本编码格式的密钥
byte[] enCodeFormat = Encoded();
//根据给定的字节数组构造⼀个密钥。enCodeFormat:密钥内容;"AES":与给定的密钥内容相关联的密钥算法的名称  SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
//将提供程序添加到下⼀个可⽤位置
Security.addProvider(new BouncyCastleProvider());
//创建⼀个实现指定转换的 Cipher对象,该转换由指定的提供程序提供。
//"AES/ECB/PKCS7Padding":转换的名称;"BC":提供程序的名称
Cipher cipher = Instance("AES/ECB/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] byteContent = Bytes("utf-8");
byte[] cryptograph = cipher.doFinal(byteContent);
de(cryptograph);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(byte[] cryptograph, String password) {
try {
KeyGenerator kgen = Instance("AES");
SecureRandom securerandom = new SecureRandom(tohash256Deal(password));
kgen.init(256, securerandom);
SecretKey secretKey = ateKey();
byte[] enCodeFormat = Encoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Instance("AES/ECB/PKCS7Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] content = cipher.doFinal(Base64.decode(cryptograph));
return new String(content);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = HexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.UpperCase());
}
String();
}
/*private static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}*/
private static byte[] tohash256Deal(String datastr) {
try {
MessageDigest Instance("SHA-256");
digester.Bytes());
byte[] hex=digester.digest();
return hex;
} catch (NoSuchAlgorithmException e) {
throw new Message());
}
}
public static void main(String[] args) {
String content = "0f607264fc6318a92b9e13c65db7cd3c";
String password = "zsyy";
System.out.println("明⽂:" + content);
System.out.println("key:" + password);
byte[] encryptResult = pt(content, password);
System.out.println("密⽂:" + AES256.parseByte2HexStr(encryptResult));
String decryptResult = AES256.decrypt(encryptResult, password);
System.out.println("解密:" + decryptResult);
}
}
运⾏截图:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。