-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnupama-Cryptography.java
More file actions
225 lines (177 loc) · 5.96 KB
/
Anupama-Cryptography.java
File metadata and controls
225 lines (177 loc) · 5.96 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import java.util.*;
import java.awt.Point;
public class main {
static String str1;
static String str2;
static String str3;
static String str4;
static String str5;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter line 1 of encoded message : ");
String str1 = s.nextLine();
System.out.print("Enter line 2 of encoded message : ");
String str2 = s.nextLine();
System.out.print("Enter line 3 of encoded message : ");
String str3 = s.nextLine();
System.out.print("Enter line 4 of encoded message : ");
str4 = s.nextLine();
System.out.print("Enter line 5 of encoded message : ");
str5 = s.nextLine();
System.out.println("\n\nDecoded line 5 : "+e5(str5)+"\n\n");
System.out.println("\nDecoded line 4 : "+e4(str4)+"\n\n");
System.out.println("\nDecoded line 3 : "+e3(str3)+"\n\n");
System.out.println("\nDecoded line 2 : "+e2(str2)+"\n\n");
System.out.println("\nDecoded line 1 : "+e1(str1)+"\n\n");
}
public static String e5(String str)
{
Base64.Decoder decoder = Base64.getDecoder();
String dStr = new String(decoder.decode(str));
// System.out.println("Decoded string: "+dStr);
return dStr;
}
public static String e4(String str){
String message, decryptedMessage = "";
int key;
char ch;
message = e5(str);
key = 13;
for(int i = 0; i < message.length(); ++i){
ch = message.charAt(i);
if(ch >= 'a' && ch <= 'z'){
ch = (char)(ch - key);
if(ch < 'a'){
ch = (char)(ch + 'z' - 'a' + 1);
}
decryptedMessage += ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = (char)(ch - key);
if(ch < 'A'){
ch = (char)(ch + 'Z' - 'A' + 1);
}
decryptedMessage += ch;
}
else {
decryptedMessage += ch;
}
}
return decryptedMessage;
}
static int key_e3=0;
public static String e3(String s){
String message, decryptedMessage = "";
char ch;
Scanner sc = new Scanner(System.in);
message = e4(s);
if(key_e3==0)
{
System.out.println("Enter key for e3: ");
key_e3 = sc.nextInt();
}
for(int i = 0; i < message.length(); ++i){
ch = message.charAt(i);
if(ch >= 'a' && ch <= 'z'){
ch = (char)(ch - key_e3);
if(ch < 'a'){
ch = (char)(ch + 'z' - 'a' + 1);
}
decryptedMessage += ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = (char)(ch - key_e3);
if(ch < 'A'){
ch = (char)(ch + 'Z' - 'A' + 1);
}
decryptedMessage += ch;
}
else {
decryptedMessage += ch;
}
}
return decryptedMessage;
}
static String key_e2="";
public static String e2(String Message) {
Scanner in = new Scanner(System.in);
String str = e3(Message);
if(key_e2=="")
{
System.out.print("Enter the key for e2: ");
key_e2 = in.next();
key_e2 = key_e2.toUpperCase();
}
String DMessage = "";
str = str.toUpperCase();
for (int i = 0, j = 0; i < str.length(); i++) {
char letter = str.charAt(i);
DMessage += (char)((letter - key_e2.charAt(j) + 26) % 26 + 65);
j = ++j % key_e2.length();
}
return DMessage;
}
private static char[][] charTable;
private static Point[] positions;
static String key_e1 = "";
public static String e1(String str) {
Scanner sc = new Scanner(System.in);
String txt = e2(str);
if(key_e1=="")
{
System.out.print("Enter an encryption key of e1: ");
key_e1 = sc.nextLine();
}
System.out.print("Replace J with I? y/n: ");
String jti = sc.nextLine();
boolean changeJtoI = jti.equalsIgnoreCase("y");
createTable(key_e1, changeJtoI);
return decode(txt);
}
private static String prepareText(String s, boolean changeJtoI) {
s = s.toUpperCase().replaceAll("[^A-Z]", "");
return changeJtoI ? s.replace("J", "I") : s.replace("Q", "");
}
private static void createTable(String key, boolean changeJtoI) {
charTable = new char[5][5];
positions = new Point[26];
String s = prepareText(key + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", changeJtoI);
int len = s.length();
for (int i = 0, k = 0; i < len; i++) {
char c = s.charAt(i);
if (positions[c - 'A'] == null) {
charTable[k / 5][k % 5] = c;
positions[c - 'A'] = new Point(k % 5, k / 5);
k++;
}
}
}
private static String decode(String s) {
return codec(new StringBuilder(s), 4);
}
private static String codec(StringBuilder text, int direction) {
int len = text.length();
for (int i = 0; i < len; i += 2) {
char a = text.charAt(i);
char b = text.charAt(i + 1);
int row1 = positions[a - 'A'].y;
int row2 = positions[b - 'A'].y;
int col1 = positions[a - 'A'].x;
int col2 = positions[b - 'A'].x;
if (row1 == row2) {
col1 = (col1 + direction) % 5;
col2 = (col2 + direction) % 5;
} else if (col1 == col2) {
row1 = (row1 + direction) % 5;
row2 = (row2 + direction) % 5;
} else {
int tmp = col1;
col1 = col2;
col2 = tmp;
}
text.setCharAt(i, charTable[row1][col1]);
text.setCharAt(i + 1, charTable[row2][col2]);
}
return text.toString();
}
}