Skip to content

Commit cdff8a2

Browse files
author
khalil2535
committed
Update ColumnarTranspositionCipher.java
1 parent e5e6fc1 commit cdff8a2

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

ciphers/ColumnarTranspositionCipher.java

+35-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public class ColumnarTranspositionCipher {
1010
private static String keyword;
1111
private static Object[][] table;
1212
private static String abecedarium;
13-
public static final String ABECEDARIUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.;:-@";
13+
public static final String ABECEDARIUM = "abcdefghijklmnopqrstuvwxyzABCDEFG"
14+
+ "HIJKLMNOPQRSTUVWXYZ0123456789,.;:-@";
1415
private static final String ENCRYPTION_FIELD = "≈";
1516
private static final char ENCRYPTION_FIELD_CHAR = '≈';
1617

@@ -46,7 +47,8 @@ public static String encrpyter(String word, String keyword) {
4647
* @return a String with the word encrypted by the Columnar Transposition
4748
* Cipher Rule
4849
*/
49-
public static String encrpyter(String word, String keyword, String abecedarium) {
50+
public static String encrpyter(String word, String keyword,
51+
String abecedarium) {
5052
ColumnarTranspositionCipher.keyword = keyword;
5153
if (abecedarium != null) {
5254
ColumnarTranspositionCipher.abecedarium = abecedarium;
@@ -122,18 +124,24 @@ private static int numberOfRows(String word) {
122124
}
123125
}
124126

127+
/**
128+
*
129+
* @return charValues
130+
*/
125131
private static Object[] findElements() {
126132
Object[] charValues = new Object[keyword.length()];
127133
for (int i = 0; i < charValues.length; i++) {
128-
for (int j = 0; j < abecedarium.length(); j++) {
129-
if (keyword.charAt(i) == abecedarium.charAt(j)) {
130-
charValues[i] = j;
131-
}
132-
}
134+
int charValueIndex = abecedarium.indexOf(keyword.charAt(i));
135+
charValues[i] = charValueIndex > -1 ? charValueIndex : null;
133136
}
134137
return charValues;
135138
}
136139

140+
/**
141+
*
142+
* @param table
143+
* @return tableSorted
144+
*/
137145
private static Object[][] sortTable(Object[][] table) {
138146
Object[][] tableSorted = new Object[table.length][table[0].length];
139147
for (int i = 0; i < tableSorted.length; i++) {
@@ -150,6 +158,13 @@ private static Object[][] sortTable(Object[][] table) {
150158
return tableSorted;
151159
}
152160

161+
/**
162+
*
163+
* @param table
164+
* @param rows
165+
* @param column
166+
* @return columnArray
167+
*/
153168
private static Object[] getColumn(Object[][] table, int rows, int column) {
154169
Object[] columnArray = new Object[rows];
155170
for (int i = 0; i < rows; i++) {
@@ -158,7 +173,15 @@ private static Object[] getColumn(Object[][] table, int rows, int column) {
158173
return columnArray;
159174
}
160175

161-
private static void switchColumns(Object[][] table, int firstColumnIndex, int secondColumnIndex, Object[] columnToSwitch) {
176+
/**
177+
*
178+
* @param table
179+
* @param firstColumnIndex
180+
* @param secondColumnIndex
181+
* @param columnToSwitch
182+
*/
183+
private static void switchColumns(Object[][] table, int firstColumnIndex,
184+
int secondColumnIndex, Object[] columnToSwitch) {
162185
for (int i = 0; i < table.length; i++) {
163186
table[i][secondColumnIndex] = table[i][firstColumnIndex];
164187
table[i][firstColumnIndex] = columnToSwitch[i];
@@ -191,8 +214,10 @@ public static void main(String[] args) {
191214
String wordBeingEncrypted = "This is a test of the Columnar Transposition Cipher";
192215
System.out.println("### Example of Columnar Transposition Cipher ###\n");
193216
System.out.println("Word being encryped ->>> " + wordBeingEncrypted);
194-
System.out.println("Word encrypted ->>> " + ColumnarTranspositionCipher.encrpyter(wordBeingEncrypted, keywordForExample));
195-
System.out.println("Word decryped ->>> " + ColumnarTranspositionCipher.decrypter());
217+
System.out.println("Word encrypted ->>> " + ColumnarTranspositionCipher
218+
.encrpyter(wordBeingEncrypted, keywordForExample));
219+
System.out.println("Word decryped ->>> " + ColumnarTranspositionCipher
220+
.decrypter());
196221
System.out.println("\n### Encrypted Table ###");
197222
showTable();
198223
}

0 commit comments

Comments
 (0)