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

Commit cc41961

Browse files
committed
Rail Fence cipher
1 parent eb3851b commit cc41961

File tree

6 files changed

+259
-9
lines changed

6 files changed

+259
-9
lines changed

EncryptionAlgorithms/nbproject/project.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ annotation.processing.run.all.processors=true
66
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
77
auxiliary.org-netbeans-modules-projectimport-eclipse-core.key=src=src;file=src/lib/gui325.jar;output=bin;
88
auxiliary.org-netbeans-modules-projectimport-eclipse-core.project=.
9-
auxiliary.org-netbeans-modules-projectimport-eclipse-core.timestamp=1708152664859
9+
auxiliary.org-netbeans-modules-projectimport-eclipse-core.timestamp=1709572119708
1010
auxiliary.org-netbeans-modules-projectimport-eclipse-core.workspace=..
1111
build.classes.dir=${build.dir}/classes
1212
build.classes.excludes=**/*.java,**/*.form
@@ -78,6 +78,7 @@ jlink.additionalmodules=
7878
jlink.additionalparam=
7979
jlink.launcher=true
8080
jlink.launcher.name=EncryptionAlgorithms
81+
main.class=application.MainApplication
8182
manifest.file=manifest.mf
8283
meta.inf.dir=${src.dir}/META-INF
8384
mkdist.disabled=true

EncryptionAlgorithms/src/algorithms/CaesarCipher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public CaesarCipher(String originString, int key) {
1919
public CaesarCipher(String originString, int key, JTextArea console) {
2020
super();
2121
this.console = console;
22-
debug(" ------ \n Khởi tạo thuật toán Caesar Cipher\n ------ \n");
22+
debug("------ \nKhởi tạo thuật toán Caesar Cipher\n------ \n");
2323
setOriginString(originString);
2424
setKey(key);
2525
}
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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+
}

EncryptionAlgorithms/src/gui/Panel_MaHoaCoDien.form

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@
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="2">
66+
<StringArray count="3">
6767
<StringItem index="0" value="Caesar Cipher"/>
6868
<StringItem index="1" value="Playfair"/>
69+
<StringItem index="2" value="Rail Fence Cipher (d&#xf9;ng &#x110;&#x1ed9; s&#xe2;u)"/>
6970
</StringArray>
7071
</Property>
7172
</Properties>

EncryptionAlgorithms/src/gui/Panel_MaHoaCoDien.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import algorithms.CaesarCipher;
88
import algorithms.Playfair;
9+
import algorithms.RailFenceCipher;
10+
911
import java.awt.Color;
1012
import java.awt.event.FocusEvent;
1113
import java.util.Random;
@@ -24,7 +26,8 @@ public class Panel_MaHoaCoDien extends javax.swing.JPanel {
2426

2527
private enum EnumThuatToan {
2628
CAESAR_CIPHER,
27-
PLAYFAIR
29+
PLAYFAIR,
30+
RAIL_FENCE_CIPHER_ROW
2831
}
2932

3033
private EnumThuatToan thuatToanDangChon;
@@ -116,7 +119,7 @@ private void initComponents() {
116119
add(jLabel2, gridBagConstraints);
117120

118121
cboEncryptAlgo.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
119-
cboEncryptAlgo.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Caesar Cipher", "Playfair" }));
122+
cboEncryptAlgo.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Caesar Cipher", "Playfair", "Rail Fence Cipher (dùng Độ sâu)" }));
120123
cboEncryptAlgo.addActionListener(new java.awt.event.ActionListener() {
121124
public void actionPerformed(java.awt.event.ActionEvent evt) {
122125
cboEncryptAlgoActionPerformed(evt);
@@ -341,6 +344,10 @@ private void radEncryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI
341344
lblInput.setText("Bản rõ:");
342345
lblOutput.setText("Bản mã:");
343346
btnSubmit.setText("Mã hoá");
347+
348+
txtInput.setText(txtOutput.getText());
349+
txtOutput.setText("");
350+
344351
btnSubmit.setForeground(Color.BLUE);
345352
}//GEN-LAST:event_radEncryptActionPerformed
346353

@@ -349,6 +356,9 @@ private void radDecryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI
349356
lblInput.setText("Bản mã:");
350357
lblOutput.setText("Bản rõ:");
351358
btnSubmit.setText("Giải mã");
359+
360+
txtInput.setText(txtOutput.getText());
361+
txtOutput.setText("");
352362

353363
btnSubmit.setForeground(Color.RED);
354364
}//GEN-LAST:event_radDecryptActionPerformed
@@ -361,6 +371,10 @@ private void txtKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:
361371
private void cboEncryptAlgoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboEncryptAlgoActionPerformed
362372
// TODO add your handling code here:
363373
switch (cboEncryptAlgo.getSelectedIndex()) {
374+
375+
case 2 -> {
376+
thuatToanDangChon = EnumThuatToan.RAIL_FENCE_CIPHER_ROW;
377+
}
364378
case 1 -> {
365379
thuatToanDangChon = EnumThuatToan.PLAYFAIR;
366380
}
@@ -383,6 +397,24 @@ private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR
383397
}
384398

385399
switch (thuatToanDangChon) {
400+
401+
case RAIL_FENCE_CIPHER_ROW -> {
402+
try {
403+
keyInt = parseInt(key);
404+
if (keyInt == -1) {
405+
return;
406+
}
407+
RailFenceCipher rfc = new RailFenceCipher(input, keyInt, txtLoadConsole);
408+
if (radEncrypt.isSelected()) {
409+
txtOutput.setText(rfc.encrypt());
410+
} else {
411+
txtOutput.setText(rfc.decrypt());
412+
}
413+
} catch (Exception e) {
414+
JOptionPane.showMessageDialog(null, "Lỗi: " + e.getMessage());
415+
e.printStackTrace();
416+
}
417+
}
386418
case PLAYFAIR -> {
387419
try {
388420
Playfair playFair = new Playfair(input, key, txtLoadConsole);

EncryptionAlgorithms/src/gui/Test.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,34 @@
22

33
import algorithms.CaesarCipher;
44
import algorithms.Playfair;
5+
import algorithms.RailFenceCipher;
56

67
public class Test {
78
public static void main(String[] args) {
9+
// try {
10+
// Playfair cc = new Playfair("tree stump", "tinhoc");
11+
// System.out.println(cc.encrypt());
12+
// Playfair cc1 = new Playfair(cc.encrypt(), "tinhoc");
13+
//
14+
// System.out.println(cc1.decrypt());
15+
// } catch (Exception e) {
16+
// // TODO Auto-generated catch block
17+
// e.printStackTrace();
18+
// }
19+
//System.out.println(cc.encrypt());
20+
//System.out.println(cc.decrypt(cc.encrypt(), 199));
21+
822
try {
9-
Playfair cc = new Playfair("tree stump", "tinhoc");
23+
RailFenceCipher cc = new RailFenceCipher("meet me after the toga party", 2, null);
1024
System.out.println(cc.encrypt());
11-
Playfair cc1 = new Playfair(cc.encrypt(), "tinhoc");
25+
1226

27+
RailFenceCipher cc1 = new RailFenceCipher(cc.encrypt(), 2, null);
1328
System.out.println(cc1.decrypt());
29+
1430
} catch (Exception e) {
1531
// TODO Auto-generated catch block
1632
e.printStackTrace();
1733
}
18-
//System.out.println(cc.encrypt());
19-
//System.out.println(cc.decrypt(cc.encrypt(), 199));
2034
}
2135
}

0 commit comments

Comments
 (0)