Skip to content

Commit f2d4ccc

Browse files
committed
Added improvements and decrypt test
1 parent 2d1f6ed commit f2d4ccc

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/main/java/com/thealgorithms/ciphers/XORCipher.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.nio.charset.Charset;
44
import java.nio.charset.StandardCharsets;
5-
import java.util.Base64;
5+
import java.util.HexFormat;
66

77
/**
88
* A simple implementation of XOR cipher that, given a key, allows to encrypt and decrypt a plaintext.
@@ -17,23 +17,25 @@ public final class XORCipher {
1717
private XORCipher() {
1818
}
1919

20-
private static byte[] xor(final byte[] inputBytes, final byte[] keyBytes) {
20+
public static byte[] xor(final byte[] inputBytes, final byte[] keyBytes) {
2121
byte[] outputBytes = new byte[inputBytes.length];
2222
for (int i = 0; i < inputBytes.length; ++i) {
2323
outputBytes[i] = (byte) (inputBytes[i] ^ keyBytes[i % keyBytes.length]);
2424
}
2525
return outputBytes;
2626
}
2727

28-
public static String encrypt(final String plaintext, final String key) {
29-
byte[] plaintextBytes = plaintext.getBytes(CS_DEFAULT);
28+
public static String encrypt(final String plainText, final String key) {
29+
byte[] plainTextBytes = plainText.getBytes(CS_DEFAULT);
3030
byte[] keyBytes = key.getBytes(CS_DEFAULT);
31-
return Base64.getEncoder().encodeToString(xor(plaintextBytes, keyBytes));
31+
byte[] xorResult = xor(plainTextBytes, keyBytes);
32+
return HexFormat.of().formatHex(xorResult);
3233
}
3334

34-
public static String decrypt(final String cipher, final String key) {
35-
byte[] cipherBytes = Base64.getDecoder().decode(cipher);
35+
public static String decrypt(final String cipherText, final String key) {
36+
byte[] cipherBytes = HexFormat.of().parseHex(cipherText);
3637
byte[] keyBytes = key.getBytes(CS_DEFAULT);
37-
return new String(xor(cipherBytes, keyBytes), CS_DEFAULT);
38+
byte[] xorResult = xor(cipherBytes, keyBytes);
39+
return new String(xorResult, CS_DEFAULT);
3840
}
3941
}

src/test/java/com/thealgorithms/ciphers/XORCipherTest.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,28 @@
77
class XORCipherTest {
88

99
@Test
10-
void shouldEncryptAndDecryptTest() {
10+
void xorEncryptTest() {
1111
// given
1212
String plaintext = "My t&xt th@t will be ençrypted...";
1313
String key = "My ç&cret key!";
14+
1415
// when
1516
String cipherText = XORCipher.encrypt(plaintext, key);
17+
18+
// then
19+
assertEquals("000000b7815e1752111c601f450e48211500a1c206061ca6d35212150d4429570eed", cipherText);
20+
}
21+
22+
@Test
23+
void xorDecryptTest() {
24+
// given
25+
String cipherText = "000000b7815e1752111c601f450e48211500a1c206061ca6d35212150d4429570eed";
26+
String key = "My ç&cret key!";
27+
28+
// when
29+
String plainText = XORCipher.decrypt(cipherText, key);
30+
1631
// then
17-
assertEquals(XORCipher.decrypt(cipherText, key), plaintext);
32+
assertEquals("My t&xt th@t will be ençrypted...", plainText);
1833
}
1934
}

0 commit comments

Comments
 (0)