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