Skip to content
This repository was archived by the owner on Apr 6, 2025. It is now read-only.

Commit e3ced2f

Browse files
committed
feat: Hàng rào sắt (Key Number/String)
1 parent cc41961 commit e3ced2f

6 files changed

Lines changed: 352 additions & 56 deletions

File tree

EncryptionAlgorithms/src/algorithms/Playfair.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,25 @@ public String decrypt() {
172172
for (int i = 0; i < encContent.length(); i = i + 2) {
173173
char char1 = encContent.charAt(i), char2 = encContent.charAt(i + 1);
174174
CASE caseChar = analyticsWhat2Char(char1, char2);
175+
String temp = "";
175176
switch (caseChar) {
176177
case HAI_KI_TU_CUNG_COT -> {
177-
str += twoCharSameCol_Decrypt(char1, char2);
178+
temp = twoCharSameCol_Decrypt(char1, char2);
179+
debug(">> 2 ký tự thuộc trường hợp cùng cột, biến đổi -> " + temp);
180+
181+
str += temp;
178182
}
179183
case HAI_KI_TU_CUNG_HANG -> {
180-
str += twoCharSameRow_Decrypt(char1, char2);
184+
temp = twoCharSameRow_Decrypt(char1, char2);
185+
debug(">> 2 ký tự thuộc trường hợp cùng hàng, biến đổi -> " + temp);
186+
187+
str += temp;
181188
}
182189
default -> {
183-
str += twoCharInASquare_Decrypt(char1, char2);
190+
temp = twoCharInASquare_Decrypt(char1, char2);
191+
debug(">> 2 ký tự thuộc trường hợp không cùng cột - không cùng hàng, biến đổi -> " + temp);
192+
193+
str += temp;
184194
}
185195
}
186196
}
@@ -204,9 +214,11 @@ public String decrypt() {
204214
strFinal = strFinal.replaceAll("XWX", "XX");
205215
continue;
206216
}
207-
if (strFinal.equalsIgnoreCase(strFinal.replaceAll("(.)(X)\\1", "$1$1"))) {
217+
if (strFinal.equalsIgnoreCase(strFinal.replaceAll("(.)(X)\\1", "$1$1")) && strFinal.equalsIgnoreCase(strFinal.replaceAll("(.) (X)\\1", "$1$1"))) {
208218
break;
209219
}
220+
strFinal = strFinal.replaceAll("(.) (X)\\1", "$1$1");
221+
210222
strFinal = strFinal.replaceAll("(.)(X)\\1", "$1$1");
211223
}
212224
return strFinal;
@@ -246,16 +258,30 @@ public String encrypt() {
246258
String str = "", strFinal = "";
247259
for (int i = 0; i < stringProcess.length(); i = i + 2) {
248260
char char1 = stringProcess.charAt(i), char2 = stringProcess.charAt(i + 1);
261+
debug("* Lấy ký tự " + char1 + " và " + char2);
262+
249263
CASE caseChar = analyticsWhat2Char(char1, char2);
264+
String temp = "";
265+
266+
250267
switch (caseChar) {
251268
case HAI_KI_TU_CUNG_COT -> {
252-
str += twoCharSameCol_Encrypt(char1, char2);
269+
temp = twoCharSameCol_Encrypt(char1, char2);
270+
debug(">> 2 ký tự thuộc trường hợp cùng cột, biến đổi -> " + temp);
271+
272+
str += temp;
253273
}
254274
case HAI_KI_TU_CUNG_HANG -> {
255-
str += twoCharSameRow_Encrypt(char1, char2);
275+
temp = twoCharSameRow_Encrypt(char1, char2);
276+
debug(">> 2 ký tự thuộc trường hợp cùng hàng, biến đổi -> " + temp);
277+
278+
str += temp;
256279
}
257280
default -> {
258-
str += twoCharInASquare_Encrypt(char1, char2);
281+
temp = twoCharInASquare_Encrypt(char1, char2);
282+
debug(">> 2 ký tự thuộc trường hợp không cùng hàng - không cùng cột, biến đổi -> " + temp);
283+
284+
str += temp;
259285
}
260286
}
261287
}

EncryptionAlgorithms/src/algorithms/RailFenceCipher.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ public RailFenceCipher(String originString, String key, JTextArea console) {
8080
this.originString = originString;
8181
this.key = key;
8282
this.console = console;
83+
debug("------ \nKhởi tạo thuật toán Rail Fence Cipher (Key số)\n------ \n");
84+
8385

8486
col = key.length();
8587

86-
row = originString.length() % col + 1;
88+
row = (int) Math.ceil((double) originString.length() / col) ;
8789

8890
tableEnc = new char[col][row];
8991

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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_String {
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 char[] kyTuKey;
17+
private JTextArea console;
18+
19+
public String getOriginString() {
20+
return originString;
21+
}
22+
23+
public void setOriginString(String originString) {
24+
this.originString = originString;
25+
}
26+
27+
public int getRow() {
28+
return row;
29+
}
30+
31+
public void setRow(int row) {
32+
this.row = row;
33+
}
34+
35+
public int getCol() {
36+
return col;
37+
}
38+
39+
public void setCol(int col) {
40+
this.col = col;
41+
}
42+
43+
public String getKey() {
44+
return key;
45+
}
46+
47+
public void setKey(String key) {
48+
this.key = key;
49+
}
50+
51+
public char[][] getTableEnc() {
52+
return tableEnc;
53+
}
54+
55+
public void setTableEnc(char[][] tableEnc) {
56+
this.tableEnc = tableEnc;
57+
}
58+
59+
public JTextArea getConsole() {
60+
return console;
61+
}
62+
63+
public void setConsole(JTextArea console) {
64+
this.console = console;
65+
}
66+
67+
public static int getAlphaNumber() {
68+
return ALPHA_NUMBER;
69+
}
70+
71+
public RailFenceCipher_String() {
72+
super();
73+
// TODO Auto-generated constructor stub
74+
}
75+
76+
/*
77+
* For key - Advanced
78+
*/
79+
public RailFenceCipher_String(String _originString, String _key, JTextArea console) {
80+
super();
81+
this.originString = _originString.toUpperCase().replaceAll("\\s", "");
82+
;
83+
this.key = _key.toUpperCase().replaceAll("[^A-Z]", "");
84+
this.console = console;
85+
86+
debug("------ \nKhởi tạo thuật toán Rail Fence Cipher (Key chuỗi)\n------ \n");
87+
88+
col = key.length();
89+
90+
debug("Khởi tạo số cột: " + col + "\n Khoá là: " + key);
91+
92+
row = (int) Math.ceil((double) originString.length() / col) ;
93+
debug("Khởi tạo số dòng: " + row + "\n Văn bản xử lý: " + originString);
94+
95+
tableEnc = new char[col][row];
96+
kyTuKey = new char[col];
97+
for (int i = 0; i < col; i++) {
98+
kyTuKey[i] = key.charAt(i);
99+
}
100+
101+
}
102+
103+
public void debug(String str) {
104+
if (console == null) {
105+
System.out.println(str);
106+
return;
107+
}
108+
console.setText(console.getText() + "\n" + str);
109+
console.setCaretPosition(console.getText().length());
110+
}
111+
112+
public String encrypt() {
113+
114+
int indexString = 0;
115+
String str = "";
116+
117+
String temp = "";
118+
for (int i = 0; i < row; i++) {
119+
for (int j = 0; j < col; j++) {
120+
if (originString.length() == indexString) {
121+
break;
122+
}
123+
tableEnc[j][i] = originString.charAt(indexString++);
124+
temp += tableEnc[j][i] + " ";
125+
}
126+
temp += "\n";
127+
}
128+
debug("Khởi tạo chuỗi cùng độ sâu\n" + temp);
129+
130+
int pos[] = new int[col];
131+
for (int i = 0; i < col; i++) {
132+
pos[i] = i;
133+
}
134+
135+
for (int i = 0; i < col - 1; i++) {
136+
for (int j = 0; j < col - i - 1; j++) {
137+
if (kyTuKey[j] > kyTuKey[j + 1]) {
138+
char tempCharKey = kyTuKey[j];
139+
kyTuKey[j] = kyTuKey[j + 1];
140+
kyTuKey[j + 1] = tempCharKey;
141+
int tempPos = pos[j];
142+
pos[j] = pos[j + 1];
143+
pos[j + 1] = tempPos;
144+
145+
}
146+
}
147+
}
148+
149+
temp = "";
150+
for (int i = 0; i < col; i++) {
151+
temp += " " + kyTuKey[i];
152+
}
153+
154+
debug("Sắp xếp khoá thứ tự: " + temp);
155+
156+
for (int i = 0; i < col; i++) {
157+
for (int j = 0; j < row; j++) {
158+
indexString++;
159+
str += (((int) tableEnc[pos[i]][j] <= 0)) ? "" : tableEnc[pos[i]][j];
160+
}
161+
}
162+
163+
debug("Kết quả chuỗi mã hoá với độ sâu = " + row + ": \n" + str);
164+
165+
return str;
166+
}
167+
168+
public String decrypt() {
169+
170+
int indexString = 0;
171+
String str = "";
172+
173+
String temp = "";
174+
175+
int pos[] = new int[col];
176+
for (int i = 0; i < col; i++) {
177+
pos[i] = i;
178+
}
179+
180+
for (int i = 0; i < col - 1; i++) {
181+
for (int j = 0; j < col - i - 1; j++) {
182+
if (kyTuKey[j] > kyTuKey[j + 1]) {
183+
char tempCharKey = kyTuKey[j];
184+
kyTuKey[j] = kyTuKey[j + 1];
185+
kyTuKey[j + 1] = tempCharKey;
186+
int tempPos = pos[j];
187+
pos[j] = pos[j + 1];
188+
pos[j + 1] = tempPos;
189+
190+
}
191+
}
192+
}
193+
194+
temp = "";
195+
for (int i = 0; i < col; i++) {
196+
temp += " " + kyTuKey[i];
197+
}
198+
debug("Sắp xếp khoá thứ tự: " + temp);
199+
200+
temp = "";
201+
202+
int maximumLastRow = col - ((row * col - originString.length()) % col) - 1;
203+
204+
debug("\nƯớc tính số ký tự hàng cuối: " + maximumLastRow + 1);
205+
int countRow = 0;
206+
207+
for (int j = 0; j < col; j++) {
208+
for (int i = 0; i < row; i++) {
209+
210+
if (indexString == originString.length())
211+
break;
212+
int acCol = pos[j];
213+
if (row - 1 == i)
214+
if (maximumLastRow - acCol < 0) {
215+
continue;
216+
}
217+
218+
tableEnc[acCol][i] = originString.charAt(indexString++);
219+
temp += tableEnc[acCol][i] + " ";
220+
}
221+
temp += "\n";
222+
}
223+
debug("Tái tạo bảng: \n" + temp);
224+
225+
temp = "";
226+
for (int i = 0; i < row; i++) {
227+
for (int j = 0; j < col; j++) {
228+
str += ((int)tableEnc[j][i]) <= 0 ? "" : tableEnc[j][i];
229+
temp += tableEnc[j][i] + " ";
230+
}
231+
temp += "\n";
232+
}
233+
234+
235+
debug("Sắp xếp dựa vào thứ tự khoá: \n" + temp);
236+
237+
debug("Kết quả chuỗi giải mã với độ sâu = " + row + ": \n" + str);
238+
239+
return str;
240+
}
241+
242+
@Override
243+
public String toString() {
244+
return "RailFenceCipher";
245+
}
246+
247+
}

EncryptionAlgorithms/src/application/InfoSoftware.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
* @author duong
1010
*/
1111
public class InfoSoftware {
12-
public static final String VERSION = "1.0";
12+
public static final String VERSION = "1.0.4_ALPHA";
1313
}

EncryptionAlgorithms/src/gui/Panel_MaHoaCoDien.form

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@
6363
<Font name="Segoe UI" size="14" style="0"/>
6464
</Property>
6565
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
66-
<StringArray count="3">
66+
<StringArray count="4">
6767
<StringItem index="0" value="Caesar Cipher"/>
6868
<StringItem index="1" value="Playfair"/>
6969
<StringItem index="2" value="Rail Fence Cipher (d&#xf9;ng &#x110;&#x1ed9; s&#xe2;u)"/>
70+
<StringItem index="3" value="Rail Fence Cipher (d&#xf9;ng Kho&#xe1;)"/>
7071
</StringArray>
7172
</Property>
7273
</Properties>

0 commit comments

Comments
 (0)