Skip to content

Add cipher class, cipher tests, enhance docs in 'AtbashCipher.java' #5690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 11, 2024
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 @@ -50,6 +50,7 @@
* [AES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AES.java)
* [AESEncryption](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AESEncryption.java)
* [AffineCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AffineCipher.java)
* [AtbashCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AtbashCipher.java)
* [Autokey](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Autokey.java)
* [Blowfish](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Blowfish.java)
* [Caesar](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Caesar.java)
Expand Down Expand Up @@ -663,6 +664,7 @@
* [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java)
* [AESEncryptionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java)
* [AffineCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AffineCipherTest.java)
* [AtbashTest](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AtbashTest.java)
* [AutokeyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java)
* [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java)
* [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java)
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/thealgorithms/ciphers/AtbashCipher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.thealgorithms.ciphers;

/**
* The Atbash cipher is a simple substitution cipher that replaces each letter
* in the alphabet with its reverse.
* For example, 'A' becomes 'Z', 'B' becomes 'Y', and so on. It works
* identically for both uppercase and lowercase letters.
* It's a symmetric cipher, meaning applying it twice returns the original text.
* Hence, the encrypting and the decrypting functions are identical
* @author https://github.com/Krounosity
* Learn more: https://en.wikipedia.org/wiki/Atbash
*/

public class AtbashCipher {

private String toConvert;

// Default constructor.
AtbashCipher() {
}

// String setting constructor.
AtbashCipher(String str) {
toConvert = str;
}

// String getter method.
public String getString() {
return toConvert;
}

// String setter method.
public void setString(String str) {
toConvert = str;
}

// Checking whether the current character is capital.
private boolean isCapital(char ch) {
return ch >= 'A' && ch <= 'Z';
}

// Checking whether the current character is smallcased.
private boolean isSmall(char ch) {
return ch >= 'a' && ch <= 'z';
}

// Converting text to atbash cipher code or vice versa.
public String convert() {

// Using StringBuilder to store new string.
StringBuilder convertedString = new StringBuilder();

// Iterating for each character.
for (char ch : toConvert.toCharArray()) {

// If the character is smallcased.
if (isSmall(ch)) {
convertedString.append((char) ('z' - (ch - 'a')));
}
// If the character is capital cased.
else if (isCapital(ch)) {
convertedString.append((char) ('Z' - (ch - 'A')));
}
// Non-alphabetical character.
else {
convertedString.append(ch);
}
}
return convertedString.toString();
}
}
28 changes: 28 additions & 0 deletions src/test/java/com/thealgorithms/ciphers/AtbashTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.thealgorithms.ciphers;

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

import org.junit.jupiter.api.Test;

public class AtbashTest {

@Test
public void atbashEncrypt() {
AtbashCipher normalToEncrypt = new AtbashCipher("Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!");
String expectedText = "Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!";

normalToEncrypt.setString(normalToEncrypt.convert());

assertEquals(expectedText, normalToEncrypt.getString());
}

@Test
public void atbashDecrypt() {
AtbashCipher encryptToNormal = new AtbashCipher("Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!");
String expectedText = "Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!";

encryptToNormal.setString(encryptToNormal.convert());

assertEquals(expectedText, encryptToNormal.getString());
}
}