Algorithm/Steps :
- Take plain text from file named input.txt
- Take key from user and encrypt the message taken from file
- Make matrix values from the given keys.
- Read text characters then encrypt the text using Generated Matrix.
- Save Encrypted text in encrypted.txt
- For decryption use inverse matrix technique.
- Hence decrypt the encrypted message and save decrypted text in decrypted.txt
Code :
Encrypted:
import
java.util.*;
import
java.io.*;
public
class hillenc
{
public static void main(String[]
args)throws Exception
{
BufferedReader br = new
BufferedReader(new FileReader("input.txt"));
FileWriter f1=new
FileWriter("encrypted.txt");
String st="";
String st1="";
while ((st =
br.readLine()) != null)
{
st1=st;
}
StringBuilder sb1=new
StringBuilder(st1);
while(st1.indexOf("
")>=0)
{
sb1.deleteCharAt(st1.indexOf("
"));
st1=sb1.toString();
}
st1=st1.toUpperCase();
System.out.println("Input:"+st1);
int i,j,k;
Scanner s1=new
Scanner(System.in);
System.out.println("Enter
your Key Matrix Size:");
int size;
size=s1.nextInt();
int keyMatrix[][]=new
int[10][10];
for (i = 0; i < size;
i++)
{
for (j = 0;
j < size; j++)
{
keyMatrix[i][j]
=s1.nextInt();
}
}
while((st1.length()%size)
!= 0)
{
st1=st1+"X";
}
System.out.println(st1);
int plain[]=new int
[100];
int cipher[]=new int
[100];
for(i=0;i<st1.length();i++)
{
plain[i]=(st1.charAt(i))%65;
}
int m=st1.length()/size;
int p=0;
int q=0;
int s=0;
for(k=0;k<m;k++)
{
if(k==0)
{
s=0;
}
else
{
s=s+size;
}
for(i=0;i<size;i++)
{
cipher[p]=0;
q=s;
for(j=0;j<size;j++)
{
cipher[p]=cipher[p]+(keyMatrix[i][j]*plain[q]);
q++;
}
cipher[p]=cipher[p]%26;
p++;
}
}
int ans;
for(i=0;i<st1.length();i++)
{
ans=(cipher[i]+65);
f1.write(ans);
}
f1.close();
}
}
2)
Decrypted:
import
java.util.*;
import
java.io.*;
import
java.math.*;
class
deter
{
public 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++;
}
}
}
}
}
public int determinant(int a[][],
int n)
{
int det = 0, sign = 1, p
= 0, q = 0;
if(n==1){
det =
a[0][0];
}
else{
int b[][] =
new int[n-1][n-1];
for(int x =
0 ; x < n ; x++){
p=0;q=0;
for(int
i = 1;i < n; i++){
for(int
j = 0; j < n;j++){
if(j
!= x){
b[p][q++]
= a[i][j];
if(q
% (n-1) == 0){
p++;
q=0;
}
}
}
}
det
= det + a[0][x] *
determinant(b, n-1) *
sign;
sign
= -sign;
}
}
return det;
}
}
class
HillDEC1
{
public static void main(String
args[]) throws Exception
{
deter d1 = new deter();
int n;
Scanner s1 = new
Scanner(System.in);
System.out.print("Enter
size of matric:- ");
n = s1.nextInt();
System.out.print("Enter
key matric Elements:-");
int keyMatrix[][]=new
int[10][10];
int adj_key[][] = new
int[n][n];
for(int i=0;i<n;i++)
{
for(int
j=0;j<n;j++)
{
keyMatrix[i][j]=
s1.nextInt();
}
}
int det_key =
d1.determinant(keyMatrix,n);
System.out.println(det_key);
det_key%=26;
System.out.println(det_key);
if(det_key<0)
det_key+=26;
BigInteger bi1 = new
BigInteger(""+det_key);
System.out.print(bi1);
BigInteger bi2 = new
BigInteger("26");
System.out.println(bi2);
BigInteger bi3 =
bi1.modInverse(bi2);
System.out.println(bi3);
BufferedReader br = new
BufferedReader(new FileReader("encrypted.txt"));
FileWriter f1=new
FileWriter("decrypted.txt");
if (n == 1)
{
adj_key[0][0]
= 1;
}
// temp is used to store
cofactors of A[][]
int sign = 1;
int temp[][] = new
int[n][n];
int i,j,k;
for (i=0; i<n; i++)
{
for (j=0;
j<n; j++)
{
d1.getCofactor(keyMatrix,
temp, i, j, n);
sign
= ((i+j)%2==0)? 1: -1;
k=(sign)*(d1.determinant(temp,
n-1))%26;
if(k<0)
{
adj_key[j][i]
= k+26;
}
else
adj_key[j][i] = k;
}
}
String st="";
String st1="";
while ((st =
br.readLine()) != null)
{
st1=st;
}
int plain[]=new int
[100];
int cipher[]=new int
[100];
for(i=0;i<st1.length();i++)
{
plain[i]=(st1.charAt(i))%65;
}
int m=st1.length()/n;
int p=0;
int q=0;
int s=0;
for(k=0;k<m;k++)
{
if(k==0)
{
s=0;
}
else
{
s=s+n;
}
for(i=0;i<n;i++)
{
cipher[p]=0;
q=s;
for(j=0;j<n;j++)
{
cipher[p]=cipher[p]+(adj_key[i][j]*plain[q]*bi3.intValue());
q++;
}
cipher[p]=cipher[p]%26;
p++;
}
}
int ans;
for(i=0;i<st1.length();i++)
{
ans=(cipher[i]+65);
f1.write(ans);
}
f1.close();
}
}
No comments:
Post a Comment