mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-10 13:39:08 +08:00
Add cipher class, cipher tests, enhance docs in 'AtbashCipher.java' (#5690)
This commit is contained in:
@ -50,6 +50,7 @@
|
|||||||
* [AES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AES.java)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [Caesar](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Caesar.java)
|
||||||
@ -663,6 +664,7 @@
|
|||||||
* [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java)
|
||||||
|
71
src/main/java/com/thealgorithms/ciphers/AtbashCipher.java
Normal file
71
src/main/java/com/thealgorithms/ciphers/AtbashCipher.java
Normal file
@ -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
src/test/java/com/thealgorithms/ciphers/AtbashTest.java
Normal file
28
src/test/java/com/thealgorithms/ciphers/AtbashTest.java
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user