forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneTimePadCipherTest.java
More file actions
33 lines (25 loc) · 1.01 KB
/
OneTimePadCipherTest.java
File metadata and controls
33 lines (25 loc) · 1.01 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
package com.thealgorithms.ciphers;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
public class OneTimePadCipherTest {
@Test
public void encryptDecryptWorks() {
String original = "OTP Test";
byte[] plaintext = original.getBytes(StandardCharsets.UTF_8);
byte[] key = OneTimePadCipher.generateKey(plaintext.length);
byte[] encrypted = OneTimePadCipher.encrypt(plaintext, key);
byte[] decrypted = OneTimePadCipher.decrypt(encrypted, key);
assertEquals(original, new String(decrypted, StandardCharsets.UTF_8));
}
@Test
public void throwsIfDifferentLength() {
byte[] plaintext = "Hi".getBytes(StandardCharsets.UTF_8);
byte[] key = new byte[] {1, 2, 3};
assertThrows(
IllegalArgumentException.class,
() -> OneTimePadCipher.encrypt(plaintext, key)
);
}
}