-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
228 lines (228 loc) · 9.58 KB
/
Main.java
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
//GridShift Cipher Copyright 2022 by Divesh Gupta is licensed under CC BY-NC-ND 4.0. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/
import java.util.Scanner;
import java.util.Random;
import java.util.*;
import java.awt.datatransfer.StringSelection;
import java.awt.Toolkit;
public class Main {
public static void main(String[] args) {
Scanner scam = new Scanner(System.in);
String alphabet = " abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+`{[}]:;\'\"<,>.?/|ABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder sb=new StringBuilder(alphabet);
StringBuilder ralphabet1 = sb.reverse();
String ralphabet = ralphabet1.toString();
String plain = "";
String cipher = "";
String reverse = "";
String key = "";
int index = 0;
int grid = 0;
int modifier = 0;
int dirInt = 0;
double gridSqr = 0;
System.out.print("Enter Command (e/d/help/exit): ");
String operation = scam.nextLine();
if (operation.equals("e")){
System.out.println("Enter message to encrypt: ");
String message = scam.nextLine();
if (message.contains("~")){
System.out.println("Key detected in message!");
int kIndex = message.indexOf("~");
key = message.substring(kIndex+1, message.length());
message = message.substring(0,kIndex);
}else{
System.out.println("Enter the key (<key>/auto):");
key = scam.nextLine();
}
if (key.equals("auto")){
System.out.println("Generating Key...");
grid = (int)(Math.random() * 90 + 10);
gridSqr = Math.pow(grid,2)-1;
int randstart = (int)(Math.random() * gridSqr);
String[] dirList = {"-", "+"};
Random ran = new Random();
reverse = dirList[ran.nextInt(dirList.length)];
dirInt = (int)(Math.random() * grid + 10);
dirInt %= 100;
if (dirInt == 0){
dirInt = 10;
}
String dir = Integer.toString(dirInt);
key = grid + "(" + reverse + dir + ")" + randstart;
System.out.println("Key Generated: " + key);
List<Integer> numList = new ArrayList<>();
for (int i = 0; i < gridSqr+1; i++){
numList.add(i);
}
System.out.println("Encrypting...");
for (int i = 0; i < message.length(); i++){
plain = message.substring(0+i, 1+i);
if(reverse == "-"){
index = ralphabet.indexOf(plain);
randstart %= 93;
index += randstart;
index %= 93;
cipher += ralphabet.substring(index, index+1);
}else if(reverse.equals("+")){
index = alphabet.indexOf(plain);
randstart %= 93;
index += randstart;
index %= 93;
cipher += alphabet.substring(index, index+1);
}
randstart += grid+dirInt;
}
System.out.println("Finished!");
System.out.println("\nPlaintext:\n" + message + "\n\nCiphertext:\n" + cipher + "\n\nKey:\n" + key + "\n\nExport (Ciphertext is before ~ and Key is after ~):\n" + cipher + "~" + key);
}
else{
System.out.println("Using key: " + key);
String gridS = key.substring(0,1);
try{
grid = Integer.parseInt(key.substring(1,2));
gridS += key.substring(1,2);
grid = Integer.parseInt(gridS);
modifier = 1;
}catch (Exception error){
System.out.println("One digit grid number detected! Cipher will be less secure!");
grid = Integer.parseInt(gridS);
}
gridSqr = Math.pow(grid,2)-1;
int randstart = 0;
reverse = key.substring(2+modifier,3+modifier);
modifier += 1;
String dir = key.substring(2+modifier,4+modifier);
dirInt = Integer.parseInt(dir);
String start1 = key.substring(5+modifier,6+modifier);
String randstartS = start1;
try{
String start2 = key.substring(6+modifier,7+modifier);
randstartS += start2;
randstart = Integer.parseInt(randstartS);
}catch (Exception e) {
System.out.println("Detected 1 digit starting number or incorrect key. Encryption may fail.");
}
try{
String start3 = key.substring(7+modifier,8+modifier);
randstartS += start3;
randstart = Integer.parseInt(randstartS);
}catch (Exception e) {}
try{
String start4 = key.substring(8+modifier,9+modifier);
randstartS += start4;
randstart = Integer.parseInt(randstartS);
}catch (Exception e) {}
randstart = Integer.parseInt(randstartS);
if (randstart <= gridSqr){
key = grid + "(" + reverse + dir + ")" + randstart;
}
List<Integer> numList = new ArrayList<>();
for (int i = 0; i < gridSqr+1; i++){
numList.add(i);
}
System.out.println("Encrypting...");
for (int i = 0; i < message.length(); i++){
plain = message.substring(0+i, 1+i);
if(reverse.equals("-")){
index = ralphabet.indexOf(plain);
randstart %= 93;
index += randstart;
index %= 93;
cipher += ralphabet.substring(index, index+1);
}else if(reverse.equals("+")){
index = alphabet.indexOf(plain);
randstart %= 93;
index += randstart;
index %= 93;
cipher += alphabet.substring(index, index+1);
}
randstart += grid+dirInt;
}
System.out.println("Finished!");
System.out.println("\nPlaintext:\n" + message + "\n\nCiphertext:\n" + cipher + "\n\nKey:\n" + key + "\n\nExport (Ciphertext is before ~, Key is after ~):\n" + cipher + "~" + key);
}
}
else if (operation.equals("d")){
System.out.println("Enter Ciphertext or Export text to decrypt: ");
String message = scam.nextLine();
if (message.contains("~")){
System.out.println("Key detected!");
int kIndex = message.indexOf("~");
key = message.substring(kIndex+1, message.length());
message = message.substring(0,kIndex);
}else{
System.out.println("Enter the key (<key>/auto):");
key = scam.nextLine();
}
String gridS = key.substring(0,1);
try{
grid = Integer.parseInt(key.substring(1,2));
gridS += key.substring(1,2);
grid = Integer.parseInt(gridS);
modifier = 1;
}catch (Exception error){
grid = Integer.parseInt(gridS);
}
gridSqr = Math.pow(grid,2)-1;
int randstart = 0;
reverse = key.substring(2+modifier,3+modifier);
modifier += 1;
String dir = key.substring(2+modifier,4+modifier);
dirInt = Integer.parseInt(dir);
String start1 = key.substring(5+modifier,6+modifier);
String randstartS = start1;
try{
String start2 = key.substring(6+modifier,7+modifier);
randstartS += start2;
randstart = Integer.parseInt(randstartS);
}catch (Exception e) {
System.out.println("Detected 1 digit starting number or incorrect key. Decryption may fail.");
}
try{
String start3 = key.substring(7+modifier,8+modifier);
randstartS += start3;
randstart = Integer.parseInt(randstartS);
}catch (Exception e) {}
try{
String start4 = key.substring(8+modifier,9+modifier);
randstartS += start4;
randstart = Integer.parseInt(randstartS);
}catch (Exception e) {}
randstart = Integer.parseInt(randstartS);
if (randstart <= gridSqr){
key = grid + "(" + reverse + dir + ")" + randstart;
}
List<Integer> numList = new ArrayList<>();
for (int i = 0; i < gridSqr+1; i++){
numList.add(i);
}
System.out.println("Decrypting...");
for (int i = 0; i < message.length(); i++){
plain = message.substring(0+i, 1+i);
if(reverse.equals("-")){
index = alphabet.indexOf(plain);
randstart %= 93;
index += randstart;
index %= 93;
cipher += alphabet.substring(index, index+1);
}else if(reverse.equals("+")){
index = ralphabet.indexOf(plain);
randstart %= 93;
index += randstart;
index %= 93;
cipher += ralphabet.substring(index, index+1);
}
randstart += grid+dirInt;
}
System.out.println("Finished!");
System.out.println("\nCiphertext:\n" + message + "\n\nPlaintext:\n"+ cipher + "\n\nKey:\n" + key);
}
else if (operation.equals("help")){
System.out.println("\nThis program is an original algorithm that encrypts a message using a key.\n\nEncrypt (e)\n Type the message and press Enter.\nEnter the key or type auto and press Enter.\n\nTo make a key, combine these 5 parts:\n> Grid size (1-99)\n> Open Parenthesis: (\n> Grid Advancement (Any two digit negative or positive number with sign +/- before number)\n> Closed Parenthesis: )\n> Start number (0 to [Grid size squared - 1])\nExample key: 77(+98)540\nAnother Example: 34(-05)25\n\nAfter entering the key, wait for the program to encrypt your message. The Export includes the message and the key seperated by ~\n\n\nDecrypt (d)\nType the ciphertext encrypted with the encrypt function or the Export value from the Encryption function and press Enter.\nIf you typed the Export text, the decryption will happen. Otherwise, type the key used to encrypt the message.\nPress Enter and you now have the original message.\n\nRun the program again to get started!");
}
else if (operation.equals("exit")){}
else{
System.out.println("Error! Could not find command: " + operation + "\nPlease run the program again!");
}
}
}