-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvigne.java
More file actions
121 lines (115 loc) · 3.79 KB
/
Copy pathvigne.java
File metadata and controls
121 lines (115 loc) · 3.79 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
import java.util.Scanner;
public class vigne
{
private String word, key;
private String finish = "";
private String[] alphabet = new String[] {"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","_"};
private String[][] solver = new String[27][27];
/**
* Constructor for objects of class vigne
*/
public vigne(){
}
public void setUp(){
int count = -1;
//sets up 27x27 alphabet matrix
for(int i =0; i<27; i++){
count = -1;
for(int j = i; j<27;j++){
count++;
solver[i][count] = alphabet[j];
}
count = -1;
for(int k = (27-i); k<27; k++){
count++;
solver[i][k] = alphabet[count];
}
}
for(int row = 0; row<27; row++){
for(int col = 0; col<27; col++){
System.out.print(solver[row][col]);
}
System.out.println();
}
System.out.println("Encrypt or decrypt");
Scanner scan = new Scanner(System.in);
String a = scan.nextLine();
if(a.equals("e")){
encrypt();
}
else if(a.equals("d")){
decrypt();
}
}
public void encrypt(){
finish ="";
System.out.println("Enter word to be encrypted");
Scanner scan = new Scanner(System.in);
word = scan.nextLine();
System.out.println("Enter key to encrypt word with");
key = scan.nextLine();
key = key.toUpperCase();
word = word.toUpperCase();
System.out.println("encrypting "+word);
System.out.println("with key "+key);
int vindex, keyin, kindex;
keyin = 0;
kindex = 0;
vindex = 0;
for(int i = 0; i<word.length();i++){
if(keyin>=key.length()) keyin=0;
for(int k = 0; k<27; k++){
if(word.substring(i,i+1).equals(alphabet[k])){
vindex = k;
break;
}
}
for(int j = 0; j<27; j++){
if(key.substring(keyin,keyin+1).equals(solver[j][0])){
kindex = j;
keyin++;
break;
}
}
System.out.println("hor: "+kindex+" vert: "+vindex);
finish+=solver[kindex][vindex];
System.out.println(finish);
}
System.out.println("last: "+finish);
}
public void decrypt(){
finish = "";
System.out.println("Enter encrypted word");
Scanner scan = new Scanner(System.in);
word = scan.nextLine();
System.out.println("Enter key");
key = scan.nextLine();
word=word.toUpperCase();
key=key.toUpperCase();
System.out.println("encrypted word: "+word);
System.out.println("key "+key);
int vindex, keyin, kindex;
keyin = 0;
kindex = 0;
vindex = 0;
for(int i = 0; i<word.length();i++){
if(keyin>=key.length()) keyin=0;
for(int j = 0; j<27; j++){
if(key.substring(keyin,keyin+1).equals(solver[j][0])){
kindex = j;
keyin++;
break;
}
}
for(int k = 0; k<27; k++){
if(word.substring(i,i+1).equals(solver[kindex][k])){
vindex = k;
break;
}
}
finish+=alphabet[vindex];
System.out.println(finish);
}
System.out.println("last: "+finish);
}
}