From bca1b11283584db35516235b61a697eeb3d36c36 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 11:12:16 +0530 Subject: [PATCH 1/3] Add Junit tests for `AESEncryption.java` --- .../ciphers/AESEncryptionTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java diff --git a/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java b/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java new file mode 100644 index 000000000000..60a8f80f9cc1 --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java @@ -0,0 +1,62 @@ +package com.thealgorithms.ciphers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import javax.crypto.SecretKey; +import org.junit.jupiter.api.Test; + +public class AESEncryptionTest { + + @Test + public void testGetSecretEncryptionKey() throws Exception { + SecretKey key = AESEncryption.getSecretEncryptionKey(); + assertNotNull(key, "Secret key should not be null"); + assertEquals(128, key.getEncoded().length * 8, "Key size should be 128 bits"); + } + + @Test + public void testEncryptText() throws Exception { + String plainText = "Hello World"; + SecretKey secKey = AESEncryption.getSecretEncryptionKey(); + byte[] cipherText = AESEncryption.encryptText(plainText, secKey); + + assertNotNull(cipherText, "Ciphertext should not be null"); + assertTrue(cipherText.length > 0, "Ciphertext should not be empty"); + } + + @Test + public void testDecryptText() throws Exception { + String plainText = "Hello World"; + SecretKey secKey = AESEncryption.getSecretEncryptionKey(); + byte[] cipherText = AESEncryption.encryptText(plainText, secKey); + + // Decrypt the ciphertext + String decryptedText = AESEncryption.decryptText(cipherText, secKey); + + assertNotNull(decryptedText, "Decrypted text should not be null"); + assertEquals(plainText, decryptedText, "Decrypted text should match the original plain text"); + } + + @Test + public void testEncryptDecrypt() throws Exception { + String plainText = "Hello AES!"; + SecretKey secKey = AESEncryption.getSecretEncryptionKey(); + + // Encrypt the plaintext + byte[] cipherText = AESEncryption.encryptText(plainText, secKey); + + // Decrypt the ciphertext + String decryptedText = AESEncryption.decryptText(cipherText, secKey); + + assertEquals(plainText, decryptedText, "Decrypted text should match the original plain text"); + } + + @Test + public void testBytesToHex() { + byte[] bytes = new byte[]{0, 1, 15, 16, (byte) 255}; // Test with diverse byte values + String hex = AESEncryption.bytesToHex(bytes); + assertEquals("00010F10FF", hex, "Hex representation should match the expected value"); + } +} From 1aabda916cb8d9b488c53305e38e7e8085e862f9 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 6 Oct 2024 05:42:35 +0000 Subject: [PATCH 2/3] Update directory --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1bad5d3b98a3..6ace939f788f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -618,6 +618,7 @@ * ciphers * a5 * [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java) + * [AESEncryptionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java) * [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java) * [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java) * [DESTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/DESTest.java) From a1ce5ae0ca8996ec8067f4d9f14543314ccd3dc5 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 11:24:56 +0530 Subject: [PATCH 3/3] Fix --- src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java b/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java index 60a8f80f9cc1..2f0831e35064 100644 --- a/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java +++ b/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java @@ -55,7 +55,7 @@ public void testEncryptDecrypt() throws Exception { @Test public void testBytesToHex() { - byte[] bytes = new byte[]{0, 1, 15, 16, (byte) 255}; // Test with diverse byte values + byte[] bytes = new byte[] {0, 1, 15, 16, (byte) 255}; // Test with diverse byte values String hex = AESEncryption.bytesToHex(bytes); assertEquals("00010F10FF", hex, "Hex representation should match the expected value"); }