import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AESUtil {
private static final Logger logger = LoggerFactory.getLogger(AESUtil.class);
private static String ivParameter = "2563685754236500";
/**
* 加密
*
* @param sSrc
* @param sKey
* @return
*/
public static String encrypt(String sSrc, String sKey) {
if (StringUtils.isBlank(sKey)) {
return null;
}
sKey = MD5(sKey);
if (StringUtils.isBlank(sKey)) {
return null;
}
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return Base64.encodeBase64String(encrypted);
} catch (Exception e) {
logger.error("encrypt err", e);
return null;
}
}
/**
* 解密
*
* @param sSrc
* @param sKey
* @return
* @throws Exception
*/
public static String decrypt(String sSrc, String sKey) {
if (StringUtils.isBlank(sKey)) {
return null;
}
sKey = MD5(sKey);
if (StringUtils.isBlank(sKey)) {
return null;
}
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = Base64.decodeBase64(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
return new String(original, "utf-8");
} catch (Exception ex) {
logger.error("decrypt err", ex);
return null;
}
}
/**
* 16位MD5码
*
* @param sourceStr
* @return
*/
private static String MD5(String sourceStr) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sourceStr.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
result = buf.toString();
result = buf.toString().substring(8, 24);
} catch (NoSuchAlgorithmException e) {
logger.error("MD5 err", e);
}
return result;
}
/**
* encrypt32
*
* @param encryptStr
* @return String
*/
public static String MD5Long(String encryptStr) {
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
byte[] md5Bytes = md5.digest(encryptStr.getBytes());
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
encryptStr = hexValue.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
return encryptStr;
}
}
JAVA AES加解密工具类
最新推荐文章于 2024-02-27 00:15:00 发布