Repository navigation
Expand file tree
/
Copy pathAESEncryptionTest.java
More file actions
125 lines (99 loc) · 5.18 KB
/
Copy pathAESEncryptionTest.java
File metadata and controls
125 lines (99 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import javax.crypto.AEADBadTagException;
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");
}
@Test
public void testMultipleEncryptionsAndDecryptions() throws Exception {
SecretKey key = AESEncryption.getSecretEncryptionKey();
byte[] cipher1 = AESEncryption.encryptText("First Message", key);
byte[] cipher2 = AESEncryption.encryptText("Second Message", key);
assertEquals("First Message", AESEncryption.decryptText(cipher1, key));
assertEquals("Second Message", AESEncryption.decryptText(cipher2, key));
}
@Test
public void testShortCiphertextThrowsException() throws Exception {
SecretKey key = AESEncryption.getSecretEncryptionKey();
assertThrows(IllegalArgumentException.class, () -> AESEncryption.decryptText(null, key));
assertThrows(IllegalArgumentException.class, () -> AESEncryption.decryptText(new byte[27], key));
}
@Test
public void testEmptyPlaintextRoundTrip() throws Exception {
SecretKey key = AESEncryption.getSecretEncryptionKey();
byte[] cipherText = AESEncryption.encryptText("", key);
assertEquals(28, cipherText.length, "Empty plaintext should produce 12-byte IV + 16-byte tag");
assertEquals("", AESEncryption.decryptText(cipherText, key));
}
@Test
public void testFreshRandomIvPerEncryption() throws Exception {
SecretKey key = AESEncryption.getSecretEncryptionKey();
byte[] cipher1 = AESEncryption.encryptText("Same Message", key);
byte[] cipher2 = AESEncryption.encryptText("Same Message", key);
assertTrue(!Arrays.equals(cipher1, cipher2), "Each encryption should use a fresh random IV");
assertEquals("Same Message", AESEncryption.decryptText(cipher1, key));
assertEquals("Same Message", AESEncryption.decryptText(cipher2, key));
}
@Test
public void testModifiedCiphertextFailsAuthentication() throws Exception {
SecretKey key = AESEncryption.getSecretEncryptionKey();
byte[] cipherText = AESEncryption.encryptText("Authenticated Message", key);
byte[] modifiedCiphertext = cipherText.clone();
modifiedCiphertext[12] ^= 0x01;
assertThrows(AEADBadTagException.class, () -> AESEncryption.decryptText(modifiedCiphertext, key));
byte[] modifiedTag = cipherText.clone();
modifiedTag[modifiedTag.length - 1] ^= 0x01;
assertThrows(AEADBadTagException.class, () -> AESEncryption.decryptText(modifiedTag, key));
}
@Test
public void testDifferentKeyFailsAuthentication() throws Exception {
SecretKey key1 = AESEncryption.getSecretEncryptionKey();
SecretKey key2 = AESEncryption.getSecretEncryptionKey();
byte[] cipherText = AESEncryption.encryptText("Secret Message", key1);
assertThrows(AEADBadTagException.class, () -> AESEncryption.decryptText(cipherText, key2));
}
}