|
|
|
@ -1,17 +1,20 @@
|
|
|
|
|
package com.baiye.util;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
|
|
import cn.hutool.core.util.CharsetUtil;
|
|
|
|
|
import com.baiye.constant.AdPlatFormConstants;
|
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import sun.misc.BASE64Decoder;
|
|
|
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
|
import javax.crypto.KeyGenerator;
|
|
|
|
|
import javax.crypto.Mac;
|
|
|
|
|
import javax.crypto.SecretKey;
|
|
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -28,10 +31,16 @@ public class EncryptUtil {
|
|
|
|
|
public static final String HmacSHA1 = "HmacSHA1";
|
|
|
|
|
public static final String DES = "DES";
|
|
|
|
|
public static final String AES = "AES";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 签名算法
|
|
|
|
|
*/
|
|
|
|
|
public static final String SIGN_ALGORITHMS = "SHA1PRNG";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 编码格式;默认使用uft-8
|
|
|
|
|
*/
|
|
|
|
@ -152,7 +161,7 @@ public class EncryptUtil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String base64(byte[] res) {
|
|
|
|
|
return Base64.encode(res);
|
|
|
|
|
return Base64.getEncoder().encodeToString(res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -313,24 +322,90 @@ public class EncryptUtil {
|
|
|
|
|
return res ^ key.hashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 使用Base64进行加密
|
|
|
|
|
* base 64 encode
|
|
|
|
|
*
|
|
|
|
|
* @param res 密文
|
|
|
|
|
* @return
|
|
|
|
|
* @param bytes 待编码的byte[]
|
|
|
|
|
* @return 编码后的base 64 code
|
|
|
|
|
*/
|
|
|
|
|
public String Base64Encode(String res) {
|
|
|
|
|
return Base64.encode(res.getBytes());
|
|
|
|
|
public static String base64Encode(byte[] bytes) {
|
|
|
|
|
return Base64.getEncoder().encodeToString(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 使用Base64进行解密
|
|
|
|
|
* base 64 decode
|
|
|
|
|
*
|
|
|
|
|
* @param res
|
|
|
|
|
* @return
|
|
|
|
|
* @param base64Code 待解码的base 64 code
|
|
|
|
|
* @return 解码后的byte[]
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public String Base64Decode(String res) {
|
|
|
|
|
return new String(Base64.decode(res));
|
|
|
|
|
public static byte[] base64Decode(String base64Code) throws Exception {
|
|
|
|
|
return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AES加密
|
|
|
|
|
*
|
|
|
|
|
* @param content 待加密的内容
|
|
|
|
|
* @param encryptKey 加密密钥
|
|
|
|
|
* @return 加密后的byte[]
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
@SneakyThrows
|
|
|
|
|
public static byte[] aesEncryptToBytes(String content, String encryptKey) {
|
|
|
|
|
KeyGenerator kgen = KeyGenerator.getInstance(AES);
|
|
|
|
|
kgen.init(128);
|
|
|
|
|
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
|
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), AES));
|
|
|
|
|
return cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AES加密为base 64 code
|
|
|
|
|
*
|
|
|
|
|
* @param content 待加密的内容
|
|
|
|
|
* @param encryptKey 加密密钥
|
|
|
|
|
* @return 加密后的base 64 code
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static String aesEncrypt(String content, String encryptKey) {
|
|
|
|
|
return base64Encode(aesEncryptToBytes(content, encryptKey));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AES解密
|
|
|
|
|
*
|
|
|
|
|
* @param encryptBytes 待解密的byte[]
|
|
|
|
|
* @param decryptKey 解密密钥
|
|
|
|
|
* @return 解密后的String
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
|
|
|
|
|
KeyGenerator kgen = KeyGenerator.getInstance(AES);
|
|
|
|
|
kgen.init(128);
|
|
|
|
|
|
|
|
|
|
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
|
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), AES));
|
|
|
|
|
byte[] decryptBytes = cipher.doFinal(encryptBytes);
|
|
|
|
|
return new String(decryptBytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将base 64 code AES解密
|
|
|
|
|
*
|
|
|
|
|
* @param encryptStr 待解密的base 64 code
|
|
|
|
|
* @param decryptKey 解密密钥
|
|
|
|
|
* @return 解密后的string
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
|
|
|
|
|
return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|