-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathAtbashCipher.java
101 lines (91 loc) · 3.02 KB
/
AtbashCipher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.thealgorithms.ciphers;
/**
* The Atbash cipher is a classic substitution cipher that substitutes each letter
* with its opposite letter in the alphabet.
*
* For example:
* - 'A' becomes 'Z', 'B' becomes 'Y', 'C' becomes 'X', and so on.
* - Similarly, 'a' becomes 'z', 'b' becomes 'y', and so on.
*
* The cipher works identically for both uppercase and lowercase letters.
* Non-alphabetical characters remain unchanged in the output.
*
* This cipher is symmetric, meaning that applying the cipher twice will return
* the original text. Therefore, the same function is used for both encryption and decryption.
*
* <p>Usage Example:</p>
* <pre>
* AtbashCipher cipher = new AtbashCipher("Hello World!");
* String encrypted = cipher.convert(); // Output: "Svool Dliow!"
* </pre>
*
* @author <a href="https://github.com/Krounosity">Krounosity</a>
* @see <a href="https://en.wikipedia.org/wiki/Atbash">Atbash Cipher (Wikipedia)</a>
*/
public class AtbashCipher {
private String toConvert;
public AtbashCipher() {
}
/**
* Constructor with a string parameter.
*
* @param str The string to be converted using the Atbash cipher
*/
public AtbashCipher(String str) {
this.toConvert = str;
}
/**
* Returns the current string set for conversion.
*
* @return The string to be converted
*/
public String getString() {
return toConvert;
}
/**
* Sets the string to be converted using the Atbash cipher.
*
* @param str The new string to convert
*/
public void setString(String str) {
this.toConvert = str;
}
/**
* Checks if a character is uppercase.
*
* @param ch The character to check
* @return {@code true} if the character is uppercase, {@code false} otherwise
*/
private boolean isCapital(char ch) {
return ch >= 'A' && ch <= 'Z';
}
/**
* Checks if a character is lowercase.
*
* @param ch The character to check
* @return {@code true} if the character is lowercase, {@code false} otherwise
*/
private boolean isSmall(char ch) {
return ch >= 'a' && ch <= 'z';
}
/**
* Converts the input string using the Atbash cipher.
* Alphabetic characters are substituted with their opposite in the alphabet,
* while non-alphabetic characters remain unchanged.
*
* @return The converted string after applying the Atbash cipher
*/
public String convert() {
StringBuilder convertedString = new StringBuilder();
for (char ch : toConvert.toCharArray()) {
if (isSmall(ch)) {
convertedString.append((char) ('z' - (ch - 'a')));
} else if (isCapital(ch)) {
convertedString.append((char) ('Z' - (ch - 'A')));
} else {
convertedString.append(ch);
}
}
return convertedString.toString();
}
}