AES加解密算法,使用Base64做转码以及辅助加密:
package
com.wintv.common;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/** *****************************************************************************
* AES加解密算法
*
* @author arix04
*
*/
public class AES {
// 加密
public static String Encrypt(String sSrc, String sKey) 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 = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, " AES " );
Cipher cipher = Cipher.getInstance( " AES/CBC/PKCS5Padding " ); // "算法/模式/补码方式"
IvParameterSpec iv = new IvParameterSpec( " 0102030405060708 " .getBytes()); // 使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte [] encrypted = cipher.doFinal(sSrc.getBytes());
return new BASE64Encoder().encode(encrypted); // 此处使用BASE64做转码功能,同时能起到2次加密的作用。
}
// 解密
public static String Decrypt(String sSrc, String sKey) 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位 " );
&n
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/** *****************************************************************************
* AES加解密算法
*
* @author arix04
*
*/
public class AES {
// 加密
public static String Encrypt(String sSrc, String sKey) 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 = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, " AES " );
Cipher cipher = Cipher.getInstance( " AES/CBC/PKCS5Padding " ); // "算法/模式/补码方式"
IvParameterSpec iv = new IvParameterSpec( " 0102030405060708 " .getBytes()); // 使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte [] encrypted = cipher.doFinal(sSrc.getBytes());
return new BASE64Encoder().encode(encrypted); // 此处使用BASE64做转码功能,同时能起到2次加密的作用。
}
// 解密
public static String Decrypt(String sSrc, String sKey) 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位 " );
&n

本文介绍如何运用AES算法进行数据加密和解密,并结合Base64编码进行辅助操作,详细阐述了byte到hex的转换过程。
最低0.47元/天 解锁文章
1139

被折叠的 条评论
为什么被折叠?



