diff --git a/src/main/java/com/dispose/service/impl/ProtocolSecurityServiceImpl.java b/src/main/java/com/dispose/service/impl/ProtocolSecurityServiceImpl.java index ac29af73..455a1ff8 100644 --- a/src/main/java/com/dispose/service/impl/ProtocolSecurityServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/ProtocolSecurityServiceImpl.java @@ -71,7 +71,7 @@ public class ProtocolSecurityServiceImpl implements ProtocolSecurityService { decryptContent = base64Decode; } else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_AES256.getCode()) { try { - decryptContent = CryptoHelper.aes256Decryption(base64Decode, SecurityConfigValue.AES_KEY); + decryptContent = CryptoHelper.aes128Decryption(base64Decode, SecurityConfigValue.AES_KEY); } catch (Exception e) { log.error("AES256 decode message error: {}", base64Decode); throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_AES256); @@ -123,7 +123,7 @@ public class ProtocolSecurityServiceImpl implements ProtocolSecurityService { cipherText = CryptoHelper.base64Encryption(plainText.getBytes(StandardCharsets.UTF_8)); } else if (cryptoType == ProtoCryptoType.CRYPTO_AES256.getCode()) { try { - byte[] encode = CryptoHelper.aes256Encryption(plainText.getBytes(StandardCharsets.UTF_8), + byte[] encode = CryptoHelper.aes128Encryption(plainText.getBytes(StandardCharsets.UTF_8), SecurityConfigValue.AES_KEY); cipherText = CryptoHelper.base64Encryption(encode); } catch (Exception e) { diff --git a/src/main/java/com/security/arithmetic/CryptoHelper.java b/src/main/java/com/security/arithmetic/CryptoHelper.java index 2ac8652f..0582233c 100644 --- a/src/main/java/com/security/arithmetic/CryptoHelper.java +++ b/src/main/java/com/security/arithmetic/CryptoHelper.java @@ -82,13 +82,13 @@ public class CryptoHelper { * @throws BadPaddingException the bad padding 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 { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); 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); SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key); @@ -107,13 +107,13 @@ public class CryptoHelper { * @throws BadPaddingException the bad padding 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 { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); 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); SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "AES"); cipher.init(Cipher.DECRYPT_MODE, key);