修改加密参数 上传数量

master
bynt 1 year ago
parent 64e181fd69
commit ff8f051d29

@ -10,5 +10,9 @@ public class AdPlatFormConstants {
public static final String AD_PLATFORM = "ad-platform"; public static final String AD_PLATFORM = "ad-platform";
public static final String PLATFORM_USER = "platform-user"; public static final String PLATFORM_USER = "platform-user";
public static final String PLAT_DECRYPTION = "CXpRNIJC4xlZd6vk";
} }

@ -1,17 +1,20 @@
package com.baiye.util; package com.baiye.util;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import com.baiye.constant.AdPlatFormConstants; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator; import javax.crypto.KeyGenerator;
import javax.crypto.Mac; import javax.crypto.Mac;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.Base64;
import java.util.Objects; import java.util.Objects;
/** /**
@ -28,10 +31,16 @@ public class EncryptUtil {
public static final String HmacSHA1 = "HmacSHA1"; public static final String HmacSHA1 = "HmacSHA1";
public static final String DES = "DES"; public static final String DES = "DES";
public static final String AES = "AES"; public static final String AES = "AES";
/** /**
* *
*/ */
public static final String SIGN_ALGORITHMS = "SHA1PRNG"; public static final String SIGN_ALGORITHMS = "SHA1PRNG";
private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
/** /**
* 使uft-8 * 使uft-8
*/ */
@ -152,7 +161,7 @@ public class EncryptUtil {
} }
private String base64(byte[] res) { 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(); return res ^ key.hashCode();
} }
/** /**
* 使Base64 * base 64 encode
* *
* @param res * @param bytes byte[]
* @return * @return base 64 code
*/ */
public String Base64Encode(String res) { public static String base64Encode(byte[] bytes) {
return Base64.encode(res.getBytes()); return Base64.getEncoder().encodeToString(bytes);
} }
/** /**
* 使Base64 * base 64 decode
* *
* @param res * @param base64Code base 64 code
* @return * @return byte[]
* @throws Exception
*/ */
public String Base64Decode(String res) { public static byte[] base64Decode(String base64Code) throws Exception {
return new String(Base64.decode(res)); 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));
}
/**
* AESbase 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);
}
} }

Loading…
Cancel
Save