Skip to content

Commit 9bf2860

Browse files
committed
fixed ColumnarTranspositionCipher and added tests for it
1 parent ee6cd64 commit 9bf2860

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

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

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private ColumnarTranspositionCipher() {
1515
private static Object[][] table;
1616
private static String abecedarium;
1717
public static final String ABECEDARIUM = "abcdefghijklmnopqrstuvwxyzABCDEFG"
18-
+ "HIJKLMNOPQRSTUVWXYZ0123456789,.;:-@";
18+
+ "HIJKLMNOPQRSTUVWXYZ0123456789,.;:-@";
1919
private static final String ENCRYPTION_FIELD = "≈";
2020
private static final char ENCRYPTION_FIELD_CHAR = '≈';
2121

@@ -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,10 +117,10 @@ 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 {
122-
return word.length() / keyword.length();
123+
return word.length() / keyword.length() ;
123124
}
124125
}
125126

@@ -138,7 +139,7 @@ private static Object[] findElements() {
138139
/**
139140
* @return tableSorted
140141
*/
141-
private static Object[][] sortTable(Object[][] table) {
142+
private static Object[][] sortTable(Object[][] table) {
142143
Object[][] tableSorted = new Object[table.length][table[0].length];
143144
for (int i = 0; i < tableSorted.length; i++) {
144145
System.arraycopy(table[i], 0, tableSorted[i], 0, tableSorted[i].length);
@@ -173,18 +174,15 @@ 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();
186185
}
187-
188186
private static void showTable() {
189187
for (Object[] table1 : table) {
190188
for (Object item : table1) {
@@ -195,12 +193,12 @@ private static void showTable() {
195193
}
196194

197195
public static void main(String[] args) {
198-
String keywordForExample = "asd215";
199-
String wordBeingEncrypted = "This is a test of the Columnar Transposition Cipher";
196+
String keywordForExample = "test123456";
197+
String wordBeingEncrypted = "test";
200198
System.out.println("### Example of Columnar Transposition Cipher ###\n");
201199
System.out.println("Word being encryped ->>> " + wordBeingEncrypted);
202-
System.out.println("Word encrypted ->>> " + ColumnarTranspositionCipher.encrpyter(wordBeingEncrypted, keywordForExample));
203-
System.out.println("Word decryped ->>> " + ColumnarTranspositionCipher.decrypter());
200+
System.out.println("Word encrypted ->>> " + ColumnarTranspositionCipher.encrypt(wordBeingEncrypted, keywordForExample));
201+
System.out.println("Word decryped ->>> " + ColumnarTranspositionCipher.decrypt());
204202
System.out.println("\n### Encrypted Table ###");
205203
showTable();
206204
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package com.thealgorithms.ciphers;public class ColumnarTranspositionCipherTest {
2+
}

0 commit comments

Comments
 (0)