-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHillCipher.java
More file actions
98 lines (97 loc) · 2.44 KB
/
HillCipher.java
File metadata and controls
98 lines (97 loc) · 2.44 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
import java.io.*;
import java.util.*;
import java.io.*;
public class HillCipher
{
static float[][] decrypt = new float[3][1];
static float[][] a = new float[3][3];
static float[][] b = new float[3][3];
static float[][] mes = new float[3][1];
static float[][] res = new float[3][1];
static BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
static Scanner sc = new Scanner (System.in);
public static void main (String[]args) throws IOException
{
// TODO code application logic here
getkeymes();
for (int i = 0; i < 3; i++)
for (int j = 0; j < 1; j++)
for (int k = 0; k < 3; k++)
{
res[i][j] = res[i][j] + a[i][k] * mes[k][j];
}
System.out.print ("\nEncrypted string is :");
for (int i = 0; i < 3; i++)
{
System.out.print ((char) (res[i][0] % 26 + 97));
res[i][0] = res[i][0];
}
inverse ();
for (int i = 0; i < 3; i++)
for (int j = 0; j < 1; j++)
for (int k = 0; k < 3; k++)
{
decrypt[i][j] = decrypt[i][j] + b[i][k] * res[k][j];
}
System.out.print ("\nDecrypted string is : ");
for (int i = 0; i < 3; i++)
{
System.out.print ((char) (decrypt[i][0] % 26 + 97));
}
System.out.print ("\n");
}
public static void getkeymes () throws IOException
{
System.out.
println ("Enter 3x3 matrix for key (It should be inversible): ");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
a[i][j] = sc.nextFloat ();
System.out.print ("\nEnter a 3 letter string: ");
String msg = br.readLine ();
for (int i = 0; i < 3; i++)
mes[i][0] = msg.charAt (i) - 97;
}
public static void inverse ()
{
float p, q;
float[][] c = a;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
//a[i][j]=sc.nextFloat();
if (i == j)
b[i][j] = 1;
else
b[i][j] = 0;
}
for (int k = 0; k < 3; k++)
{
for (int i = 0; i < 3; i++)
{
p = c[i][k];
q = c[k][k];
for (int j = 0; j < 3; j++)
{
if (i != k)
{
c[i][j] = c[i][j] * q - p * c[k][j];
b[i][j] = b[i][j] * q - p * b[k][j];
}
}
}
}
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
b[i][j] = b[i][j] / c[i][i];
}
System.out.println ("");
System.out.println ("\nInverse Matrix is : ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
System.out.print (b[i][j] + " ");
System.out.print ("\n");
}
}}