-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADFGVX.java
159 lines (117 loc) · 4.34 KB
/
ADFGVX.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
// This program can be used to encrypt and decrypt a message using the
// ADFGVX cipher.
import java.io.*;
import java.util.*;
public class ADFGVX{
public static String[] ciphertext;
public static String[] plaintext;
public static String letterLookup = "ADFGVX";
public static char[][] grid;
public static int[] permutation;
public static void main(String[] args) throws IOException {
grid = new char[6][6];
int mode;
int numMessages;
int fullRows, extraLetters;
String keyword;
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
grid[i][j] = sc.next().charAt(0);
}
}
keyword = sc.next().toString();
// Find keyword permeutation for columns
permutation = permArray(keyword);
numMessages = sc.nextInt();
ciphertext = new String[numMessages];
plaintext = new String[numMessages];
for(int i = 0; i < numMessages; i++){
mode = sc.nextInt();
if(mode == 1){
plaintext[i] = sc.next().toString();
System.out.println(encrypt(plaintext[i]));
}
else if(mode == 2){
ciphertext[i] = sc.next().toString();
// Number of fullRows
fullRows = ciphertext[i].length() / keyword.length();
// Number of extra letters
extraLetters = ciphertext[i].length() % keyword.length();
// Print out decrypted text
System.out.println(decrypt(ciphertext[i], fullRows, extraLetters, keyword));
}
}
}
// Alphabetize the keyword and get the int array for permutations
public static int[] permArray(String keyword){
// Keyword length
int keyL = keyword.length();
// Permutation
int[] perm = new int[keyL];
// Create array objects to hold letters and their index
permFinder[] sorted = new permFinder[keyL];
for(int i = 0; i < keyL; i++)
sorted[i] = new permFinder(keyword.charAt(i), i);
// Sort letters with their indexes
Arrays.sort(sorted);
// Assign new sorted indexes to new int array
for(int i = 0; i < keyL; i++)
perm[i] = sorted[i].index;
return perm;
}
public static String encrypt(String plaintext){
String substituted = "";
String encrypted = "";
// Substituion phase
for(int k = 0; k < plaintext.length(); k++){
for(int i = 0; i < 6; i++){
// Finds unencrypted char in key grid and substitues the cipher chars
// Index = Ciphertext Char
// 0 = A
// 1 = D
// 2 = F
// 3 = G
// 4 = V
// 5 = X
for(int j = 0; j < 6; j++){
if(grid[i][j] == plaintext.charAt(k)){
substituted += letterLookup.charAt(i);
substituted += letterLookup.charAt(j);
}
}
}
}
// Transposition Phase
for (int i = 0;i < permutation.length; i++)
for (int j=permutation[i]; j < substituted.length(); j+= permutation.length)
encrypted += substituted.charAt(j);
return encrypted;
}
public static String decrypt(String ciphertext, int fullRows, int extraLetters, String keyword){
// Vars
String decrypted = "";
char[] partial = new char[ciphertext.length()];
int rowValue = 0;
// rowValue
if(extraLetters > 0){
rowValue = fullRows++;
}
return decrypted;
}
}
// Used for finding permutation of keyword
class permFinder implements Comparable<permFinder> {
public char letter;
public int index;
public permFinder(char l, int i) {
letter = l;
index = i;
}
public int compareTo(permFinder other) {
// Check for double letters
if (this.letter != other.letter)
return this.letter - other.letter;
return this.index - other.index;
}
}