File tree Expand file tree Collapse file tree 3 files changed +62
-0
lines changed
main/java/com/thealgorithms/ciphers
test/java/com/thealgorithms/ciphers Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change 52
52
* [ RSA] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/RSA.java )
53
53
* [ SimpleSubCipher] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/SimpleSubCipher.java )
54
54
* [ Vigenere] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Vigenere.java )
55
+ * [ XORCipher] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/XORCipher.java )
55
56
* conversions
56
57
* [ AffineConverter] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/AffineConverter.java )
57
58
* [ AnyBaseToAnyBase] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java )
613
614
* [ RSATest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/RSATest.java )
614
615
* [ SimpleSubCipherTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/SimpleSubCipherTest.java )
615
616
* [ VigenereTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/VigenereTest.java )
617
+ * [ XORCipherTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/XORCipherTest.java )
616
618
* conversions
617
619
* [ AnyBaseToDecimalTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AnyBaseToDecimalTest.java )
618
620
* [ BinaryToDecimalTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java )
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .ciphers ;
2
+
3
+ import java .nio .charset .Charset ;
4
+ import java .nio .charset .StandardCharsets ;
5
+ import java .util .Base64 ;
6
+
7
+ /**
8
+ * A simple implementation of XOR cipher that, given a key, allows to encrypt and decrypt a plaintext.
9
+ *
10
+ * @author <a href="https://github.com/lcsjunior">lcsjunior</a>
11
+ *
12
+ */
13
+ public class XORCipher {
14
+
15
+ private static final Charset CS_DEFAULT = StandardCharsets .UTF_8 ;
16
+
17
+ private XORCipher () {
18
+ }
19
+
20
+ private static byte [] xor (final byte [] inputBytes , final byte [] keyBytes ) {
21
+ byte [] outputBytes = new byte [inputBytes .length ];
22
+ for (int i = 0 ; i < inputBytes .length ; ++i ) {
23
+ outputBytes [i ] = (byte ) (inputBytes [i ] ^ keyBytes [i % keyBytes .length ]);
24
+ }
25
+ return outputBytes ;
26
+ }
27
+
28
+ public static String encrypt (final String plaintext , final String key ) {
29
+ byte [] plaintextBytes = plaintext .getBytes (CS_DEFAULT );
30
+ byte [] keyBytes = key .getBytes (CS_DEFAULT );
31
+ return Base64 .getEncoder ().encodeToString (xor (plaintextBytes , keyBytes ));
32
+ }
33
+
34
+ public static String decrypt (final String cipher , final String key ) {
35
+ byte [] cipherBytes = Base64 .getDecoder ().decode (cipher );
36
+ byte [] keyBytes = key .getBytes (CS_DEFAULT );
37
+ return new String (xor (cipherBytes , keyBytes ), CS_DEFAULT );
38
+ }
39
+
40
+ }
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .ciphers ;
2
+
3
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4
+
5
+ import org .junit .jupiter .api .Test ;
6
+
7
+ class XORCipherTest {
8
+
9
+ @ Test
10
+ void shouldEncryptAndDecryptTest () {
11
+ // given
12
+ String plaintext = "My t&xt th@t will be ençrypted..." ;
13
+ String key = "My ç&cret key!" ;
14
+ // when
15
+ String cipherText = XORCipher .encrypt (plaintext , key );
16
+ // then
17
+ assertEquals (XORCipher .decrypt (cipherText , key ), plaintext );
18
+ }
19
+
20
+ }
You can’t perform that action at this time.
0 commit comments