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); + } }