Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* [RSA](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/RSA.java)
* [SimpleSubCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/SimpleSubCipher.java)
* [Vigenere](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Vigenere.java)
* [XORCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/XORCipher.java)
* conversions
* [AffineConverter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/AffineConverter.java)
* [AnyBaseToAnyBase](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java)
Expand Down Expand Up @@ -619,6 +620,7 @@
* [RSATest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/RSATest.java)
* [SimpleSubCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/SimpleSubCipherTest.java)
* [VigenereTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/VigenereTest.java)
* [XORCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/XORCipherTest.java)
* conversions
* [AnyBaseToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AnyBaseToDecimalTest.java)
* [BinaryToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java)
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/thealgorithms/ciphers/XORCipher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.thealgorithms.ciphers;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HexFormat;

/**
* A simple implementation of XOR cipher that, given a key, allows to encrypt and decrypt a plaintext.
*
* @author <a href="https://github.com/lcsjunior">lcsjunior</a>
*
*/
public final class XORCipher {

private static final Charset CS_DEFAULT = StandardCharsets.UTF_8;

private XORCipher() {
}

public static byte[] xor(final byte[] inputBytes, final byte[] keyBytes) {
byte[] outputBytes = new byte[inputBytes.length];
for (int i = 0; i < inputBytes.length; ++i) {
outputBytes[i] = (byte) (inputBytes[i] ^ keyBytes[i % keyBytes.length]);
}
return outputBytes;
}

public static String encrypt(final String plainText, final String key) {
byte[] plainTextBytes = plainText.getBytes(CS_DEFAULT);
byte[] keyBytes = key.getBytes(CS_DEFAULT);
byte[] xorResult = xor(plainTextBytes, keyBytes);
return HexFormat.of().formatHex(xorResult);
}

public static String decrypt(final String cipherText, final String key) {
byte[] cipherBytes = HexFormat.of().parseHex(cipherText);
byte[] keyBytes = key.getBytes(CS_DEFAULT);
byte[] xorResult = xor(cipherBytes, keyBytes);
return new String(xorResult, CS_DEFAULT);
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/thealgorithms/ciphers/XORCipherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.thealgorithms.ciphers;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class XORCipherTest {

@Test
void xorEncryptTest() {
// given
String plaintext = "My t&xt th@t will be ençrypted...";
String key = "My ç&cret key!";

// when
String cipherText = XORCipher.encrypt(plaintext, key);

// then
assertEquals("000000b7815e1752111c601f450e48211500a1c206061ca6d35212150d4429570eed", cipherText);
}

@Test
void xorDecryptTest() {
// given
String cipherText = "000000b7815e1752111c601f450e48211500a1c206061ca6d35212150d4429570eed";
String key = "My ç&cret key!";

// when
String plainText = XORCipher.decrypt(cipherText, key);

// then
assertEquals("My t&xt th@t will be ençrypted...", plainText);
}
}