REM:
1. 协议加密方法由AES256修改为AES128
This commit is contained in:
HuangXin 2020-09-13 08:01:16 +08:00
parent 9776440d04
commit 73a9bcbe43
2 changed files with 8 additions and 8 deletions

View File

@ -71,7 +71,7 @@ public class ProtocolSecurityServiceImpl implements ProtocolSecurityService {
decryptContent = base64Decode; decryptContent = base64Decode;
} else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_AES256.getCode()) { } else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_AES256.getCode()) {
try { try {
decryptContent = CryptoHelper.aes256Decryption(base64Decode, SecurityConfigValue.AES_KEY); decryptContent = CryptoHelper.aes128Decryption(base64Decode, SecurityConfigValue.AES_KEY);
} catch (Exception e) { } catch (Exception e) {
log.error("AES256 decode message error: {}", base64Decode); log.error("AES256 decode message error: {}", base64Decode);
throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_AES256); throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_AES256);
@ -123,7 +123,7 @@ public class ProtocolSecurityServiceImpl implements ProtocolSecurityService {
cipherText = CryptoHelper.base64Encryption(plainText.getBytes(StandardCharsets.UTF_8)); cipherText = CryptoHelper.base64Encryption(plainText.getBytes(StandardCharsets.UTF_8));
} else if (cryptoType == ProtoCryptoType.CRYPTO_AES256.getCode()) { } else if (cryptoType == ProtoCryptoType.CRYPTO_AES256.getCode()) {
try { try {
byte[] encode = CryptoHelper.aes256Encryption(plainText.getBytes(StandardCharsets.UTF_8), byte[] encode = CryptoHelper.aes128Encryption(plainText.getBytes(StandardCharsets.UTF_8),
SecurityConfigValue.AES_KEY); SecurityConfigValue.AES_KEY);
cipherText = CryptoHelper.base64Encryption(encode); cipherText = CryptoHelper.base64Encryption(encode);
} catch (Exception e) { } catch (Exception e) {

View File

@ -82,13 +82,13 @@ public class CryptoHelper {
* @throws BadPaddingException the bad padding exception * @throws BadPaddingException the bad padding exception
* @throws IllegalBlockSizeException the illegal block size exception * @throws IllegalBlockSizeException the illegal block size exception
*/ */
public static byte[] aes256Encryption(byte[] plaintext, String aesKey) throws NoSuchAlgorithmException, public static byte[] aes128Encryption(byte[] plaintext, String aesKey) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES"); KeyGenerator keyGen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(sha256Encryption(aesKey)); secureRandom.setSeed(aesKey.getBytes());
keyGen.init(256, secureRandom); keyGen.init(128, secureRandom);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR); Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR);
SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "AES"); SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key); cipher.init(Cipher.ENCRYPT_MODE, key);
@ -107,13 +107,13 @@ public class CryptoHelper {
* @throws BadPaddingException the bad padding exception * @throws BadPaddingException the bad padding exception
* @throws IllegalBlockSizeException the illegal block size exception * @throws IllegalBlockSizeException the illegal block size exception
*/ */
public static byte[] aes256Decryption(byte[] ciphertext, String aesKey) throws NoSuchAlgorithmException, public static byte[] aes128Decryption(byte[] ciphertext, String aesKey) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES"); KeyGenerator keyGen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(sha256Encryption(aesKey)); secureRandom.setSeed(aesKey.getBytes());
keyGen.init(256, secureRandom); keyGen.init(128, secureRandom);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR); Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR);
SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "AES"); SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "AES");
cipher.init(Cipher.DECRYPT_MODE, key); cipher.init(Cipher.DECRYPT_MODE, key);