-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathXORCipher.java
41 lines (34 loc) · 1.36 KB
/
XORCipher.java
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
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);
}
}