javaaes源码_JAVAAES加密算法实现代码package com.zhaochao.utill;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
pto.BadPaddingException;
pto.Cipher;
pto.IllegalBlockSizeException;
pto.KeyGenerator;
pto.NoSuchPaddingException;
pto.SecretKey;
pto.spec.SecretKeySpec;
public class AESUitl {
public static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = Instance("AES");
kgen.init(128, new Bytes()));
SecretKey secretKey = ateKey();
byte[] enCodeFormat = Encoded();
java源代码加密SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Instance("AES");// 创建密码器
byte[] byteContent = Bytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = Instance("AES");
kgen.init(128, new Bytes())); SecretKey secretKey = ateKey();
byte[] enCodeFormat = Encoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Instance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(content);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] rags) {
String content = "test2222";
String password = "12345678";
// 加密
System.out.println("加密前:" + content);
byte[] encryptResult = encrypt(content, password); System.out.println("加密后:"+new String(encryptResult)); // 解密
byte[] decryptResult = decrypt(encryptResult, password); System.out.println("解密后:" + new String(decryptResult)); }
}
2、输出结果
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论