-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
87 lines (72 loc) · 2.99 KB
/
Copy pathmain.java
File metadata and controls
87 lines (72 loc) · 2.99 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
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.Scanner;
public class CipherMachine {
public static final String SEQ1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String SEQ2 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
public static Scanner inp = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please enter the key");
String key = String.valueOf(inp.next().charAt(0)).toUpperCase();
System.out.println("Do you want to encrypt or decrypt?");
String option = String.valueOf(inp.next().charAt(0)).toUpperCase();
inp.nextLine();
switch (option) {
case "E":
encryption(SEQ1.indexOf(key));
break;
case "D":
decryption(SEQ1.indexOf(key));
break;
default:
System.out.println("That is not a valid option.");
break;
}
}
public static void encryption(int shift) {
System.out.println("Please enter the message you want to encrypt");
String message = inp.nextLine();
String result = "";
int SEQ1Index;
int SEQ2Index;
for (int n = 0; n < message.length(); n++) {
SEQ1Index = SEQ1.indexOf(message.charAt(n));
SEQ2Index = SEQ2.indexOf(message.charAt(n));
if (SEQ1Index != -1) {
result += SEQ1.charAt(SEQ1Index + shift);
} else if (SEQ2Index != -1) {
result += SEQ2.charAt(SEQ2Index + shift);
} else {
result += message.charAt(n);
}
}
System.out.println("\n" + result + "\n(copied to clipboard)");
copyToClipboard(result);
}
public static void decryption(int shift) {
System.out.println("Please enter the message you want to decrypt");
String message = inp.nextLine();
String result = "";
int SEQ1Index;
int SEQ2Index;
for (int n = 0; n < message.length(); n++) {
SEQ1Index = SEQ1.lastIndexOf(message.charAt(n));
SEQ2Index = SEQ2.lastIndexOf(message.charAt(n));
if (SEQ1Index != -1) {
result += SEQ1.charAt(SEQ1Index - shift);
} else if (SEQ2Index != -1) {
result += SEQ2.charAt(SEQ2Index - shift);
} else {
result += message.charAt(n);
}
}
System.out.println("\n" + result + "\n(copied to clipboard)");
copyToClipboard(result);
}
public static void copyToClipboard(String result) {
StringSelection selection = new StringSelection(result);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
}
}