forked from scr34m0/Java-Cryptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColumnarCipher.java
More file actions
135 lines (125 loc) · 3.13 KB
/
ColumnarCipher.java
File metadata and controls
135 lines (125 loc) · 3.13 KB
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
126
127
128
129
130
131
132
133
134
135
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author arthur.paixao
* @category Columnar Cipher Encode/Decode
*/
public class ColumnarCipher {
char arr[][], encrypt[][], decrypt[][], keya[], keytemp[];
public void creatematrixE(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] = ' ';
}
}
}
}
public 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;
}
}
}
}
public void creatematrixD(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] = ' ';
}
}
}
}
public 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;
}
}
}
}
public 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;
}
}
}
}
public void resultE(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]);
}
}
}
public void resultD(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;
ColumnarCipher obj = new ColumnarCipher();
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();
switch (choice) {
case 1:
obj.creatematrixE(s, key, row, column);
obj.createkey(key, column);
obj.encrypt(row, column);
obj.resultE(row, column, obj.encrypt);
break;
case 2:
obj.creatematrixD(s, key, row, column);
obj.createkey(key, column);
obj.decrypt(row, column);
obj.resultD(row, column, obj.decrypt);
break;
}
}
}