87 lines
2.2 KiB
Java
87 lines
2.2 KiB
Java
|
|
package com.ule.demo;
|
||
|
|
|
||
|
|
import javax.crypto.Cipher;
|
||
|
|
import javax.crypto.spec.IvParameterSpec;
|
||
|
|
import javax.crypto.spec.SecretKeySpec;
|
||
|
|
|
||
|
|
import org.apache.commons.codec.binary.Base64;
|
||
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 邮乐报文 加解密工具类
|
||
|
|
*
|
||
|
|
* @author admin
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
public class AESUtils {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 数据加密
|
||
|
|
*
|
||
|
|
* @param requestData
|
||
|
|
* @param aesKey
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
public static String encryData(String requestData, String aesKey) throws Exception {
|
||
|
|
byte[] encryptedData = aesEncrypt(requestData, aesKey, aesKey);
|
||
|
|
return Base64.encodeBase64String(encryptedData);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 数据解密
|
||
|
|
*
|
||
|
|
* @param requestData
|
||
|
|
* @param aesKey
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
public static String decryptData(String encryptedData, String aesKey) throws Exception {
|
||
|
|
return aesDecrypt(encryptedData, aesKey, aesKey);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* aes加密
|
||
|
|
*
|
||
|
|
* @param decryptedData
|
||
|
|
* @param encryptKey
|
||
|
|
* @param ivKeys
|
||
|
|
* @return
|
||
|
|
* @throws Exception
|
||
|
|
*/
|
||
|
|
private static byte[] aesEncrypt(String decryptedData, String encryptKey, String ivKeys) throws Exception {
|
||
|
|
String PKCS5Padding = "AES/CBC/PKCS5Padding";
|
||
|
|
Cipher cipher = Cipher.getInstance(PKCS5Padding);
|
||
|
|
byte[] raw = ivKeys.getBytes("utf-8");
|
||
|
|
IvParameterSpec iv = new IvParameterSpec(raw);
|
||
|
|
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes("utf-8"), "AES"), iv);
|
||
|
|
byte[] encryptedData = cipher.doFinal(decryptedData.getBytes("UTF-8"));
|
||
|
|
return encryptedData;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* aes解密
|
||
|
|
*
|
||
|
|
* @param encryptedData
|
||
|
|
* @param encryptKey
|
||
|
|
* @param ivKeys
|
||
|
|
* @return
|
||
|
|
* @throws Exception
|
||
|
|
*/
|
||
|
|
private static String aesDecrypt(String encryptedData, String encryptKey, String ivKeys) throws Exception {
|
||
|
|
String PKCS5Padding = "AES/CBC/PKCS5Padding";
|
||
|
|
Cipher cipher = Cipher.getInstance(PKCS5Padding);
|
||
|
|
byte[] raw = ivKeys.getBytes("utf-8");
|
||
|
|
IvParameterSpec iv = new IvParameterSpec(raw);
|
||
|
|
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(encryptKey.getBytes("utf-8"), "AES"), iv);
|
||
|
|
byte[] decryptedData = cipher.doFinal(Base64.decodeBase64(encryptedData));
|
||
|
|
return new String(decryptedData, "UTF-8");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* MD5摘要算法
|
||
|
|
*
|
||
|
|
* @throws @throws
|
||
|
|
*/
|
||
|
|
public static String MD5(String key) {
|
||
|
|
return DigestUtils.md5Hex(key);
|
||
|
|
}
|
||
|
|
}
|