Skip to content

Commit 1f6f98a

Browse files
committed
fix clang format
1 parent 2eda377 commit 1f6f98a

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,32 @@
88
* A simple implementation of XOR cipher that, given a key, allows to encrypt and decrypt a plaintext.
99
*
1010
* @author <a href="https://github.com/lcsjunior">lcsjunior</a>
11-
*
11+
*
1212
*/
1313
public class XORCipher {
1414

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-
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+
}
4039
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ class XORCipherTest {
88

99
@Test
1010
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);
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);
1818
}
19-
2019
}

0 commit comments

Comments
 (0)