forked from ravipatel447/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhill_cipher.java
More file actions
154 lines (144 loc) · 3.9 KB
/
hill_cipher.java
File metadata and controls
154 lines (144 loc) · 3.9 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
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
import java.util.Scanner;
public class hill_cipher {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a message:");
String s = sc.nextLine().toUpperCase();
String key = "GYBNQKURP".toUpperCase();
int length = (s.length()+2)/3;
String ar[] = new String [length];
int curr = 0;
for(int i=0;i<length;i++){
ar[i] = s.charAt(curr)+""+s.charAt(curr+1)+""+s.charAt(curr+2)+"";
curr+=3;
HillCipher(ar[i],key);
}
System.out.println("19DCE111:- Ravi Patel");
}
static void getKeyMatrix(String key, int keyMatrix[][])
{
int k = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
keyMatrix[i][j] = (key.charAt(k)) % 65;
k++;
}
}
}
static void encrypt(int cipherMatrix[][],
int keyMatrix[][],
int messageVector[][])
{
int x, i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 1; j++)
{
cipherMatrix[i][j] = 0;
for (x = 0; x < 3; x++)
{
cipherMatrix[i][j] +=
keyMatrix[i][x] * messageVector[x][j];
}
cipherMatrix[i][j] = cipherMatrix[i][j] % 26;
}
}
}
static void decrypt(int finalmetrix[][],int keymetrix[][],int cipherMatrix[][]){
int inv[][] = new int [3][3];
inverse(keymetrix, inv);
encrypt(finalmetrix, inv, cipherMatrix);
}
static void HillCipher(String message, String key)
{
int [][]keyMatrix = new int[3][3];
getKeyMatrix(key, keyMatrix);
int [][]messageVector = new int[3][1];
for (int i = 0; i < 3; i++)
messageVector[i][0] = (message.charAt(i)) % 65;
int [][]cipherMatrix = new int[3][1];
encrypt(cipherMatrix, keyMatrix, messageVector);
String CipherText="";
for (int i = 0; i < 3; i++)
CipherText += (char)(cipherMatrix[i][0] + 65);
System.out.println("Ciphertext: " + CipherText);
int [][]finalmetrix = new int [3][1];
decrypt(finalmetrix, keyMatrix, cipherMatrix);
String PlainText = "";
for(int i=0;i<3;i++)
PlainText += (char)(finalmetrix[i][0]+65);
System.out.println("Decrypted: "+PlainText);
}
static void inverse(int [][]keyMatrix,int [][]inv){
adjoint(keyMatrix, inv);
int det = determinant(keyMatrix, 3);
int invdet = -1;
for(int i=1;i<1000;i++){
if((det*i)%26==1){
invdet = i;
break;
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
inv[i][j] *= invdet;
if(inv[i][j]<0){
inv[i][j] = 26-(((-1)*inv[i][j])%26);
}
else{
inv[i][j] = inv[i][j]%26;
}
}
}
}
static void adjoint(int A[][],int [][]adj)
{
int sign = 1;
int [][]temp = new int[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
getCofactor(A, temp, i, j, 3);
sign = ((i + j) % 2 == 0)? 1: -1;
adj[j][i] = (sign)*(determinant(temp, 2));
}
}
}
static int determinant(int A[][], int n)
{
int D = 0;
if (n == 1)
return A[0][0];
int [][]temp = new int[3][3];
int sign = 1;
for (int f = 0; f < n; f++)
{
getCofactor(A, temp, 0, f, n);
D += sign * A[0][f] * determinant(temp, n - 1);
sign = -sign;
}
return D;
}
static void getCofactor(int A[][], int temp[][], int p, int q, int n)
{
int i = 0, j = 0;
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
if (row != p && col != q)
{
temp[i][j++] = A[row][col];
if (j == n - 1)
{
j = 0;
i++;
}
}
}
}
}
}