Skip to content

Commit 6e6028e

Browse files
authored
Fix columnarTranspositionCipher and typos in Test (#5649)
1 parent 029dd85 commit 6e6028e

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ private ColumnarTranspositionCipher() {
2727
* @return a String with the word encrypted by the Columnar Transposition
2828
* Cipher Rule
2929
*/
30-
public static String encrpyter(String word, String keyword) {
30+
public static String encrypt(final String word, final String keyword) {
3131
ColumnarTranspositionCipher.keyword = keyword;
32-
abecedariumBuilder(500);
32+
abecedariumBuilder();
3333
table = tableBuilder(word);
3434
Object[][] sortedTable = sortTable(table);
3535
StringBuilder wordEncrypted = new StringBuilder();
36-
for (int i = 0; i < sortedTable[i].length; i++) {
36+
for (int i = 0; i < sortedTable[0].length; i++) {
3737
for (int j = 1; j < sortedTable.length; j++) {
3838
wordEncrypted.append(sortedTable[j][i]);
3939
}
@@ -51,11 +51,12 @@ public static String encrpyter(String word, String keyword) {
5151
* @return a String with the word encrypted by the Columnar Transposition
5252
* Cipher Rule
5353
*/
54-
public static String encrpyter(String word, String keyword, String abecedarium) {
54+
public static String encrypt(String word, String keyword, String abecedarium) {
5555
ColumnarTranspositionCipher.keyword = keyword;
5656
ColumnarTranspositionCipher.abecedarium = Objects.requireNonNullElse(abecedarium, ABECEDARIUM);
5757
table = tableBuilder(word);
5858
Object[][] sortedTable = sortTable(table);
59+
5960
StringBuilder wordEncrypted = new StringBuilder();
6061
for (int i = 0; i < sortedTable[0].length; i++) {
6162
for (int j = 1; j < sortedTable.length; j++) {
@@ -72,7 +73,7 @@ public static String encrpyter(String word, String keyword, String abecedarium)
7273
* @return a String decrypted with the word encrypted by the Columnar
7374
* Transposition Cipher Rule
7475
*/
75-
public static String decrypter() {
76+
public static String decrypt() {
7677
StringBuilder wordDecrypted = new StringBuilder();
7778
for (int i = 1; i < table.length; i++) {
7879
for (Object item : table[i]) {
@@ -91,14 +92,14 @@ public static String decrypter() {
9192
*/
9293
private static Object[][] tableBuilder(String word) {
9394
Object[][] table = new Object[numberOfRows(word) + 1][keyword.length()];
94-
char[] wordInChards = word.toCharArray();
95-
// Fils in the respective numbers
95+
char[] wordInChars = word.toCharArray();
96+
// Fills in the respective numbers for the column
9697
table[0] = findElements();
9798
int charElement = 0;
9899
for (int i = 1; i < table.length; i++) {
99100
for (int j = 0; j < table[i].length; j++) {
100-
if (charElement < wordInChards.length) {
101-
table[i][j] = wordInChards[charElement];
101+
if (charElement < wordInChars.length) {
102+
table[i][j] = wordInChars[charElement];
102103
charElement++;
103104
} else {
104105
table[i][j] = ENCRYPTION_FIELD_CHAR;
@@ -116,7 +117,7 @@ private static Object[][] tableBuilder(String word) {
116117
* order to respect the Columnar Transposition Cipher Rule.
117118
*/
118119
private static int numberOfRows(String word) {
119-
if (word.length() / keyword.length() > word.length() / keyword.length()) {
120+
if (word.length() % keyword.length() != 0) {
120121
return (word.length() / keyword.length()) + 1;
121122
} else {
122123
return word.length() / keyword.length();
@@ -173,13 +174,11 @@ private static void switchColumns(Object[][] table, int firstColumnIndex, int se
173174
}
174175

175176
/**
176-
* Creates an abecedarium with a specified ascii inded
177-
*
178-
* @param value Number of characters being used based on the ASCII Table
177+
* Creates an abecedarium with all available ascii values.
179178
*/
180-
private static void abecedariumBuilder(int value) {
179+
private static void abecedariumBuilder() {
181180
StringBuilder t = new StringBuilder();
182-
for (int i = 0; i < value; i++) {
181+
for (int i = 0; i < 256; i++) {
183182
t.append((char) i);
184183
}
185184
abecedarium = t.toString();

src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void setUp() {
2020

2121
@Test
2222
public void testEncryption() {
23-
String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword);
23+
String encryptedText = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
2424
assertNotNull(encryptedText, "The encrypted text should not be null.");
2525
assertFalse(encryptedText.isEmpty(), "The encrypted text should not be empty.");
2626
// Check if the encrypted text is different from the plaintext
@@ -29,19 +29,19 @@ public void testEncryption() {
2929

3030
@Test
3131
public void testDecryption() {
32-
String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword);
33-
String decryptedText = ColumnarTranspositionCipher.decrypter();
32+
String encryptedText = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
33+
String decryptedText = ColumnarTranspositionCipher.decrypt();
3434

3535
assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original plaintext, ignoring spaces.");
36-
assertEquals(encryptedText, ColumnarTranspositionCipher.encrpyter(plaintext, keyword), "The encrypted text should be the same when encrypted again.");
36+
assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(plaintext, keyword), "The encrypted text should be the same when encrypted again.");
3737
}
3838

3939
@Test
4040
public void testLongPlainText() {
4141
String longText = "This is a significantly longer piece of text to test the encryption and decryption capabilities of the Columnar Transposition Cipher. It should handle long strings gracefully.";
42-
String encryptedText = ColumnarTranspositionCipher.encrpyter(longText, keyword);
43-
String decryptedText = ColumnarTranspositionCipher.decrypter();
42+
String encryptedText = ColumnarTranspositionCipher.encrypt(longText, keyword);
43+
String decryptedText = ColumnarTranspositionCipher.decrypt();
4444
assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces.");
45-
assertEquals(encryptedText, ColumnarTranspositionCipher.encrpyter(longText, keyword), "The encrypted text should be the same when encrypted again.");
45+
assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(longText, keyword), "The encrypted text should be the same when encrypted again.");
4646
}
4747
}

0 commit comments

Comments
 (0)