Repository navigation
Expand file tree
/
Copy pathPlayfairTest.java
More file actions
63 lines (46 loc) · 1.92 KB
/
Copy pathPlayfairTest.java
File metadata and controls
63 lines (46 loc) · 1.92 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
package com.thealgorithms.ciphers;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class PlayfairTest {
@Test
public void testEncryption() {
PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD");
String plaintext = "HELLO";
String encryptedText = playfairCipher.encrypt(plaintext);
assertEquals("GYIZSC", encryptedText);
}
@Test
public void testDecryption() {
PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD");
String encryptedText = "UDRIYP";
String decryptedText = playfairCipher.decrypt(encryptedText);
assertEquals("NEBFVH", decryptedText);
}
@Test
public void testEncryptionAndDecryption() {
PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD");
String plaintext = "PLAYFAIR";
String encryptedText = playfairCipher.encrypt(plaintext);
String decryptedText = playfairCipher.decrypt(encryptedText);
assertEquals(plaintext, decryptedText);
}
@Test
public void testLowercaseKeyBehavesLikeUppercase() {
PlayfairCipher lowercase = new PlayfairCipher("monarchy");
PlayfairCipher uppercase = new PlayfairCipher("MONARCHY");
assertEquals(uppercase.encrypt("HELLO"), lowercase.encrypt("HELLO"));
assertEquals("CFSUPM", lowercase.encrypt("HELLO"));
}
@Test
public void testKeyContainingJStillCoversZ() {
PlayfairCipher playfairCipher = new PlayfairCipher("JUMP");
String decryptedText = playfairCipher.decrypt(playfairCipher.encrypt("ZOO"));
assertEquals("ZOXO", decryptedText);
}
@Test
public void testKeyWithNonLettersIsStripped() {
PlayfairCipher messy = new PlayfairCipher("Mon@rchy2026");
PlayfairCipher clean = new PlayfairCipher("MONRCHY");
assertEquals(clean.encrypt("HELLO"), messy.encrypt("HELLO"));
}
}