Skip to content

Commit 8267fb2

Browse files
authored
Added Column transposition Cipher in Java
The Columnar Transposition Cipher is a form of transposition cipher just like Rail Fence Cipher. Columnar Transposition involves writing the plaintext out in rows, and then reading the ciphertext off in columns one by one.
1 parent 0e93e2f commit 8267fb2

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

ColumnarTranspositionCipher.java

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import java.io.*;
2+
class Columnar {
3+
char arr[][], encrypt[][], decrypt[][], keya[], keytemp[];
4+
5+
void createMatrixEncrypt(String s, String key, int row, int column) {
6+
arr = new char[row][column];
7+
int k = 0;
8+
keya = key.toCharArray();
9+
for (int i = 0; i < row; i++) {
10+
for (int j = 0; j < column; j++) {
11+
if (k < s.length()) {
12+
arr[i][j] = s.charAt(k);
13+
k++;
14+
} else {
15+
arr[i][j] = ' ';
16+
}
17+
}
18+
}
19+
}
20+
21+
void createkey(String key, int column) {
22+
keytemp = key.toCharArray();
23+
for (int i = 0; i < column - 1; i++) {
24+
for (int j = i + 1; j < column; j++) {
25+
if (keytemp[i] > keytemp[j]) {
26+
char temp = keytemp[i];
27+
keytemp[i] = keytemp[j];
28+
keytemp[j] = temp;
29+
}
30+
}
31+
}
32+
}
33+
34+
void createMatrixDecrypt(String s, String key, int row, int column) {
35+
arr = new char[row][column];
36+
int k = 0;
37+
keya = key.toCharArray();
38+
for (int i = 0; i < column; i++) {
39+
for (int j = 0; j < row; j++) {
40+
if (k < s.length()) {
41+
arr[j][i] = s.charAt(k);
42+
k++;
43+
} else {
44+
arr[j][i] = ' ';
45+
}
46+
}
47+
}
48+
}
49+
50+
void encrypt(int row, int column) {
51+
encrypt = new char[row][column];
52+
for (int i = 0; i < column; i++) {
53+
for (int j = 0; j < column; j++) {
54+
if (keya[i] == keytemp[j]) {
55+
for (int k = 0; k < row; k++) {
56+
encrypt[k][j] = arr[k][i];
57+
}
58+
keytemp[j] = '?';
59+
break;
60+
}
61+
}
62+
}
63+
}
64+
65+
void decrypt(int row, int column) {
66+
decrypt = new char[row][column];
67+
for (int i = 0; i < column; i++) {
68+
for (int j = 0; j < column; j++) {
69+
if (keya[j] == keytemp[i]) {
70+
for (int k = 0; k < row; k++) {
71+
decrypt[k][j] = arr[k][i];
72+
}
73+
keya[j] = '?';
74+
break;
75+
}
76+
}
77+
}
78+
}
79+
80+
void resultEncrypt(int row, int column, char arr[][]) {
81+
System.out.println("Result:");
82+
for (int i = 0; i < column; i++) {
83+
for (int j = 0; j < row; j++) {
84+
System.out.print(arr[j][i]);
85+
}
86+
}
87+
}
88+
89+
void resultDecrypt(int row, int column, char arr[][]) {
90+
System.out.println("Result:");
91+
for (int i = 0; i < row; i++) {
92+
for (int j = 0; j < column; j++) {
93+
System.out.print(arr[i][j]);
94+
}
95+
}
96+
}
97+
98+
public static void main(String args[]) throws IOException {
99+
int row, column, choice;
100+
Columnar obj = new Columnar();
101+
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
102+
System.out.println("Menu:\n1) Encryption\n2) Decryption");
103+
choice = Integer.parseInt(in.readLine());
104+
System.out.println("Enter the string:");
105+
String s = in.readLine();
106+
System.out.println("Enter the key:");
107+
String key = in.readLine();
108+
row = s.length() / key.length();
109+
if (s.length() % key.length() != 0)
110+
row++;
111+
column = key.length();
112+
if (choice == 1) {
113+
obj.createMatrixEncrypt(s, key, row, column);
114+
obj.createkey(key, column);
115+
obj.encrypt(row, column);
116+
obj.resultEncrypt(row, column, obj.encrypt);
117+
} else {
118+
obj.createMatrixDecrypt(s, key, row, column);
119+
obj.createkey(key, column);
120+
obj.decrypt(row, column);
121+
obj.resultDecrypt(row, column, obj.decrypt);
122+
}
123+
124+
}
125+
}

0 commit comments

Comments
 (0)