-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigenere.java
52 lines (43 loc) · 1.69 KB
/
vigenere.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
package willcrack;
import java.util.Scanner;
import java.lang.Math;
public class vigenere {
String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
String letterIndividual = "";
int value;
public vigenere(String msg, String key) {
// Iterate through the message
for (int i = 0; i < msg.length(); i++) {
letterIndividual = msg.substring(i, i+1);
// Extract the corresponding key letter
char keyLetter = key.charAt(i % key.length());
// Find the corresponding value for the key letter
for (int j = 0; j < letters.length; j++) {
if (keyLetter == letters[j].charAt(0)) {
value = j;
break;
}
}
// Find the corresponding letter for the message letter
for (int j = 0; j < letters.length; j++) {
if (letterIndividual.equals(letters[j])) {
System.out.print(letters[Math.abs((j+value)%26)]);
break;
}
}
}
System.out.println("");
}
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter cipher");
System.out.print("Cipher: ");
String cipher = myObj.nextLine();
Scanner myObj2 = new Scanner(System.in);
System.out.println("Enter key");
System.out.print("Key: ");
String key = myObj2.nextLine();
System.out.print("Decoded message: ");
vigenere decode = new vigenere(cipher, key);
}
}