From 73a9bcbe43e35b310d71b73e65aa53818945835b Mon Sep 17 00:00:00 2001 From: HuangXin Date: Sun, 13 Sep 2020 08:01:16 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=8D=8F=E8=AE=AE=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E6=96=B9=E6=B3=95=E7=94=B1AES256=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=BAAES128?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ProtocolSecurityServiceImpl.java | 4 ++-- .../java/com/security/arithmetic/CryptoHelper.java | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) 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);