@@ -27,13 +27,13 @@ private ColumnarTranspositionCipher() {
27
27
* @return a String with the word encrypted by the Columnar Transposition
28
28
* Cipher Rule
29
29
*/
30
- public static String encrpyter ( String word , String keyword ) {
30
+ public static String encrypt ( final String word , final String keyword ) {
31
31
ColumnarTranspositionCipher .keyword = keyword ;
32
- abecedariumBuilder (500 );
32
+ abecedariumBuilder ();
33
33
table = tableBuilder (word );
34
34
Object [][] sortedTable = sortTable (table );
35
35
StringBuilder wordEncrypted = new StringBuilder ();
36
- for (int i = 0 ; i < sortedTable [i ].length ; i ++) {
36
+ for (int i = 0 ; i < sortedTable [0 ].length ; i ++) {
37
37
for (int j = 1 ; j < sortedTable .length ; j ++) {
38
38
wordEncrypted .append (sortedTable [j ][i ]);
39
39
}
@@ -51,11 +51,12 @@ public static String encrpyter(String word, String keyword) {
51
51
* @return a String with the word encrypted by the Columnar Transposition
52
52
* Cipher Rule
53
53
*/
54
- public static String encrpyter (String word , String keyword , String abecedarium ) {
54
+ public static String encrypt (String word , String keyword , String abecedarium ) {
55
55
ColumnarTranspositionCipher .keyword = keyword ;
56
56
ColumnarTranspositionCipher .abecedarium = Objects .requireNonNullElse (abecedarium , ABECEDARIUM );
57
57
table = tableBuilder (word );
58
58
Object [][] sortedTable = sortTable (table );
59
+
59
60
StringBuilder wordEncrypted = new StringBuilder ();
60
61
for (int i = 0 ; i < sortedTable [0 ].length ; i ++) {
61
62
for (int j = 1 ; j < sortedTable .length ; j ++) {
@@ -72,7 +73,7 @@ public static String encrpyter(String word, String keyword, String abecedarium)
72
73
* @return a String decrypted with the word encrypted by the Columnar
73
74
* Transposition Cipher Rule
74
75
*/
75
- public static String decrypter () {
76
+ public static String decrypt () {
76
77
StringBuilder wordDecrypted = new StringBuilder ();
77
78
for (int i = 1 ; i < table .length ; i ++) {
78
79
for (Object item : table [i ]) {
@@ -91,14 +92,14 @@ public static String decrypter() {
91
92
*/
92
93
private static Object [][] tableBuilder (String word ) {
93
94
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
96
97
table [0 ] = findElements ();
97
98
int charElement = 0 ;
98
99
for (int i = 1 ; i < table .length ; i ++) {
99
100
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 ];
102
103
charElement ++;
103
104
} else {
104
105
table [i ][j ] = ENCRYPTION_FIELD_CHAR ;
@@ -116,7 +117,7 @@ private static Object[][] tableBuilder(String word) {
116
117
* order to respect the Columnar Transposition Cipher Rule.
117
118
*/
118
119
private static int numberOfRows (String word ) {
119
- if (word .length () / keyword .length () > word . length () / keyword . length () ) {
120
+ if (word .length () % keyword .length () != 0 ) {
120
121
return (word .length () / keyword .length ()) + 1 ;
121
122
} else {
122
123
return word .length () / keyword .length ();
@@ -173,13 +174,11 @@ private static void switchColumns(Object[][] table, int firstColumnIndex, int se
173
174
}
174
175
175
176
/**
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.
179
178
*/
180
- private static void abecedariumBuilder (int value ) {
179
+ private static void abecedariumBuilder () {
181
180
StringBuilder t = new StringBuilder ();
182
- for (int i = 0 ; i < value ; i ++) {
181
+ for (int i = 0 ; i < 256 ; i ++) {
183
182
t .append ((char ) i );
184
183
}
185
184
abecedarium = t .toString ();
0 commit comments