-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
AESEncryptionTest.java
62 lines (48 loc) · 2.3 KB
/
AESEncryptionTest.java
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
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");
}
}