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