-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneTimePad.java
More file actions
119 lines (94 loc) · 3.12 KB
/
OneTimePad.java
File metadata and controls
119 lines (94 loc) · 3.12 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
import java.util.Random;
import java.util.Scanner;
import sun.net.www.content.text.plain;
public class OneTimePad {
private static String plainText = "";
private static String key = "";
private static String encryptedMessage = "";
private static int messageSize;
private static Scanner scanner;
private static String option;
public static String encrypt (String plainText, String key) {
String encryptedMessage = "";
char letter;
int index;
for (int i =0 ; i < plainText.length(); i++) {
index = plainText.toCharArray()[i] + key.toCharArray()[i];
if (index <26) {
index= index;
} else {
index = index%26;
}
letter = (char) ('A' + index);
System.out.println(
plainText.toCharArray()[i] +
" + " + key.toCharArray()[i] +
" = " + index + " ~ " +
letter);
encryptedMessage += letter;
}
return encryptedMessage;
}
public static String decrypt (String plainText, String key) {
String decryptedMessage = "";
char letter;
int index;
for (int i =0 ; i < plainText.length(); i++) {
index = plainText.toCharArray()[i] - key.toCharArray()[i];
if (index > 0) {
index = index;
} else {
index +=26;
}
if (index == 26) {
index = 0;
}
letter = (char) ('A' + index);
System.out.println(
plainText.toCharArray()[i] +
" - " + key.toCharArray()[i] +
" = " + index + " ~ " +
letter);
decryptedMessage += letter;
}
return decryptedMessage;
}
public static void main (String[] args) {
System.out.println("Welcome");
scanner = new Scanner (System.in);
Random random = new Random();
System.out.println("Enter your message:");
plainText = scanner.nextLine();
plainText = plainText.replaceAll("\\s","").trim().toUpperCase();
plainText = plainText.replace("1", "ONE");
plainText = plainText.replace("2", "TWO");
plainText = plainText.replace("3", "THREE");
plainText = plainText.replace("4", "FOUR");
plainText = plainText.replace("5", "FIVE");
plainText = plainText.replace("6", "SIX");
plainText = plainText.replace("7", "SEVEN");
plainText = plainText.replace("8", "EIGHT");
plainText = plainText.replace("9", "NINE");
plainText = plainText.replace("0", "ZERO");
messageSize = plainText.length();
System.out.println("The modified message is: " + plainText);
System.out.println("Enter option");
option = scanner.nextLine();
if (option.trim().toLowerCase().equals("encrypt")) {
for (int i =0; i< messageSize; i++) {
key += (char) ('A' + random.nextInt(26));
}
System.out.println("The key is:" + key);
encryptedMessage = encrypt(plainText, key);
System.out.println("The encrypted message is: " + encryptedMessage);
} else if (option.trim().toLowerCase().equals
("decrypt")) {
System.out.println("Eneter key");
key = scanner.nextLine();
encryptedMessage = decrypt (plainText, key);
System.out.println("The source text is: " + encryptedMessage);
} else {
System.out.println("Unknown command");
}
}
}