Write a C/JAVA program to perform encryption and decryption using Hill Cipher.

Hill Cipher:   

Ø  The Hill cipher is a poly-graphic substitution cipher (plaintext letters are substituted in larger groups, instead of substituting letters individually i.e. Works on multiple letters at same time).
Ø  In this Hill cipher technique, the KEY and PLAINTEXT must be in the form of square matrix.
Ø  The KEY must be chosen randomly according to PLAINTEXT.
ENCRYPTION:
Ø  To encrypt the message, we will use the formula C=K.P mod 26 where C is Ciphertext, K is the Key, P is Plaintext.
Ø  Each letter is represented by a number modulo 26. Often following simple scheme is used.
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
0
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

Ø  By using above value table we have to write the corresponding numbers of alphabets present in KEY and PLAINTEXT.
Ø  For example:
PLAINTEXT message: ATTACK,               KEY:CDDG


DECRYPTION:
Steps:
1)     Find inverse of random chosen key matrix
2)    Multiply Inverse matrix with ciphertext against mod 26

Original Plaintext =  [(Inverse of key matrix) x (Ciphertext)] mod 26

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
unsigned int a[3][3]={{6,24,1},{13,16,10},{20,17,15}};
unsigned int b[3][3]={{8,5,10},{21,8,21},{21,12,8}};
int i,j, t=0;
unsigned int c[20],d[20];
char msg[20];
clrscr();
printf("Enter plain text
scanf("%s",msg);
for(i=0;i<strlen(msg);i++)
{ c[i]=msg[i]-65;
printf("%d ",c[i]);
}
for(i=0;i<3;i++)
{ t=0;
for(j=0;j<3;j++)
{
t=t+(a[i][j]*c[j]);
}
d[i]=t%26;
}
printf("\nEncrypted Cipher Text :");
for(i=0;i<3;i++)
printf(" %c",d[i]+65);
for(i=0;i<3;i++)
{
t=0;
for(j=0;j<3;j++)
{
t=t+(b[i][j]*d[j]);
}
c[i]=t%26;
}
printf("\nDecrypted Cipher Text :");
for(i=0;i<3;i++)
printf(" %c",c[i]+65);
getch();
return 0;
}

OUTPUT


Write a JAVA program to perform encryption and decryption using Hill Cipher.

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=newfloat[3][3]; static float[][] mes=newfloat[3][1];
staticfloat[][ ]res=new float[3][1];
static BufferedReaderbr=new BufferedReader(newInputStreamReader(System.in)); static Scannersc=new Scanner(System.in);
public static void main(String[]args) throws IOException {
//TODO code application logic here get keymes();
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("\nEncryptedstringis:");
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("\nDecryptedstringis:");
for(int i=0;i<3;i++) {
System.out.print((char)(decrypt[i][0]%26+97));
}
System.out.print("\n");
}
Public static void getkeymes( )throwsIOException { System.out.println("Enter3x3matrixforkey(Itshouldbeinversible):");
 for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]=sc.nextFloat();
System.out.print("\nEntera3letterstring:");
Stringmsg=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(intj=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("\nInverseMatrixis:");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++)
System.out.print(b[i][j]+"     ");
System.out.print("\n");}
}}


OUTPUT:
Enter a 3 letter string: hai 

Encrypted string is: fdx
 Inverse Matrix is:
0.083333336     0.41666666     -0.33333334
-0.41666666     -0.083333336     0.6666667
0.5833333     -0.083333336     -0.33333334
Decrypted string is: hai

Comments

Search related post on google