- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathColumnarTranspositionCipher.java
More file actions
Latest commit
122 lines (100 loc) · 3.76 KB
/
Copy pathColumnarTranspositionCipher.java
File metadata and controls
122 lines (100 loc) · 3.76 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
importjava.util.HashMap;
importjava.util.Map;
publicclassColumnarTranspositionCipher {
// Key for Columnar Transposition
privatestaticfinalStringkey = "HACK";
privatestaticfinalMap<Character, Integer> keyMap = newHashMap<>();
privatestaticvoidsetPermutationOrder() {
// Add the permutation order into the map
for (inti = 0; i < key.length(); i++) {
keyMap.put(key.charAt(i), i);
}
}
// Encryption
privatestaticStringencryptMessage(Stringmsg) {
introw, col, j;
StringBuildercipher = newStringBuilder();
// Calculate the number of columns in the matrix
col = key.length();
// Calculate the maximum number of rows in the matrix
row = msg.length() / col;
if (msg.length() % col != 0) {
row += 1;
}
char[][] matrix = newchar[row][col];
for (inti = 0, k = 0; i < row; i++) {
for (j = 0; j < col;) {
if (msg.charAt(k) == '\0') {
// Adding the padding character '_'
matrix[i][j] = '_';
j++;
}
if (Character.isAlphabetic(msg.charAt(k)) || msg.charAt(k) == ' ') {
// Adding only space and alphabets into the matrix
matrix[i][j] = msg.charAt(k);
j++;
}
k++;
}
}
for (Map.Entry<Character, Integer> entry : keyMap.entrySet()) {
j = entry.getValue();
// Getting ciphertext from the matrix column-wise using the permuted key
for (inti = 0; i < row; i++) {
if (Character.isAlphabetic(matrix[i][j]) || matrix[i][j] == ' ' || matrix[i][j] == '_') {
cipher.append(matrix[i][j]);
}
}
}
returncipher.toString();
}
// Decryption
privatestaticStringdecryptMessage(Stringcipher) {
// Calculate the number of rows and columns for the cipher matrix
intcol = key.length();
introw = cipher.length() / col;
char[][] cipherMat = newchar[row][col];
// Add characters into the matrix column-wise
intk = 0;
for (intj = 0; j < col; j++) {
for (inti = 0; i < row; i++) {
cipherMat[i][j] = cipher.charAt(k++);
}
}
// Update the order of the key for decryption
intindex = 0;
for (Map.Entry<Character, Integer> entry : keyMap.entrySet()) {
entry.setValue(index++);
}
// Arrange the matrix column-wise according to the permutation order by adding
// into a new matrix
char[][] decCipher = newchar[row][col];
intl = 0, j;
for (; l < key.length();) {
j = keyMap.get(key.charAt(l++));
for (inti = 0; i < row; i++) {
decCipher[i][j] = cipherMat[i][l - 1];
}
}
// Get the original message from the matrix
StringBuildermsg = newStringBuilder();
for (inti = 0; i < row; i++) {
for (intj1 = 0; j1 < col; j1++) {
if (decCipher[i][j1] != '_') {
msg.append(decCipher[i][j1]);
}
}
}
returnmsg.toString();
}
publicstaticvoidmain(String[] args) {
Stringmsg = "Open Source is Fun";
setPermutationOrder();
// Calling encryption function
Stringcipher = encryptMessage(msg);
System.out.println("Encrypted Message: " + cipher);
// Calling Decryption function
StringdecryptedMessage = decryptMessage(cipher);
System.out.println("Decrypted Message: " + decryptedMessage);
}
}