@@ -10,7 +10,8 @@ public class ColumnarTranspositionCipher {
10
10
private static String keyword ;
11
11
private static Object [][] table ;
12
12
private static String abecedarium ;
13
- public static final String ABECEDARIUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.;:-@" ;
13
+ public static final String ABECEDARIUM = "abcdefghijklmnopqrstuvwxyzABCDEFG"
14
+ + "HIJKLMNOPQRSTUVWXYZ0123456789,.;:-@" ;
14
15
private static final String ENCRYPTION_FIELD = "≈" ;
15
16
private static final char ENCRYPTION_FIELD_CHAR = '≈' ;
16
17
@@ -46,7 +47,8 @@ public static String encrpyter(String word, String keyword) {
46
47
* @return a String with the word encrypted by the Columnar Transposition
47
48
* Cipher Rule
48
49
*/
49
- public static String encrpyter (String word , String keyword , String abecedarium ) {
50
+ public static String encrpyter (String word , String keyword ,
51
+ String abecedarium ) {
50
52
ColumnarTranspositionCipher .keyword = keyword ;
51
53
if (abecedarium != null ) {
52
54
ColumnarTranspositionCipher .abecedarium = abecedarium ;
@@ -122,18 +124,24 @@ private static int numberOfRows(String word) {
122
124
}
123
125
}
124
126
127
+ /**
128
+ *
129
+ * @return charValues
130
+ */
125
131
private static Object [] findElements () {
126
132
Object [] charValues = new Object [keyword .length ()];
127
133
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 ;
133
136
}
134
137
return charValues ;
135
138
}
136
139
140
+ /**
141
+ *
142
+ * @param table
143
+ * @return tableSorted
144
+ */
137
145
private static Object [][] sortTable (Object [][] table ) {
138
146
Object [][] tableSorted = new Object [table .length ][table [0 ].length ];
139
147
for (int i = 0 ; i < tableSorted .length ; i ++) {
@@ -150,6 +158,13 @@ private static Object[][] sortTable(Object[][] table) {
150
158
return tableSorted ;
151
159
}
152
160
161
+ /**
162
+ *
163
+ * @param table
164
+ * @param rows
165
+ * @param column
166
+ * @return columnArray
167
+ */
153
168
private static Object [] getColumn (Object [][] table , int rows , int column ) {
154
169
Object [] columnArray = new Object [rows ];
155
170
for (int i = 0 ; i < rows ; i ++) {
@@ -158,7 +173,15 @@ private static Object[] getColumn(Object[][] table, int rows, int column) {
158
173
return columnArray ;
159
174
}
160
175
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 ) {
162
185
for (int i = 0 ; i < table .length ; i ++) {
163
186
table [i ][secondColumnIndex ] = table [i ][firstColumnIndex ];
164
187
table [i ][firstColumnIndex ] = columnToSwitch [i ];
@@ -191,8 +214,10 @@ public static void main(String[] args) {
191
214
String wordBeingEncrypted = "This is a test of the Columnar Transposition Cipher" ;
192
215
System .out .println ("### Example of Columnar Transposition Cipher ###\n " );
193
216
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 ());
196
221
System .out .println ("\n ### Encrypted Table ###" );
197
222
showTable ();
198
223
}
0 commit comments