-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathKeywordCipher.java
More file actions
145 lines (119 loc) · 4.66 KB
/
KeywordCipher.java
File metadata and controls
145 lines (119 loc) · 4.66 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
/*********
-*- Made by VoxelPixel
-*- For YouTube Tutorial
-*- https://github.com/VoxelPixel
-*- Support me on Patreon: https://www.patreon.com/voxelpixel
*********/
import java.util.Scanner;
public class KeywordCipher {
private static Scanner in;
private static String keyword = "";
private static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args){
in = new Scanner(System.in);
KeywordPicker();
System.out.print("1. Encryption\n2. Decryption\nChoose(1,2): ");
int choice = in.nextInt();
in.nextLine();
if (choice == 1){
System.out.println("---Encryption---");
cipherEncryption();
} else if (choice == 2){
System.out.println("---Decryption---");
cipherDecryption();
} else {
System.out.println("Incorrect Choice");
}
}
private static void cipherDecryption() {
System.out.print("Enter message: ");
String message = in.nextLine();
in.nextLine();
message = message.toUpperCase();
String decrypText = "";
for (int i = 0; i < message.length(); i++) {
if(message.charAt(i) == (char)32){
decrypText += " ";
} else {
int counter = 0;
for (int j = 0; j < alpha.length(); j++) {
if(message.charAt(i) == keyword.charAt(j)){
//find character's loaction in keyword variable
//character at that location in alphabet is decrypted letter
decrypText += alpha.charAt(counter);
break;
} else {
counter++;
} // if-else
} // inner for
} //if-else
} // for
System.out.println("Decrypted Text: " + decrypText);
}
private static void cipherEncryption() {
System.out.print("Enter message: ");
String message = in.nextLine();
in.nextLine();
message = message.toUpperCase();
String encrypText = "";
for (int i = 0; i < message.length(); i++) {
if(message.charAt(i) == (char)32){
encrypText += " ";
} else {
int counter = 0;
for (int j = 0; j < alpha.length(); j++) {
if(message.charAt(i) == alpha.charAt(j)){
// character at count location of alphabet is encrypted letter in key
encrypText += keyword.charAt(counter);
break;
} else {
counter++;
} // if-else
} // inner for
} //if-else
} // for
System.out.println("Encrypted Text: " + encrypText);
}
private static void KeywordPicker() {
System.out.println("Keyword must contain each letter A-Z exactly once, no repeating letter");
System.out.print("Enter Keyword: ");
String keywr = in.nextLine();
keywr = keywr.toUpperCase();
in.nextLine();
// checking repetition of letters in keyword
for (int i = 0; i < keywr.length(); i++) {
int pos = i;
for (int j = 0; j < keywr.length(); j++) {
if (pos == j){
//skipping the letter comparison with itself
continue;
} else if(keywr.charAt(i) == keywr.charAt(j)){
System.out.println("Letter \"" + keywr.charAt(i) + "\" Repeating in keyword");
System.exit(0);
} // if-else
} // inner for
} // for
//generating alphabet with keyword
String temp = "";
for (int i = 0; i < keywr.length(); i++) {
temp += keywr.charAt(i);
}
for (int i = 0; i < 26; i++) {
temp += (char)(i+65);
}
//removing letters of keyword from alphabet in temp and adding result in keyword variable
for (int i = 0; i < temp.length(); i++) {
Boolean found = false;
for (int j = 0; j < keyword.length(); j++) {
if (temp.charAt(i) == keyword.charAt(j)){
found = true;
break; // no need to iterate any further
}
} // inner for
if (found == false){
keyword += temp.charAt(i);
}
} // for
// System.out.println(keyword);
}
}