|
1 |
| -/** |
| 1 | +package ciphers; |
| 2 | + |
| 3 | +/** |
2 | 4 | * Columnar Transposition Cipher Encryption and Decryption.
|
| 5 | + * |
3 | 6 | * @author <a href="https://github.com/freitzzz">freitzzz</a>
|
4 | 7 | */
|
5 | 8 | public class ColumnarTranspositionCipher {
|
| 9 | + |
6 | 10 | private static String keyword;
|
7 | 11 | private static Object[][] table;
|
8 | 12 | private static String abecedarium;
|
9 |
| - public static final String ABECEDARIUM="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.;:-@"; |
10 |
| - private static final String ENCRYPTION_FIELD="≈"; |
11 |
| - private static final char ENCRYPTION_FIELD_CHAR='≈'; |
| 13 | + public static final String ABECEDARIUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.;:-@"; |
| 14 | + private static final String ENCRYPTION_FIELD = "≈"; |
| 15 | + private static final char ENCRYPTION_FIELD_CHAR = '≈'; |
| 16 | + |
12 | 17 | /**
|
13 | 18 | * Encrypts a certain String with the Columnar Transposition Cipher Rule
|
| 19 | + * |
14 | 20 | * @param word Word being encrypted
|
15 | 21 | * @param keyword String with keyword being used
|
16 |
| - * @return a String with the word encrypted by the Columnar Transposition Cipher Rule |
| 22 | + * @return a String with the word encrypted by the Columnar Transposition |
| 23 | + * Cipher Rule |
17 | 24 | */
|
18 |
| - public static String encrpyter(String word,String keyword){ |
19 |
| - ColumnarTranspositionCipher.keyword=keyword; |
| 25 | + public static String encrpyter(String word, String keyword) { |
| 26 | + ColumnarTranspositionCipher.keyword = keyword; |
20 | 27 | abecedariumBuilder(500);
|
21 |
| - table=tableBuilder(word); |
22 |
| - Object[][] sortedTable=sortTable(table); |
23 |
| - String wordEncrypted=""; |
24 |
| - for(int i=0;i<sortedTable[0].length;i++){ |
25 |
| - for(int j=1;j<sortedTable.length;j++){ |
26 |
| - wordEncrypted+=sortedTable[j][i]; |
| 28 | + table = tableBuilder(word); |
| 29 | + Object[][] sortedTable = sortTable(table); |
| 30 | + String wordEncrypted = ""; |
| 31 | + for (int i = 0; i < sortedTable[i].length; i++) { |
| 32 | + for (int j = 1; j < sortedTable.length; j++) { |
| 33 | + wordEncrypted += sortedTable[j][i]; |
27 | 34 | }
|
28 | 35 | }
|
29 | 36 | return wordEncrypted;
|
30 | 37 | }
|
31 | 38 |
|
32 | 39 | /**
|
33 | 40 | * Encrypts a certain String with the Columnar Transposition Cipher Rule
|
| 41 | + * |
34 | 42 | * @param word Word being encrypted
|
35 | 43 | * @param keyword String with keyword being used
|
36 |
| - * @param abecedarium String with the abecedarium being used. null for default one |
37 |
| - * @return a String with the word encrypted by the Columnar Transposition Cipher Rule |
| 44 | + * @param abecedarium String with the abecedarium being used. null for |
| 45 | + * default one |
| 46 | + * @return a String with the word encrypted by the Columnar Transposition |
| 47 | + * Cipher Rule |
38 | 48 | */
|
39 |
| - public static String encrpyter(String word,String keyword,String abecedarium){ |
40 |
| - ColumnarTranspositionCipher.keyword=keyword; |
41 |
| - if(abecedarium!=null){ |
42 |
| - ColumnarTranspositionCipher.abecedarium=abecedarium; |
43 |
| - }else{ |
44 |
| - ColumnarTranspositionCipher.abecedarium=ABECEDARIUM; |
| 49 | + public static String encrpyter(String word, String keyword, String abecedarium) { |
| 50 | + ColumnarTranspositionCipher.keyword = keyword; |
| 51 | + if (abecedarium != null) { |
| 52 | + ColumnarTranspositionCipher.abecedarium = abecedarium; |
| 53 | + } else { |
| 54 | + ColumnarTranspositionCipher.abecedarium = ABECEDARIUM; |
45 | 55 | }
|
46 |
| - table=tableBuilder(word); |
47 |
| - Object[][] sortedTable=sortTable(table); |
48 |
| - String wordEncrypted=""; |
49 |
| - for(int i=0;i<sortedTable[0].length;i++){ |
50 |
| - for(int j=1;j<sortedTable.length;j++){ |
51 |
| - wordEncrypted+=sortedTable[j][i]; |
| 56 | + table = tableBuilder(word); |
| 57 | + Object[][] sortedTable = sortTable(table); |
| 58 | + String wordEncrypted = ""; |
| 59 | + for (int i = 0; i < sortedTable[0].length; i++) { |
| 60 | + for (int j = 1; j < sortedTable.length; j++) { |
| 61 | + wordEncrypted += sortedTable[j][i]; |
52 | 62 | }
|
53 | 63 | }
|
54 | 64 | return wordEncrypted;
|
55 | 65 | }
|
| 66 | + |
56 | 67 | /**
|
57 |
| - * Decryps a certain encrypted String with the Columnar Transposition Cipher Rule |
58 |
| - * @return a String decrypted with the word ecrypted by the Columnar Transpositiion Cipher Rule |
| 68 | + * Decrypts a certain encrypted String with the Columnar Transposition |
| 69 | + * Cipher Rule |
| 70 | + * |
| 71 | + * @return a String decrypted with the word encrypted by the Columnar |
| 72 | + * Transposition Cipher Rule |
59 | 73 | */
|
60 |
| - public static String decrypter(){ |
61 |
| - String wordDecrypted=""; |
62 |
| - for(int i=1;i<table.length;i++){ |
63 |
| - for(int j=0;j<table[i].length;j++){ |
64 |
| - wordDecrypted+=table[i][j]; |
| 74 | + public static String decrypter() { |
| 75 | + String wordDecrypted = ""; |
| 76 | + for (int i = 1; i < table.length; i++) { |
| 77 | + for (Object item : table[i]) { |
| 78 | + wordDecrypted += item; |
65 | 79 | }
|
66 | 80 | }
|
67 |
| - return wordDecrypted.replaceAll(ENCRYPTION_FIELD,""); |
| 81 | + return wordDecrypted.replaceAll(ENCRYPTION_FIELD, ""); |
68 | 82 | }
|
| 83 | + |
69 | 84 | /**
|
70 |
| - * Builds a table with the word to be encrpyted in rows by the Columnar Transposition Cipher Rule |
71 |
| - * @return An Object[][] with the word to be encrypted filled in rows and columns |
| 85 | + * Builds a table with the word to be encrypted in rows by the Columnar |
| 86 | + * Transposition Cipher Rule |
| 87 | + * |
| 88 | + * @return An Object[][] with the word to be encrypted filled in rows and |
| 89 | + * columns |
72 | 90 | */
|
73 |
| - private static Object[][] tableBuilder(String word){ |
74 |
| - Object[][] table=new Object[rows(word)+1][keyword.length()]; |
75 |
| - char[] wordInChards=word.toCharArray(); |
| 91 | + private static Object[][] tableBuilder(String word) { |
| 92 | + Object[][] table = new Object[numberOfRows(word) + 1][keyword.length()]; |
| 93 | + char[] wordInChards = word.toCharArray(); |
76 | 94 | //Fils in the respective numbers
|
77 |
| - table[0]=findElements(); |
78 |
| - int charElement=0; |
79 |
| - for(int i=1;i<table.length;i++){ |
80 |
| - for(int j=0;j<table[i].length;j++){ |
81 |
| - if(charElement<wordInChards.length){ |
82 |
| - table[i][j]=(char)wordInChards[charElement]; |
| 95 | + table[0] = findElements(); |
| 96 | + int charElement = 0; |
| 97 | + for (int i = 1; i < table.length; i++) { |
| 98 | + for (int j = 0; j < table[i].length; j++) { |
| 99 | + if (charElement < wordInChards.length) { |
| 100 | + table[i][j] = wordInChards[charElement]; |
83 | 101 | charElement++;
|
84 |
| - }else{ |
85 |
| - table[i][j]=ENCRYPTION_FIELD_CHAR; |
| 102 | + } else { |
| 103 | + table[i][j] = ENCRYPTION_FIELD_CHAR; |
86 | 104 | }
|
87 | 105 | }
|
88 | 106 | }
|
89 | 107 | return table;
|
90 | 108 | }
|
| 109 | + |
91 | 110 | /**
|
92 |
| - * Determines the number of rows the table should have regarding the Columnar Transposition Cipher Rule |
93 |
| - * @return an int with the number of rows that the table should have in order to respect the Columnar Transposition Cipher Rule. |
| 111 | + * Determines the number of rows the table should have regarding the |
| 112 | + * Columnar Transposition Cipher Rule |
| 113 | + * |
| 114 | + * @return an int with the number of rows that the table should have in |
| 115 | + * order to respect the Columnar Transposition Cipher Rule. |
94 | 116 | */
|
95 |
| - private static int rows(String word){ |
96 |
| - if((double)word.length()/keyword.length()>word.length()/keyword.length()){ |
97 |
| - return (word.length()/keyword.length())+1; |
98 |
| - }else{ |
99 |
| - return word.length()/keyword.length(); |
| 117 | + private static int numberOfRows(String word) { |
| 118 | + if ((double) word.length() / keyword.length() > word.length() / keyword.length()) { |
| 119 | + return (word.length() / keyword.length()) + 1; |
| 120 | + } else { |
| 121 | + return word.length() / keyword.length(); |
100 | 122 | }
|
101 | 123 | }
|
102 |
| - private static Object[] findElements(){ |
103 |
| - Object[] charValues=new Object[keyword.length()]; |
104 |
| - for(int i=0;i<charValues.length;i++){ |
105 |
| - for(int j=0;j<abecedarium.length();j++){ |
106 |
| - if(keyword.charAt(i)==abecedarium.charAt(j))charValues[i]=j; |
| 124 | + |
| 125 | + private static Object[] findElements() { |
| 126 | + Object[] charValues = new Object[keyword.length()]; |
| 127 | + 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 | + } |
107 | 132 | }
|
108 | 133 | }
|
109 | 134 | return charValues;
|
110 | 135 | }
|
111 |
| - private static Object[][] sortTable(Object[][] table){ |
112 |
| - Object[][] tableSorted=new Object[table.length][table[0].length]; |
113 |
| - for(int i=0;i<tableSorted.length;i++){ |
114 |
| - for(int j=0;j<tableSorted[i].length;j++){ |
115 |
| - tableSorted[i][j]=table[i][j]; |
116 |
| - } |
| 136 | + |
| 137 | + private static Object[][] sortTable(Object[][] table) { |
| 138 | + Object[][] tableSorted = new Object[table.length][table[0].length]; |
| 139 | + for (int i = 0; i < tableSorted.length; i++) { |
| 140 | + System.arraycopy(table[i], 0, tableSorted[i], 0, tableSorted[i].length); |
117 | 141 | }
|
118 |
| - for(int i=0;i<tableSorted[0].length;i++){ |
119 |
| - for(int j=i+1;j<tableSorted[0].length;j++){ |
120 |
| - if((int)tableSorted[0][i]>(int)table[0][j]){ |
121 |
| - Object[] column=getColumn(tableSorted,tableSorted.length,i); |
122 |
| - switchColumns(tableSorted,j,i,column); |
| 142 | + for (int i = 0; i < tableSorted[0].length; i++) { |
| 143 | + for (int j = i + 1; j < tableSorted[0].length; j++) { |
| 144 | + if ((int) tableSorted[0][i] > (int) table[0][j]) { |
| 145 | + Object[] column = getColumn(tableSorted, tableSorted.length, i); |
| 146 | + switchColumns(tableSorted, j, i, column); |
123 | 147 | }
|
124 | 148 | }
|
125 | 149 | }
|
126 | 150 | return tableSorted;
|
127 | 151 | }
|
128 |
| - private static Object[] getColumn(Object[][] table,int rows,int column){ |
129 |
| - Object[] columnArray=new Object[rows]; |
130 |
| - for(int i=0;i<rows;i++){ |
131 |
| - columnArray[i]=table[i][column]; |
| 152 | + |
| 153 | + private static Object[] getColumn(Object[][] table, int rows, int column) { |
| 154 | + Object[] columnArray = new Object[rows]; |
| 155 | + for (int i = 0; i < rows; i++) { |
| 156 | + columnArray[i] = table[i][column]; |
132 | 157 | }
|
133 | 158 | return columnArray;
|
134 | 159 | }
|
135 |
| - private static void switchColumns(Object[][] table, int firstColumnIndex,int secondColumnIndex,Object[] columnToSwitch){ |
136 |
| - for(int i=0;i<table.length;i++){ |
137 |
| - table[i][secondColumnIndex]=table[i][firstColumnIndex]; |
138 |
| - table[i][firstColumnIndex]=columnToSwitch[i]; |
| 160 | + |
| 161 | + private static void switchColumns(Object[][] table, int firstColumnIndex, int secondColumnIndex, Object[] columnToSwitch) { |
| 162 | + for (int i = 0; i < table.length; i++) { |
| 163 | + table[i][secondColumnIndex] = table[i][firstColumnIndex]; |
| 164 | + table[i][firstColumnIndex] = columnToSwitch[i]; |
139 | 165 | }
|
140 | 166 | }
|
| 167 | + |
141 | 168 | /**
|
142 | 169 | * Creates an abecedarium with a specified ascii inded
|
| 170 | + * |
143 | 171 | * @param value Number of characters being used based on the ASCII Table
|
144 | 172 | */
|
145 |
| - private static void abecedariumBuilder(int value){ |
146 |
| - abecedarium=""; |
147 |
| - for(int i=0;i<value;i++){ |
148 |
| - abecedarium+=(char)i; |
| 173 | + private static void abecedariumBuilder(int value) { |
| 174 | + abecedarium = ""; |
| 175 | + for (int i = 0; i < value; i++) { |
| 176 | + abecedarium += (char) i; |
149 | 177 | }
|
150 | 178 | }
|
151 |
| - private static void showTable(){ |
152 |
| - for(int i=0;i<table.length;i++){ |
153 |
| - for(int j=0;j<table[i].length;j++){ |
154 |
| - System.out.print(table[i][j]+" "); |
| 179 | + |
| 180 | + private static void showTable() { |
| 181 | + for (Object[] table1 : table) { |
| 182 | + for (Object item : table1) { |
| 183 | + System.out.print(item + " "); |
155 | 184 | }
|
156 | 185 | System.out.println();
|
157 | 186 | }
|
158 | 187 | }
|
159 |
| - public static void main(String[] args){ |
160 |
| - String keywordForExample="asd215"; |
161 |
| - String wordBeingEncrypted="This is a test of the Columnar Transposition Cipher"; |
| 188 | + |
| 189 | + public static void main(String[] args) { |
| 190 | + String keywordForExample = "asd215"; |
| 191 | + String wordBeingEncrypted = "This is a test of the Columnar Transposition Cipher"; |
162 | 192 | System.out.println("### Example of Columnar Transposition Cipher ###\n");
|
163 |
| - System.out.println("Word being encryped ->>> "+wordBeingEncrypted); |
164 |
| - System.out.println("Word encrypted ->>> "+ColumnarTranspositionCipher.encrpyter(wordBeingEncrypted,keywordForExample)); |
165 |
| - System.out.println("Word decryped ->>> "+ColumnarTranspositionCipher.decrypter()); |
| 193 | + 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()); |
166 | 196 | System.out.println("\n### Encrypted Table ###");
|
167 | 197 | showTable();
|
168 | 198 | }
|
|
0 commit comments