-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptography.java
More file actions
421 lines (342 loc) · 13.8 KB
/
Cryptography.java
File metadata and controls
421 lines (342 loc) · 13.8 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package com.example.a345793.androidcryptography;
/*
Project: The following project is a cryptography project that has a user interface and implements the vinegere, caesar, and scytale cipher
By: Deyvik Bhan
date:3/15/2019
*/
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
// Imports required
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button encryptButton;
Button decryptButton;
RadioButton scytale;
RadioButton vinegere;
RadioButton caesar;
String lowerCase = "abcdefghijklmnopqrstuvwxyz";
String upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
EditText input;
EditText key;
TextView output;
String stringInput;
String stringKey;
String secondString;
// Initialization of required variables
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
encryptButton = (Button) this.findViewById(R.id.Button1);
encryptButton.setOnClickListener(this);
decryptButton = (Button) this.findViewById(R.id.Button2);
decryptButton.setOnClickListener(this);
scytale = (RadioButton) this.findViewById(R.id.scytale);
scytale.setOnClickListener(this);
vinegere = (RadioButton) this.findViewById(R.id.vinegere);
vinegere.setOnClickListener(this);
caesar = (RadioButton) this.findViewById(R.id.caesar);
caesar.setOnClickListener(this);
// Sets button to vriable and adds on click listener
input = (EditText) this.findViewById(R.id.input);
key = (EditText) this.findViewById(R.id.key);
output = (TextView) this.findViewById(R.id.output);
// Initializes edit texts and textviews
}
@Override
public void onClick(View v) {
stringInput = input.getText().toString().toUpperCase();
// Gets string of input
stringKey = key.getText().toString();
// Gets string of key
String secondString = finalInput(stringInput);
// Takes all numbers and spaces out of strings
Button b = (Button) v;
if (b.equals(encryptButton)) {
if (scytale.isChecked()) {
// If encrypt and scytale
try {
output.setText("Output: " + sEncrypt(secondString, stringKey));
} catch (Exception E) {
Toast.makeText(this, "For the scytale encryption, make sure the key is an integer!", Toast.LENGTH_SHORT).show();
}
}
else if (caesar.isChecked()) {
// If encrypt and caesar
try {
output.setText("Output: " + encrypt(secondString,stringKey));
} catch (Exception E) {
Toast.makeText(this, "For the Caesar encryption, make sure the key is an integer between 0 and 26!", Toast.LENGTH_SHORT).show();
}
}
else if (vinegere.isChecked()) {
// If encrypt and vinegere
try {
output.setText("Output: " + finalInput(vEncrypt(secondString, stringKey)));
} catch(Exception E) {
Toast.makeText(this, "For the Vinegere encryption, make sure the key is an string!", Toast.LENGTH_SHORT).show();
}
}
}
if (b.equals(decryptButton)) {
// If decrypt and scytale
if (scytale.isChecked()) {
try {
output.setText("Output: " + sDecrypt(secondString, stringKey));
} catch(Exception E) {
Toast.makeText(this, "For the scytale decryption, make sure the key is an integer!", Toast.LENGTH_SHORT).show();
}
}
if (caesar.isChecked()) {
// If decrypt and caesar
try {
output.setText("Output: " + decrypt(secondString, stringKey));
} catch(Exception E) {
Toast.makeText(this, "For the Caesar decryption, make sure the key is an integer between 0 and 16!", Toast.LENGTH_SHORT).show();
}
}
if (vinegere.isChecked()) {
// If decrypt and vinegere
try {
output.setText("Output: " + vDecrypt(secondString, stringKey));
} catch(Exception E) {
Toast.makeText(this, "For the Vinegere decryption, make sure the key is an string!", Toast.LENGTH_SHORT).show();
}
}
}
}
// Function used to find numeric index of certain character
public int findValue(char a) {
if (Character.isLowerCase(a)) {
// Loops through alphabet string to find index of character
for (int i = 0; i < lowerCase.length(); i++) {
if (lowerCase.charAt(i) == a) {
return i + 1;
}
}
}
if (Character.isUpperCase(a)) {
// Loops through alphabet string to find index of character
for (int i = 0; i < upperCase.length(); i++) {
if (upperCase.charAt(i) == a) {
return i + 1;
}
}
return a;
}
return a;
// Returns numeric index of alphabet passed in
}
// Caesar encryption
public String encrypt(String input, String key) {
String output = "";
int intKey = Integer.parseInt(key);
char[] charArray = input.toCharArray();
for (int i = 0; i < charArray.length; i++) {
int index = findValue(charArray[i]) - 1;
if (charArray[i] != ' ') {
if (index + intKey <= 25) {
// If character does not go over z
output += upperCase.charAt(index + intKey);
} else if (index + intKey > 25) {
// If new character goes above z, go back to the start of the alphabet
output += upperCase.charAt((index + intKey) - 26);
}
} else {
output += " ";
}
}
return output;
// Returns encrypted string using caesar cipher
}
// Decrypt for caesar, uses parameter input as input and uses string key as the key to decrypt the input
public String decrypt(String input, String key) {
String output = "";
int intKey = Integer.parseInt(key);
char[] charArray = input.toCharArray();
for (int i = 0; i < charArray.length; i++) {
int index = findValue(charArray[i]);
if (charArray[i] != ' ') {
if (index - intKey > 0) {
output += upperCase.charAt((index - intKey) - 1);
} else if (index - intKey <= 0) {
output += upperCase.charAt((index - intKey) + 25);
}
} else {
output += " ";
}
}
return output;
// Returns decrypted string using caesar
}
// Uses the string input, and uses the key to form a string that will determine the amount of shift for each letter in the input for vinegere
public StringBuilder replaced(String input, String key) {
StringBuilder str = new StringBuilder();
str.append(input);
String index = "";
String output = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ') {
str.deleteCharAt(i);
index += i + ",";
}
}
int i = 0;
while (i < str.length()) {
int j = 0;
for (j = 0; j < key.length(); j++) {
if (i < str.length()) {
output += key.charAt(j);
}
i++;
}
}
str.replace(0, str.length(), output);
int t = 0;
String[] stringArray = index.split(",");
if (!stringArray[0].equals("")) {
for (int i1 = 0; i1 < stringArray.length; i1++) {
int string = Integer.parseInt(stringArray[i1]);
str.insert(string + t, ' ');
t++;
}
}
return str;
/*
Returns string of repeated key used for vinegere,
Example: String Hello my name is Deyvik
Key: ab
Output: ababa ba baba ba bababa
*/
}
// Encrypt for vinegere, uses parameter input as input and uses string key as the key to encrypt the input
public String vEncrypt(String Input, String key) {
String output = "";
StringBuilder aI = new StringBuilder();
aI.append(Input);
StringBuilder vinegere = replaced(Input,key);
for (int i = 0; i < aI.length(); i++) {
if(aI.charAt(i) != ' ') {
int newKey = findValue(vinegere.charAt(i))-1;
String finalKey = Integer.toString(newKey);
String enc = Character.toString(aI.charAt(i));
output+=encrypt(enc,finalKey);
} else {
output+=" ";
}
}
return output;
// Returns encrypted string using vinegere cipher
}
// Decrypt for vinegere, uses parameter input as input and uses string key as the key to decrypt the input
public String vDecrypt(String Input, String key) {
String output = "";
StringBuilder aI = new StringBuilder();
aI.append(Input);
StringBuilder vinegere = replaced(Input,key);
for (int i = 0; i < aI.length(); i++) {
if(aI.charAt(i) != ' ') {
int newKey = findValue(vinegere.charAt(i))-1;
String finalKey = Integer.toString(newKey);
String enc = Character.toString(aI.charAt(i));
output+=decrypt(enc, finalKey);
} else {
output+=" ";
}
}
return output;
//Returns decrypted string from the vinegere cipher
}
// Encrypt for scytale, uses parameter input as input and uses string key as the key as the amount of rows to encrypt the input
public String sEncrypt(String input, String key) {
String output = "";
int rows = Integer.parseInt(key);
int collumns = input.length()/rows;
int total = rows * collumns;
int extra = input.length() % total;
int k = 0;
int l = 0;
if(input.length()%total >= 1) {
collumns +=1;
}
char[][] charArray = new char[rows][collumns];
for (int i = 0; i < charArray.length; i++) {
for (int j = 0; j < charArray[0].length; j++) {
if(j != collumns -1) {
charArray[i][j] = input.charAt(l);
l++;
} else if(k < extra && j == collumns -1) {
charArray[i][j] = input.charAt(l);
k ++;
l++;
} else {
if(j == collumns - 1 && extra > 0) {
charArray[i][j] = '@';
} else {
charArray[i][j] = input.charAt(l);
l++;
}
}
}
}
for (int i = 0; i < charArray[0].length; i++) {
for (int j = 0; j < charArray.length; j++) {
output += charArray[j][i];
}
}
return output;
// Returns encrypted string from the vinegere cipher
}
//Function that removes everything but characters that are alphabets from the string
public String finalInput(String input) {
String output = "";
for (int i = 0; i < input.length(); i ++) {
for (int j = 0; j < upperCase.length(); j ++) {
if(input.charAt(i) == upperCase.charAt(j)) {
output+=upperCase.charAt(j);
}
}
}
return output;
// Returns edited string(Only characters in the alphabet
}
// Decrypt for scytale, uses parameter input as input and uses string key as the key as the amount of rows to decrypt the input
public String sDecrypt(String input, String key) {
String output = "";
int rows = Integer.parseInt(key);
int collumns = input.length()/rows;
int total = rows * collumns;
int extra = input.length() % total;
int k = 0;
int l = 0;
if(input.length()%total > 0) {
collumns +=1;
}
for (int i = 0; i < rows*collumns; i ++) {
if(i > input.length() - 1) {
input+= " ";
}
}
System.out.println(collumns);
char[][] charArray = new char[rows][collumns];
for (int i = 0; i < charArray[0].length; i++) {
for (int j = 0; j < charArray.length; j++) {
charArray[j][i] = input.charAt(l);
l++;
}
}
for (int i = 0; i < charArray.length; i++) {
for (int j = 0; j < charArray[0].length; j++) {
output += charArray[i][j];
}
}
return output;
//Return decrypted string
}
}