-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
389 lines (349 loc) · 16.3 KB
/
Copy pathMain.java
File metadata and controls
389 lines (349 loc) · 16.3 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import java.io.IOException;
import java.util.Scanner;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
private static final DES des = new DES();
private static final FileProcessor fileProcessor = new FileProcessor();
public static void main(String[] args) {
while (true) {
showMenu();
String choice = scanner.nextLine().trim();
switch (choice) {
case "1": keyExpansion(); break;
case "2": encryptText(); break;
case "3": decryptText(); break;
case "4": encryptFile(); break;
case "5": decryptFile(); break;
case "6": sendEmail(); break;
case "7": sendSMS(); break;
case "8": generateRandomKey(); break;
case "9":
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid option. Please try again.");
}
}
}
private static void showMenu() {
System.out.println("\n===== Enhanced DES Secure Communication System =====");
System.out.println("1. Key Expansion");
System.out.println("2. Encrypt Text");
System.out.println("3. Decrypt Text");
System.out.println("4. Encrypt File");
System.out.println("5. Decrypt File");
System.out.println("6. Send Ciphertext via Email");
System.out.println("7. Send DES Key via SMS");
System.out.println("8. Generate Random DES Key");
System.out.println("9. Exit");
System.out.print("Enter your choice: ");
}
private static int[] getKeyFromUser() {
System.out.println("--- Key Input ---");
System.out.println("1. Enter key as hex (16 hex chars)");
System.out.println("2. Enter key as text (up to 8 chars)");
System.out.println("3. Read key from file (hex)");
System.out.println("4. Read key from file (text)");
System.out.print("Choice: ");
String opt = scanner.nextLine().trim();
while (true) {
try {
switch (opt) {
case "1":
System.out.print("Enter key (hex, 16 hex chars): ");
String hex = scanner.nextLine().trim();
return validateHexKey(hex);
case "2":
System.out.print("Enter key (text, up to 8 chars): ");
String text = scanner.nextLine();
return DataUtils.stringToKeyBits(text);
case "3":
System.out.print("Enter file path: ");
String path = scanner.nextLine().trim();
String content = FileProcessor.readString(path);
return validateHexKey(content);
case "4":
System.out.print("Enter file path: ");
String fpath = scanner.nextLine().trim();
String fcontent = FileProcessor.readString(fpath);
return DataUtils.stringToKeyBits(fcontent);
default:
System.out.print("Invalid. Try again: ");
opt = scanner.nextLine().trim();
}
} catch (Exception e) {
System.out.print("Error: " + e.getMessage() + ". Try again: ");
opt = scanner.nextLine().trim();
}
}
}
private static int[] validateHexKey(String hex) {
hex = hex.replaceAll("\\s+", "");
if (hex.length() != 16) {
throw new IllegalArgumentException("Hex key must be exactly 16 hex characters (64 bits)");
}
int[] key = DataUtils.hexToKeyBits(hex);
if (key.length != 64) {
throw new IllegalArgumentException("Invalid key length");
}
return key;
}
private static int[] getDataFromUser(boolean expectHex) {
System.out.println("--- Data Input ---");
System.out.println("1. Enter from console");
System.out.println("2. Read from file");
System.out.print("Choice: ");
String opt = scanner.nextLine().trim();
try {
switch (opt) {
case "1":
if (expectHex) {
System.out.print("Enter hex data: ");
String hex = scanner.nextLine().trim();
return DataUtils.hexToBits(hex);
} else {
System.out.print("Enter text: ");
String text = scanner.nextLine();
return DataUtils.stringToBits(text);
}
case "2":
System.out.print("Enter file path: ");
String path = scanner.nextLine().trim();
byte[] fileData = FileProcessor.readAllBytes(path);
if (fileData.length % 8 != 0)
return DataUtils.bytesToBits(fileData);
default:
System.out.println("Invalid option.");
return null;
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
return null;
}
}
private static void keyExpansion() {
System.out.println("\n--- Key Expansion ---");
int[] key = getKeyFromUser();
if (key == null) return;
System.out.println("\nOriginal Key (64 bits):");
System.out.println(" Binary: " + DataUtils.bitsToBinaryString(key));
System.out.println(" Hex: " + DataUtils.bitsToHex(key));
int[] pc1Result = des.getPC1Result(key);
System.out.println("\nAfter PC-1 (56 bits):");
System.out.println(" Binary: " + DataUtils.bitsToBinaryString(pc1Result));
System.out.println(" Hex: " + DataUtils.bitsToHex(pc1Result));
int[] c = des.splitLeft(pc1Result, 28);
int[] d = des.splitRight(pc1Result, 28);
System.out.println("\nC0 (28 bits): " + DataUtils.bitsToBinaryString(c));
System.out.println("D0 (28 bits): " + DataUtils.bitsToBinaryString(d));
int[][] roundKeys = des.generateRoundKeys(key);
System.out.println("\n--- 16 Round Subkeys ---");
for (int r = 0; r < 16; r++) {
c = des.circularShift(c, DES.getSHIFTS()[r]);
d = des.circularShift(d, DES.getSHIFTS()[r]);
System.out.println("\nRound " + (r + 1) + ":");
System.out.println(" C" + (r + 1) + " (28 bits): " + DataUtils.bitsToBinaryString(c));
System.out.println(" D" + (r + 1) + " (28 bits): " + DataUtils.bitsToBinaryString(d));
System.out.println(" K" + (r + 1) + " (48 bits): " + DataUtils.bitsToBinaryString(roundKeys[r]));
System.out.println(" K" + (r + 1) + " (hex): " + DataUtils.bitsToHex(roundKeys[r]));
}
}
private static void encryptText() {
System.out.println("\n--- Encrypt Text ---");
int[] key = getKeyFromUser();
if (key == null) return;
System.out.println("Input format:");
System.out.println("1. Plain text");
System.out.println("2. Hexadecimal");
System.out.print("Choice: ");
String fmt = scanner.nextLine().trim();
int[] dataBits = getDataFromUser("2".equals(fmt));
if (dataBits == null || dataBits.length == 0) return;
int[] padded = DataUtils.padBytes(DataUtils.bitsToBytes(dataBits));
int numBlocks = padded.length / 64;
byte[] cipherBytes = new byte[numBlocks * 8];
for (int i = 0; i < numBlocks; i++) {
int[] block = new int[64];
DES.copy(padded, i * 64, block, 0, 64);
int[] encBlock = des.encrypt(block, key);
byte[] encBytes = DataUtils.bitsToBytes(encBlock);
for (int j = 0; j < 8; j++) cipherBytes[i * 8 + j] = encBytes[j];
}
System.out.println("\nCiphertext (hex): " + DataUtils.bytesToHex(cipherBytes));
}
private static void decryptText() {
System.out.println("\n--- Decrypt Text ---");
int[] key = getKeyFromUser();
if (key == null) return;
System.out.println("Input format:");
System.out.println("1. Hexadecimal");
System.out.println("2. From file");
System.out.print("Choice: ");
String fmt = scanner.nextLine().trim();
if ("2".equals(fmt)) {
System.out.print("Enter file path: ");
try {
String path = scanner.nextLine().trim();
byte[] fileData = FileProcessor.readAllBytes(path);
if (fileData.length % 8 != 0) {
System.out.println("Error: Invalid ciphertext length.");
return;
}
int numBlocks = fileData.length / 8;
byte[] decrypted = new byte[fileData.length];
for (int i = 0; i < numBlocks; i++) {
byte[] block = new byte[8];
for (int j = 0; j < 8; j++) block[j] = fileData[i * 8 + j];
int[] decBlock = des.decrypt(DataUtils.bytesToBits(block), key);
byte[] decBytes = DataUtils.bitsToBytes(decBlock);
for (int j = 0; j < 8; j++) decrypted[i * 8 + j] = decBytes[j];
}
byte[] unpadded = DataUtils.unpadBytes(decrypted);
System.out.println("\nDecrypted text: " + new String(unpadded));
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
return;
}
System.out.print("Enter ciphertext (hex): ");
String hexCipher = scanner.nextLine().trim().replaceAll("\\s+", "");
try {
byte[] cipherBytes = DataUtils.hexToBytes(hexCipher);
if (cipherBytes.length % 8 != 0) {
System.out.println("Error: Invalid ciphertext length.");
return;
}
int numBlocks = cipherBytes.length / 8;
byte[] decrypted = new byte[cipherBytes.length];
for (int i = 0; i < numBlocks; i++) {
byte[] block = new byte[8];
for (int j = 0; j < 8; j++) block[j] = cipherBytes[i * 8 + j];
int[] decBlock = des.decrypt(DataUtils.bytesToBits(block), key);
byte[] decBytes = DataUtils.bitsToBytes(decBlock);
for (int j = 0; j < 8; j++) decrypted[i * 8 + j] = decBytes[j];
}
byte[] unpadded = DataUtils.unpadBytes(decrypted);
System.out.println("\nDecrypted text: " + new String(unpadded));
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
private static void encryptFile() {
System.out.println("\n--- Encrypt File ---");
int[] key = getKeyFromUser();
if (key == null) return;
System.out.print("Enter input file path: ");
String inputPath = scanner.nextLine().trim();
String outputPath = fileProcessor.getEncryptedOutputPath(inputPath);
try {
fileProcessor.encryptFile(inputPath, outputPath, key);
System.out.println("File encrypted successfully.");
System.out.println("Output: " + outputPath);
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
private static void decryptFile() {
System.out.println("\n--- Decrypt File ---");
int[] key = getKeyFromUser();
if (key == null) return;
System.out.print("Enter encrypted file path: ");
String inputPath = scanner.nextLine().trim();
String outputPath = fileProcessor.getDecryptedOutputPath(inputPath);
try {
fileProcessor.decryptFile(inputPath, outputPath, key);
System.out.println("File decrypted successfully.");
System.out.println("Output: " + outputPath);
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
private static void sendEmail() {
System.out.println("\n--- Send Ciphertext via Email ---");
System.out.print("Recipient email: ");
String recipient = scanner.nextLine().trim();
System.out.println("Send:");
System.out.println("1. Ciphertext from console");
System.out.println("2. Encrypted file");
System.out.print("Choice: ");
String opt = scanner.nextLine().trim();
try {
String body;
String subject = "Encrypted DES Ciphertext";
if ("2".equals(opt)) {
System.out.print("Enter file path: ");
String path = scanner.nextLine().trim();
byte[] data = FileProcessor.readAllBytes(path);
body = "Encrypted file contents (hex):\n" + DataUtils.bytesToHex(data);
} else {
System.out.print("Enter ciphertext (hex): ");
body = "Ciphertext:\n" + scanner.nextLine().trim();
}
EmailService.sendEmail(recipient, subject, body);
System.out.println("Email sent successfully.");
} catch (Exception e) {
System.out.println("Email sending failed: " + e.getMessage());
}
}
private static void sendSMS() {
System.out.println("\n--- Send DES Key via SMS ---");
int[] key;
System.out.println("Key source:");
System.out.println("1. Use existing key");
System.out.println("2. Generate new key");
System.out.print("Choice: ");
String opt = scanner.nextLine().trim();
if ("2".equals(opt)) {
key = DataUtils.generateRandomKey();
System.out.println("Generated key (hex): " + DataUtils.bitsToHex(key));
} else {
key = getKeyFromUser();
if (key == null) return;
}
System.out.print("Phone number (with country code, e.g. +1234567890): ");
String phone = scanner.nextLine().trim();
try {
String keyHex = DataUtils.bitsToHex(key);
String keyBin = DataUtils.bitsToBinaryString(key);
String content = "(h): " + keyHex + " | (b): " + keyBin;
FileProcessor.writeString("des_key.txt", content);
System.out.println("Key saved to des_key.txt");
String sid = SMSService.sendSMS(phone, "Your DES Secret Key: " + keyHex);
System.out.println("SMS sent successfully! SID: " + sid);
} catch (Exception e) {
System.out.println("SMS sending failed: " + e.getMessage());
}
}
private static void generateRandomKey() {
System.out.println("\n--- Generate Random DES Key ---");
int[] key = DataUtils.generateRandomKey();
System.out.println("Generated DES Key:");
System.out.println(" Hex: " + DataUtils.bitsToHex(key));
System.out.println(" Binary: " + DataUtils.bitsToBinaryString(key));
System.out.print("Use this key for encryption? (y/n): ");
String ans = scanner.nextLine().trim().toLowerCase();
if (ans.equals("y")) {
System.out.println("--- Encrypt Text with Generated Key ---");
System.out.println("Input format:");
System.out.println("1. Plain text");
System.out.println("2. Hexadecimal");
System.out.print("Choice: ");
String fmt = scanner.nextLine().trim();
int[] dataBits = getDataFromUser("2".equals(fmt));
if (dataBits == null || dataBits.length == 0) return;
int[] padded = DataUtils.padBytes(DataUtils.bitsToBytes(dataBits));
int numBlocks = padded.length / 64;
byte[] cipherBytes = new byte[numBlocks * 8];
for (int i = 0; i < numBlocks; i++) {
int[] block = new int[64];
DES.copy(padded, i * 64, block, 0, 64);
int[] encBlock = des.encrypt(block, key);
byte[] encBytes = DataUtils.bitsToBytes(encBlock);
for (int j = 0; j < 8; j++) cipherBytes[i * 8 + j] = encBytes[j];
}
System.out.println("\nCiphertext (hex): " + DataUtils.bytesToHex(cipherBytes));
}
}
}