-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHillCipherDecrypt.java
More file actions
93 lines (83 loc) · 2.65 KB
/
HillCipherDecrypt.java
File metadata and controls
93 lines (83 loc) · 2.65 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
import java.util.Scanner;
import java.io.InputStreamReader;
import java.math.BigInteger;
public class HillCipherDecrypt {
public static void main(String[] args) {
// 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();
/*
* 2 * 2 matrix try all keys to decrypt finding every matrix that adheres
* gcd(26, (ad-bc)+26)=1
*
* split input into 2s invertible (meaning an inverse M^-1 exists) and the
* determinant(M) must be relatively prime with 26
*/
int[] key = new int[4];
int[] keyInverse = new int[4];
int determinant;
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) {
for (int k = 0; k < 26; k++) {
for (int l = 0; l < 26; l++) {
key[0] = i;
key[1] = j;
key[2] = k;
key[3] = l;
determinant = (i * l) - (j * k);
if (gcd(determinant, 26) == 1) {
// keyInverse = calculateInverse(key, determinant);
tryMatrix(key, input, len, determinant);
}
}
}
}
}
}
public static void tryMatrix(int[] key, String input, int len, int determinant) {
char char1;
char char2;
char a;
char b;
StringBuilder str = new StringBuilder();
for (int i = 0; i < len - 1; i = i + 2) {
a = (char) (input.charAt(i) - 'a');
b = (char) (input.charAt(i + 1) - 'a');
char1 = (char) ((((a * key[0]) + (b * key[1])) % 26) + 'a');
char2 = (char) ((((a * key[2]) + (b * key[3])) % 26) + 'a');
str.append(char1);
str.append(char2);
}
if (str.toString().contains("assignment")) {
System.out.println(str.toString());
System.out.println();
for (int i = 0; i < 4; i++) {
System.out.print(key[i] + " ");
}
System.out.println();
}
}
public static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public static int[] calculateInverse(int[] key, int determinant) {
int a = key[0];
int b = key[1];
int c = key[2];
int d = key[3];
key[0] = (d * determinant) % 26;
key[1] = (-b * determinant) % 26;
key[2] = (-c * determinant) % 26;
key[3] = (a * determinant) % 26;
for (int i = 0; i < 4; i++) {
if (key[i] < 0) {
key[i] = -key[i];
}
}
return key;
}
}