-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHillCipher.java
71 lines (66 loc) · 2.25 KB
/
HillCipher.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
/***********************************
* Filename: HillCipher
* Author: Brandon Badraoui
* Collaborators:
* Created: 3/25/24
* Modified: 4/14/24
* Purpose: will hold the math and computation for the Hill cipher
* <p>
* Attributes:
* -keyMatrix: int[][]
* -hCipher: int[]
* <p>
* Methods:
* +runCipher(int[]): int[]
* -encrypt: void
* +getHCipher: int[]
***********************************/
public class HillCipher extends EmptyCipher {
private final int[][] keyMatrix = {{2, 7}, {5, 22}};
private int[] hCipher;
@Override
public int[] runCipher(int[] userInAsNum) {
hCipher = new int[userInAsNum.length];
int halfLength = userInAsNum.length / 2 + 1;
int[][] messageVector = new int[2][halfLength];
int[][] cipherMatrix = new int[2][halfLength];
int index = 0;
for (int j = 0; j < halfLength; j++) {
for (int i = 0; i < 2; i++) {
if (index < userInAsNum.length) {
messageVector[i][j] = userInAsNum[index];
index++;
}
}
}
//System.out.println(Arrays.deepToString(messageVector));
encrypt(cipherMatrix, keyMatrix, messageVector, halfLength);
//System.out.println(Arrays.deepToString(cipherMatrix));
int index2 = 0;
for (int j = 0; j < halfLength; j++) {
for (int i = 0; i < 2; i++) {
if (index2 < hCipher.length) {
hCipher[index2] = cipherMatrix[i][j];
index2++;
}
}
}
//System.out.println((Arrays.toString(hCipher)));
return hCipher;
}
private void encrypt(int[][] cipherMatrix, int[][] keyMatrix, int[][] messageVector, int halfLength) {
int x, i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < halfLength; j++) {
cipherMatrix[i][j] = 0;
for (x = 0; x < 2; x++) {
cipherMatrix[i][j] += keyMatrix[i][x] * messageVector[x][j];
}
cipherMatrix[i][j] = cipherMatrix[i][j] % 36;
}
}
}
public int[] getHCipher() {
return hCipher;
}
}