-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVigenereDecrypt.java
More file actions
59 lines (51 loc) · 1.78 KB
/
VigenereDecrypt.java
File metadata and controls
59 lines (51 loc) · 1.78 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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.InputStreamReader;
public class VigenereDecrypt {
public static void main(String[] args) throws FileNotFoundException {
// Read in encrypted text from cmd line
Scanner scanner = new Scanner(new InputStreamReader(System.in));
System.out.println("Reading input from console using Scanner in Java ");
System.out.println("Please enter your input: ");
String input = scanner.nextLine();
int len = input.length();
System.out.println();
// Read in 1000 words into an array
ArrayList<String> keys = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("keys.txt"))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
keys.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
StringBuilder str = new StringBuilder();
// Try each of 1000 words as a key
for (String key : keys) {
int kLen = key.length();
// Run through input, adding key to
for (int i = 0; i < len; i++) {
int temp = ((input.charAt(i) - key.charAt(i % kLen)) % 26);
if (temp < 0) {
temp += 26;
}
char curr = (char) (temp + 'a');
str.append(curr);
}
// If 'last' exists in decrypted text, print option out to the cmd line
// This can be changed to look for any desired word
String string = str.toString();
if (string.contains("last")) {
System.out.println(string);
System.out.println();
}
str.setLength(0);
}
}
}