Skip to content

Commit 2338428

Browse files
authored
Add cipher class, cipher tests, enhance docs in 'AtbashCipher.java' (#5690)
1 parent 2040df8 commit 2338428

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* [AES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AES.java)
5151
* [AESEncryption](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AESEncryption.java)
5252
* [AffineCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AffineCipher.java)
53+
* [AtbashCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AtbashCipher.java)
5354
* [Autokey](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Autokey.java)
5455
* [Blowfish](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Blowfish.java)
5556
* [Caesar](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Caesar.java)
@@ -663,6 +664,7 @@
663664
* [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java)
664665
* [AESEncryptionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java)
665666
* [AffineCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AffineCipherTest.java)
667+
* [AtbashTest](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AtbashTest.java)
666668
* [AutokeyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java)
667669
* [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java)
668670
* [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.thealgorithms.ciphers;
2+
3+
/**
4+
* The Atbash cipher is a simple substitution cipher that replaces each letter
5+
* in the alphabet with its reverse.
6+
* For example, 'A' becomes 'Z', 'B' becomes 'Y', and so on. It works
7+
* identically for both uppercase and lowercase letters.
8+
* It's a symmetric cipher, meaning applying it twice returns the original text.
9+
* Hence, the encrypting and the decrypting functions are identical
10+
* @author https://github.com/Krounosity
11+
* Learn more: https://en.wikipedia.org/wiki/Atbash
12+
*/
13+
14+
public class AtbashCipher {
15+
16+
private String toConvert;
17+
18+
// Default constructor.
19+
AtbashCipher() {
20+
}
21+
22+
// String setting constructor.
23+
AtbashCipher(String str) {
24+
toConvert = str;
25+
}
26+
27+
// String getter method.
28+
public String getString() {
29+
return toConvert;
30+
}
31+
32+
// String setter method.
33+
public void setString(String str) {
34+
toConvert = str;
35+
}
36+
37+
// Checking whether the current character is capital.
38+
private boolean isCapital(char ch) {
39+
return ch >= 'A' && ch <= 'Z';
40+
}
41+
42+
// Checking whether the current character is smallcased.
43+
private boolean isSmall(char ch) {
44+
return ch >= 'a' && ch <= 'z';
45+
}
46+
47+
// Converting text to atbash cipher code or vice versa.
48+
public String convert() {
49+
50+
// Using StringBuilder to store new string.
51+
StringBuilder convertedString = new StringBuilder();
52+
53+
// Iterating for each character.
54+
for (char ch : toConvert.toCharArray()) {
55+
56+
// If the character is smallcased.
57+
if (isSmall(ch)) {
58+
convertedString.append((char) ('z' - (ch - 'a')));
59+
}
60+
// If the character is capital cased.
61+
else if (isCapital(ch)) {
62+
convertedString.append((char) ('Z' - (ch - 'A')));
63+
}
64+
// Non-alphabetical character.
65+
else {
66+
convertedString.append(ch);
67+
}
68+
}
69+
return convertedString.toString();
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
public class AtbashTest {
8+
9+
@Test
10+
public void atbashEncrypt() {
11+
AtbashCipher normalToEncrypt = new AtbashCipher("Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!");
12+
String expectedText = "Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!";
13+
14+
normalToEncrypt.setString(normalToEncrypt.convert());
15+
16+
assertEquals(expectedText, normalToEncrypt.getString());
17+
}
18+
19+
@Test
20+
public void atbashDecrypt() {
21+
AtbashCipher encryptToNormal = new AtbashCipher("Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!");
22+
String expectedText = "Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!";
23+
24+
encryptToNormal.setString(encryptToNormal.convert());
25+
26+
assertEquals(expectedText, encryptToNormal.getString());
27+
}
28+
}

0 commit comments

Comments
 (0)