mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-10 04:26:27 +08:00
Add cipher class, cipher tests, enhance docs in 'AtbashCipher.java' (#5690)
This commit is contained in:
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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user