From c9716b81c5370ff13d15e9b9af41640cedf812fc Mon Sep 17 00:00:00 2001 From: HuangXin Date: Wed, 16 Sep 2020 15:28:46 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0AES256?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E7=AE=97=E6=B3=95=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/security/arithmetic/CryptoHelper.java | 39 +++++++++++++++++++ .../test/dev/function/CryptoHelperTest.java | 17 +++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/security/arithmetic/CryptoHelper.java b/src/main/java/com/security/arithmetic/CryptoHelper.java index de8ae56d..4d16f02b 100644 --- a/src/main/java/com/security/arithmetic/CryptoHelper.java +++ b/src/main/java/com/security/arithmetic/CryptoHelper.java @@ -116,6 +116,45 @@ public class CryptoHelper { return cipher.doFinal(ciphertext); } + /** + * Aes 256 encryption byte [ ]. + * + * @param plaintext the plaintext + * @param aesKey the aes key + * @return the byte [ ] + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws NoSuchPaddingException the no such padding exception + * @throws InvalidKeyException the invalid key exception + * @throws BadPaddingException the bad padding exception + * @throws IllegalBlockSizeException the illegal block size exception + */ + public static byte[] aes256Encryption(byte[] plaintext, String aesKey) throws NoSuchAlgorithmException, + NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR); + SecretKeySpec key = new SecretKeySpec(sha256Encryption(aesKey), "AES"); + cipher.init(Cipher.ENCRYPT_MODE, key); + return cipher.doFinal(plaintext); + } + + /** + * Aes 256 decryption byte [ ]. + * + * @param ciphertext the ciphertext + * @param aesKey the aes key + * @return the byte [ ] + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws NoSuchPaddingException the no such padding exception + * @throws InvalidKeyException the invalid key exception + * @throws BadPaddingException the bad padding exception + * @throws IllegalBlockSizeException the illegal block size exception + */ + public static byte[] aes256Decryption(byte[] ciphertext, String aesKey) throws NoSuchAlgorithmException, + NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR); + SecretKeySpec key = new SecretKeySpec(sha256Encryption(aesKey), "AES"); + cipher.init(Cipher.DECRYPT_MODE, key); + return cipher.doFinal(ciphertext); + } /** * Des decryption byte [ ]. diff --git a/src/test/java/com/dispose/test/dev/function/CryptoHelperTest.java b/src/test/java/com/dispose/test/dev/function/CryptoHelperTest.java index 09386918..fa8db32b 100644 --- a/src/test/java/com/dispose/test/dev/function/CryptoHelperTest.java +++ b/src/test/java/com/dispose/test/dev/function/CryptoHelperTest.java @@ -65,7 +65,7 @@ public class CryptoHelperTest { * @throws InvalidKeyException the invalid key exception */ @Test - public void t2_desTest() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, + public void t3_desTest() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException { String srcTest = "hello word"; String key = "hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40"; @@ -77,4 +77,19 @@ public class CryptoHelperTest { byte[] aesDecode = CryptoHelper.desDecryption(aesCode, key); Assert.assertEquals(new String(aesDecode), srcTest); } + + @Test + public void t4_aes256Test() throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, + NoSuchAlgorithmException, NoSuchPaddingException { + String srcTest = "hello word"; + String key = "hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40"; + byte[] aesCode = CryptoHelper.aes256Encryption(srcTest.getBytes(StandardCharsets.UTF_8), key); + String showText = CryptoHelper.base64Encryption(aesCode); + + + Assert.assertEquals(showText, "3sTXo4P2/pGQEfL9UJ/wRQ=="); + + byte[] aesDecode = CryptoHelper.aes256Decryption(aesCode, key); + Assert.assertEquals(new String(aesDecode), srcTest); + } }