-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalTranpositionCipher.java
More file actions
145 lines (124 loc) · 4.61 KB
/
DiagonalTranpositionCipher.java
File metadata and controls
145 lines (124 loc) · 4.61 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
136
137
138
139
140
141
142
143
144
145
import java.util.Scanner;
import java.util.Arrays;
class DiagonalTranspositionCipher {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter plaintext:");
String plainText = scanner.nextLine();
System.out.println("Key:");
String key = scanner.nextLine();
String cipherText = encrypt(plainText, key);
System.out.println("The ciphertext is: " + cipherText);
String decryptedText=decrypt(cipherText,key);
System.out.println("Plaintext is: "+decryptedText);
}
public static String encrypt(String plainText, String key) {
key = key.replaceAll("\\s+", "");
int col = key.length();
int row = (plainText.length() + key.length() - 1) / key.length();
char[][] cipherMatrix = new char[row][col];
int k = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (k < plainText.length() && i == j) {
cipherMatrix[i][j] = plainText.charAt(k++);
} else {
cipherMatrix[i][j] = 'x';
}
}
}
for (int i = 1; i < row; i++) {
for (int j = i; j < col; j++) {
if((j-i)<row && j<col){
if (k < plainText.length()) {
cipherMatrix[j - i][j] = plainText.charAt(k++);
} else {
cipherMatrix[j - i][j] = 'x';
}
}}
}
for (int i = 1; i < row; i++) {
for (int j = i; j < row; j++) {
if ((j - i )< col && j<row) {
if (k < plainText.length()) {
cipherMatrix[j][j - i] = plainText.charAt(k++);
} else {
cipherMatrix[j][j - i] = 'x';
}
}
}
}
char[] keySort = key.toCharArray();
Arrays.sort(keySort);
int[] indexSort = new int[key.length()];
for (int i = 0; i < key.length(); i++) {
indexSort[i] = key.indexOf(keySort[i]);
}
StringBuilder ciphertext = new StringBuilder();
int direction = 0;
for (int c = 0; c < key.length(); c++) {
int colum = indexSort[c];
int rowCount = Math.min(plainText.length() - colum, row);
if (direction % 2 == 1) {
for (int r = rowCount - 1; r >= 0; r--) {
ciphertext.append(cipherMatrix[r][colum]);
}
} else {
for (int r = 0; r < rowCount; r++) {
ciphertext.append(cipherMatrix[r][colum]);
}
}
direction++;
}
return ciphertext.toString();
}
public static String decrypt(String cipherText, String key) {
key = key.replaceAll("\\s+", "");
int col = key.length();
int row = (cipherText.length() + key.length() - 1) / key.length();
char[][] cipherMatrix = new char[row][col];
char[] keySort = key.toCharArray();
Arrays.sort(keySort);
int[] indexSort = new int[key.length()];
for (int i = 0; i < key.length(); i++) {
indexSort[i] = key.indexOf(keySort[i]);
}
StringBuilder decryptedText = new StringBuilder();
int index=0;
int direction = 0;
for (int c = 0; c < key.length(); c++) {
int column = indexSort[c];
int rowCount = Math.min(cipherText.length() - column, row);
if (direction % 2 == 1) {
for (int r = rowCount - 1; r >= 0; r--) {
cipherMatrix[r][column]=cipherText.charAt(index++);
}
} else {
for (int r = 0; r < rowCount; r++) {
cipherMatrix[r][column]=cipherText.charAt(index++);
}
}
direction++;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if(i==j) {
decryptedText.append(cipherMatrix[i][j]);
}
}
}
for (int i = 1; i < row; i++) {
for (int j = i; j < col; j++) {
if((j-i)<row && j<col){
decryptedText.append(cipherMatrix[j - i][j]);
}}
}
for (int i = 1; i < row; i++) {
for (int j = i; j < row; j++) {
if((j-i)<col && j<row){
decryptedText.append(cipherMatrix[j][j - i]);
}}
}
return decryptedText.toString();
}
}