mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
Enhance docs, add more tests in XORCipher
(#5900)
This commit is contained in:
@ -1,34 +1,85 @@
|
||||
package com.thealgorithms.ciphers;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class XORCipherTest {
|
||||
|
||||
@Test
|
||||
void xorEncryptTest() {
|
||||
// given
|
||||
void xorEncryptDecryptTest() {
|
||||
String plaintext = "My t&xt th@t will be ençrypted...";
|
||||
String key = "My ç&cret key!";
|
||||
|
||||
// when
|
||||
String cipherText = XORCipher.encrypt(plaintext, key);
|
||||
String decryptedText = XORCipher.decrypt(cipherText, key);
|
||||
|
||||
// then
|
||||
assertEquals("000000b7815e1752111c601f450e48211500a1c206061ca6d35212150d4429570eed", cipherText);
|
||||
assertEquals("My t&xt th@t will be ençrypted...", decryptedText);
|
||||
}
|
||||
|
||||
@Test
|
||||
void xorDecryptTest() {
|
||||
// given
|
||||
String cipherText = "000000b7815e1752111c601f450e48211500a1c206061ca6d35212150d4429570eed";
|
||||
String key = "My ç&cret key!";
|
||||
void testEmptyPlaintext() {
|
||||
String plaintext = "";
|
||||
String key = "anykey";
|
||||
|
||||
// when
|
||||
String plainText = XORCipher.decrypt(cipherText, key);
|
||||
String cipherText = XORCipher.encrypt(plaintext, key);
|
||||
String decryptedText = XORCipher.decrypt(cipherText, key);
|
||||
|
||||
// then
|
||||
assertEquals("My t&xt th@t will be ençrypted...", plainText);
|
||||
assertEquals("", cipherText);
|
||||
assertEquals("", decryptedText);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEmptyKey() {
|
||||
String plaintext = "Hello World!";
|
||||
String key = "";
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> XORCipher.encrypt(plaintext, key));
|
||||
assertThrows(IllegalArgumentException.class, () -> XORCipher.decrypt(plaintext, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testShortKey() {
|
||||
String plaintext = "Short message";
|
||||
String key = "k";
|
||||
|
||||
String cipherText = XORCipher.encrypt(plaintext, key);
|
||||
String decryptedText = XORCipher.decrypt(cipherText, key);
|
||||
|
||||
assertEquals(plaintext, decryptedText);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNonASCIICharacters() {
|
||||
String plaintext = "こんにちは世界"; // "Hello World" in Japanese (Konichiwa Sekai)
|
||||
String key = "key";
|
||||
|
||||
String cipherText = XORCipher.encrypt(plaintext, key);
|
||||
String decryptedText = XORCipher.decrypt(cipherText, key);
|
||||
|
||||
assertEquals(plaintext, decryptedText);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSameKeyAndPlaintext() {
|
||||
String plaintext = "samekey";
|
||||
String key = "samekey";
|
||||
|
||||
String cipherText = XORCipher.encrypt(plaintext, key);
|
||||
String decryptedText = XORCipher.decrypt(cipherText, key);
|
||||
|
||||
assertEquals(plaintext, decryptedText);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLongPlaintextShortKey() {
|
||||
String plaintext = "This is a long plaintext message.";
|
||||
String key = "key";
|
||||
|
||||
String cipherText = XORCipher.encrypt(plaintext, key);
|
||||
String decryptedText = XORCipher.decrypt(cipherText, key);
|
||||
|
||||
assertEquals(plaintext, decryptedText);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user