-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptionProgram.java
More file actions
258 lines (214 loc) · 10.6 KB
/
EncryptionProgram.java
File metadata and controls
258 lines (214 loc) · 10.6 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
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Scanner;
import javax.crypto.*;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class EncryptionProgram {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Encryption Program");
System.out.println("------------------");
System.out.println("1. AES");
System.out.println("2. 3DES");
System.out.println("3. OTP");
System.out.print("Choose an encryption algorithm (1 OR 2 OR 3): ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
aesOptions(scanner);
break;
case 2:
des3Options(scanner);
break;
case 3:
otpOptions(scanner);
break;
default:
System.out.println("Invalid choice. Exiting program.");
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void aesOptions(Scanner scanner) {
System.out.println("AES Encryption/Decryption Program");
System.out.println("---------------------------------");
System.out.println("1. Encrypt");
System.out.println("2. Decrypt");
System.out.print("Enter your choice (1 or 2): ");
int choice = scanner.nextInt();
scanner.nextLine();
if (choice == 1) {
System.out.print("Enter the plain message: ");
String plainMessage = scanner.nextLine();
System.out.print("Enter the encryption key (16, 24, or 32 characters): ");
String encryptionKey = scanner.nextLine();
if (encryptionKey.length()==16|| encryptionKey.length()==24|| encryptionKey.length()==32){
String encryptedText = aesEncrypt(plainMessage, encryptionKey);
System.out.println("Encrypted Text: " + encryptedText);
}
else{
System.out.println("yuor key length wrong");
}
} else if (choice == 2) {
System.out.print("Enter the encrypted text: ");
String encryptedText = scanner.nextLine();
System.out.print("Enter the decryption key (16, 24, or 32 characters): ");
String decryptionKey = scanner.nextLine();
String decryptedMessage = aesDecrypt(encryptedText, decryptionKey);
System.out.println("Decrypted Message: " + decryptedMessage);
} else {
System.out.println("Invalid choice. Please enter 1 or 2.");
}
}
private static void otpOptions(Scanner scanner) {
System.out.println("3DES Encryption/Decryption Program");
System.out.println("----------------------------------");
System.out.println("1. Encrypt");
System.out.println("2. Decrypt");
System.out.print("Enter your choice (1 or 2): ");
int choice = scanner.nextInt();
scanner.nextLine();
if (choice == 1) {
System.out.print("Enter the plain message: ");
String plainMessage = scanner.nextLine();
System.out.print("Enter the encryption key equal or greater than plain text ");
String encryptionKey = scanner.nextLine();
if (plainMessage.length() > encryptionKey.length()) {
System.out.println("Error: The length of the message must less than or equal to the key length.");
}
else{
String encryptedText = otpEncrypt(plainMessage, encryptionKey);
System.out.println("Encrypted Text: " + encryptedText);
}
} else if (choice == 2) {
System.out.print("Enter the encrypted text: ");
String encryptedText = scanner.nextLine();
System.out.print("Enter the decryption key: ");
String decryptionKey = scanner.nextLine();
String decryptedMessage = otpDecrypt(encryptedText, decryptionKey);
System.out.println("Decrypted Message: " + decryptedMessage);
} else {
System.out.println("Invalid choice. Please enter 1 or 2 or 3.");
}
}
private static void des3Options(Scanner scanner) {
System.out.println("3DES Encryption/Decryption Program");
System.out.println("----------------------------------");
System.out.println("1. Encrypt");
System.out.println("2. Decrypt");
System.out.print("Enter your choice (1 or 2): ");
int choice = scanner.nextInt();
scanner.nextLine();
if (choice == 1) {
System.out.print("Enter the plain message: ");
String plainMessage = scanner.nextLine();
System.out.print("Enter the first encryption key (24 characters): ");
String encryptionKeyone = scanner.nextLine();
System.out.print("Enter the second encryption key (24 characters): ");
String encryptionKeytwo = scanner.nextLine();
System.out.print("Enter the third encryption key (24 characters): ");
String encryptionKeythree = scanner.nextLine();
if (encryptionKeyone.length()==24 && encryptionKeytwo.length() ==24 && encryptionKeythree.length() ==24){
String encryptedText1 = des3Encrypt(plainMessage, encryptionKeyone);
System.out.println("Encrypted Text: " + encryptedText1);
String encryptedText2 = des3Encrypt(plainMessage, encryptionKeytwo);
System.out.println("Encrypted Text: " + encryptedText2);
String encryptedText3 = des3Encrypt(plainMessage, encryptionKeythree);
System.out.println("Encrypted Text: " + encryptedText3);
}
else{
System.out.println("yuor key length wrong");
}
} else if (choice == 2) {
System.out.print("Enter the encrypted text: ");
String encryptedText = scanner.nextLine();
System.out.print("Enter the third encryption key (24 characters): ");
String decryptionKeythree = scanner.nextLine();
System.out.print("Enter the second encryption key (24 characters): ");
String decryptionKeytwo = scanner.nextLine();
System.out.print("Enter the first decryption key (24 characters): ");
String decryptionKeyone = scanner.nextLine();
String decryptedMessage = des3Decrypt(encryptedText, decryptionKeythree);
System.out.println("Decrypted Message1: " + decryptedMessage);
String decryptedMessage2 = des3Decrypt(encryptedText, decryptionKeytwo);
System.out.println("Decrypted Message2: " + decryptedMessage2);
String decryptedMessage3 = des3Decrypt(encryptedText, decryptionKeyone);
System.out.println("Decrypted Message i.e Plain Text is: " + decryptedMessage3);
} else {
System.out.println("Invalid choice. Please enter 1 or 2.");
}
}
public static String aesEncrypt(String plainText, String encryptionKey) {
try {
SecretKeySpec keySpec = new SecretKeySpec(encryptionKey.getBytes(StandardCharsets.UTF_8), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String aesDecrypt(String encryptedText, String decryptionKey) {
try {
SecretKeySpec keySpec = new SecretKeySpec(decryptionKey.getBytes(StandardCharsets.UTF_8), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String des3Encrypt(String plainText, String encryptionKey) {
try {
byte[] keyBytes = encryptionKey.getBytes(StandardCharsets.UTF_8);
DESedeKeySpec keySpec = new DESedeKeySpec(keyBytes);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey secretKey = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String des3Decrypt(String encryptedText, String decryptionKey) {
try {
byte[] keyBytes = decryptionKey.getBytes(StandardCharsets.UTF_8);
DESedeKeySpec keySpec = new DESedeKeySpec(keyBytes);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey secretKey = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String otpEncrypt(String plainMessage, String encryptionKey) {
StringBuilder encryptedText = new StringBuilder();
for (int i = 0; i < plainMessage.length(); i++) {
char plainChar = plainMessage.charAt(i);
char keyChar = encryptionKey.charAt(i % encryptionKey.length());
char encryptedChar = (char) (plainChar ^ keyChar);
encryptedText.append(encryptedChar);
}
return encryptedText.toString();
}
private static String otpDecrypt(String encryptedText, String decryptionKey) {
return otpEncrypt(encryptedText, decryptionKey); // XOR operation is its own inverse
}
}