|
| 1 | +package algorithms; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashMap; |
| 5 | + |
| 6 | +import javax.swing.JTextArea; |
| 7 | + |
| 8 | +public class RailFenceCipher { |
| 9 | + |
| 10 | + private String originString; |
| 11 | + private int row, col; |
| 12 | + private String key; |
| 13 | + private static final int ALPHA_NUMBER = 26; |
| 14 | + private static final char A = 'A', Z = 'Z', a = 'a', z = 'z'; |
| 15 | + private char[][] tableEnc; |
| 16 | + private JTextArea console; |
| 17 | + |
| 18 | + public String getOriginString() { |
| 19 | + return originString; |
| 20 | + } |
| 21 | + |
| 22 | + public void setOriginString(String originString) { |
| 23 | + this.originString = originString; |
| 24 | + } |
| 25 | + |
| 26 | + public int getRow() { |
| 27 | + return row; |
| 28 | + } |
| 29 | + |
| 30 | + public void setRow(int row) { |
| 31 | + this.row = row; |
| 32 | + } |
| 33 | + |
| 34 | + public int getCol() { |
| 35 | + return col; |
| 36 | + } |
| 37 | + |
| 38 | + public void setCol(int col) { |
| 39 | + this.col = col; |
| 40 | + } |
| 41 | + |
| 42 | + public String getKey() { |
| 43 | + return key; |
| 44 | + } |
| 45 | + |
| 46 | + public void setKey(String key) { |
| 47 | + this.key = key; |
| 48 | + } |
| 49 | + |
| 50 | + public char[][] getTableEnc() { |
| 51 | + return tableEnc; |
| 52 | + } |
| 53 | + |
| 54 | + public void setTableEnc(char[][] tableEnc) { |
| 55 | + this.tableEnc = tableEnc; |
| 56 | + } |
| 57 | + |
| 58 | + public JTextArea getConsole() { |
| 59 | + return console; |
| 60 | + } |
| 61 | + |
| 62 | + public void setConsole(JTextArea console) { |
| 63 | + this.console = console; |
| 64 | + } |
| 65 | + |
| 66 | + public static int getAlphaNumber() { |
| 67 | + return ALPHA_NUMBER; |
| 68 | + } |
| 69 | + |
| 70 | + public RailFenceCipher() { |
| 71 | + super(); |
| 72 | + // TODO Auto-generated constructor stub |
| 73 | + } |
| 74 | + |
| 75 | + /* |
| 76 | + * For key - Advanced |
| 77 | + */ |
| 78 | + public RailFenceCipher(String originString, String key, JTextArea console) { |
| 79 | + super(); |
| 80 | + this.originString = originString; |
| 81 | + this.key = key; |
| 82 | + this.console = console; |
| 83 | + |
| 84 | + col = key.length(); |
| 85 | + |
| 86 | + row = originString.length() % col + 1; |
| 87 | + |
| 88 | + tableEnc = new char[col][row]; |
| 89 | + |
| 90 | + for (int i = 0; i < key.length(); i++) { |
| 91 | + tableEnc[i][0] = key.charAt(i); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + public void debug(String str) { |
| 97 | + if (console == null) { |
| 98 | + System.out.println(str); |
| 99 | + return; |
| 100 | + } |
| 101 | + console.setText(console.getText() + "\n" + str); |
| 102 | + console.setCaretPosition(console.getText().length()); |
| 103 | + } |
| 104 | + |
| 105 | + /* |
| 106 | + * Độ sâu |
| 107 | + */ |
| 108 | + public RailFenceCipher(String _originString, int row, JTextArea console) throws Exception{ |
| 109 | + super(); |
| 110 | + this.originString = _originString.toUpperCase().replaceAll("\\s", ""); |
| 111 | + this.console = console; |
| 112 | + |
| 113 | + this.row = row; |
| 114 | + |
| 115 | + if (row == 0) |
| 116 | + throw new Exception("Độ sâu phải lớn hơn 0"); |
| 117 | + debug("Khởi tạo độ sâu: " + row); |
| 118 | + this.col = originString.length() / row + 1; |
| 119 | + debug("Khởi tạo chiều dài 1 hàng: " + col); |
| 120 | + tableEnc = new char[col][row]; |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + public String encrypt() { |
| 126 | + |
| 127 | + |
| 128 | + int indexString = 0; |
| 129 | + String str = ""; |
| 130 | + |
| 131 | + String temp = ""; |
| 132 | + for (int i = 0; i < col; i++) { |
| 133 | + for (int j = 0; j < row; j++) { |
| 134 | + if (originString.length() == indexString) { |
| 135 | + break; |
| 136 | + } |
| 137 | + tableEnc[i][j] = originString.charAt(indexString++); |
| 138 | + temp += tableEnc[i][j] + " "; |
| 139 | + } |
| 140 | + |
| 141 | + temp += "\n"; |
| 142 | + } |
| 143 | + debug("Khởi tạo chuỗi cùng độ sâu\n" + temp); |
| 144 | + |
| 145 | + indexString = 0; |
| 146 | + for (int i = 0; i < row; i++) { |
| 147 | + for (int j = 0; j < col; j++) { |
| 148 | + if (indexString == originString.length()) |
| 149 | + break; |
| 150 | + indexString++; |
| 151 | + str += tableEnc[j][i]; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + debug("Kết quả chuỗi mã hoá với độ sâu = " + row + ": \n" + str); |
| 156 | + |
| 157 | + |
| 158 | + return str; |
| 159 | + } |
| 160 | + |
| 161 | + public String decrypt() { |
| 162 | + |
| 163 | + |
| 164 | + int indexString = 0; |
| 165 | + String str = ""; |
| 166 | + |
| 167 | + String temp = ""; |
| 168 | + for (int i = 0; i < row; i++) { |
| 169 | + for (int j = 0; j < col; j++) { |
| 170 | + if (originString.length() == indexString) { |
| 171 | + break; |
| 172 | + } |
| 173 | + tableEnc[j][i] = originString.charAt(indexString++); |
| 174 | + temp += tableEnc[j][i] + " "; |
| 175 | + } |
| 176 | + |
| 177 | + temp += "\n"; |
| 178 | + } |
| 179 | + debug("Khởi tạo chuỗi cùng độ sâu\n" + temp); |
| 180 | + |
| 181 | + indexString = 0; |
| 182 | + for (int i = 0; i < col; i++) { |
| 183 | + for (int j = 0; j < row; j++) { |
| 184 | + if (indexString == originString.length()) |
| 185 | + break; |
| 186 | + indexString++; |
| 187 | + str += tableEnc[i][j]; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + debug("Kết quả chuỗi giải hoá với độ sâu = " + row + ": \n" + str); |
| 192 | + |
| 193 | + |
| 194 | + return str; |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public String toString() { |
| 199 | + return "RailFenceCipher"; |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments