123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package videoalliance;
- import java.io.UnsupportedEncodingException;
- import javax.crypto.Cipher;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import org.apache.commons.lang.StringUtils;
- import com.chinacreator.common.exception.BusinessException;
- /**
- * AES加密工具类
- */
- public class AESUtil {
- private static final byte[] IV = new byte[16];
-
- /**
- * 二进制转变为16进制
- *
- * @param buf
- * @return
- */
- public static String parseByte2HexStr(byte[] buf) {
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < buf.length; i++) {
- String hex = Integer.toHexString(buf[i] & 0xFF);
- if (hex.length() == 1) {
- hex = '0' + hex;
- }
- sb.append(hex.toUpperCase());
- }
- return sb.toString();
- }
- /**
- * 将16进制转变为二进制
- *
- * @param hexStr
- * @return
- */
- public 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;
- }
- /**
- * AES解密
- *
- * @param content
- * 密文
- * @param keyWord
- * 密钥
- * @return
- * @throws BusinessException
- * @throws Exception
- */
- public static byte[] decrypt(byte[] content, String keyWord) throws BusinessException {
- if(StringUtils.isEmpty(keyWord)) {
- return null;
- }
- try {
- SecretKeySpec key = new SecretKeySpec(keyWord.getBytes(), "AES");
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV));
- byte[] result = cipher.doFinal(content);
- return result;
- } catch (Exception e) {
- throw new BusinessException("9010", "解密失败");
- }
- }
- /**
- * AES 解密
- *
- * @param content
- * 密文
- * @param keyWord
- * 密钥
- * @return
- * @throws UnsupportedEncodingException
- * @throws BusinessException
- */
- public static String decrypt(String content, String keyWord) throws UnsupportedEncodingException, BusinessException {
- if(StringUtils.isEmpty(content)) {
- return null;
- }
- byte[] contentBytes = parseHexStr2Byte(content);
- byte[] result = decrypt(contentBytes, keyWord);
- return new String(result, "UTF-8");
- }
- /**
- * AES 加密
- *
- * @param content
- * 内容
- * @param keyWord
- * 密钥
- * @return
- * @throws BusinessException
- */
- public static byte[] encrypt(byte[] content, String keyWord) throws BusinessException {
- if(StringUtils.isEmpty(keyWord)) {
- return null;
- }
- try {
- SecretKeySpec key = new SecretKeySpec(keyWord.getBytes(), "AES");
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV));
- byte[] encryptedData = cipher.doFinal(content);
- return encryptedData;
- } catch (Exception e) {
- throw new BusinessException("9010", "加密失败");
- }
- }
- /**
- * AES 加密
- *
- * @param content
- * 内容
- * @param keyWord
- * 密钥
- * @return
- * @throws UnsupportedEncodingException
- * @throws BusinessException
- */
- public static String encrypt(String content, String keyWord) throws UnsupportedEncodingException, BusinessException {
- if(StringUtils.isEmpty(content)) {
- return null;
- }
- byte[] result = encrypt(content.getBytes("UTF-8"), keyWord);
- return parseByte2HexStr(result);
- }
-
- public static void main(String[] args) throws UnsupportedEncodingException, BusinessException {
- System.out.println(decrypt("7EED1F816FEFB5EABEA99967BBCC2274".toLowerCase(), "1234567812345678"));
- }
- }
|