0% found this document useful (0 votes)
10 views2 pages

3-Consider A Sender and Receiver Who Need

The document outlines the implementation of a Java program for encryption and decryption using the Hill cipher substitution technique, which is based on linear algebra. It details the algorithm for encoding and decoding messages by representing letters as numbers and using a 3x3 matrix as the cipher key. Example runs demonstrate the program's functionality, including padding text and displaying encrypted and decrypted messages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

3-Consider A Sender and Receiver Who Need

The document outlines the implementation of a Java program for encryption and decryption using the Hill cipher substitution technique, which is based on linear algebra. It details the algorithm for encoding and decoding messages by representing letters as numbers and using a 3x3 matrix as the cipher key. Example runs demonstrate the program's functionality, including padding text and displaying encrypted and decrypted messages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Hill Cipher

Aim
To implement a Java program for encryption and decryption using Hill cipher substitution technique.

Concept to be Applied
Hill cipher is a polygraphic substitution cipher based on linear algebra.
Each letter is represented by a number modulo 26.
Often the simple scheme A = 0, B = 1, …, Z = 25 is used, but this is not an essential feature of the cipher.
To encrypt a message, each block of n letters (considered as an n-component vector) is multiplied by an
invertible n × n matrix, against modulus 26.
To decrypt the message, each block is multiplied by the inverse of the matrix used for encryption.
The matrix used for encryption is the cipher key, and it should be chosen randomly from the set of invertible n
× n matrices (modulo 26).

Algorithm:
1. Obtain a plain text message to encode in Standard English with no spaces.
2. Split the plain text into group of length three. To fill this, add X at the end.
3. Convert each group of letters with length three into plain text vectors.
4. Replace each letter by the number corresponding to its position in the alphabet i.e. A=1, B=2, C=3…Z=0.
5. Create the key word in a 3*3 matrix.
6. Multiply the two matrices to obtain the cipher text of length three.
7. For decryption, convert each entry in the cipher text vector into its plain text vector by multiplying the cipher text
vector and inverse of a matrix.
8. Thus plaintext is obtained from the corresponding plaintext vector by corresponding position in the alphabet.

Program
package CNS;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class hillcipher
{
//the 3x3 key matrix for 3 characters at once
public static int[][] keymat = new int[][]{{ 1, 2, 1 },{ 2, 3, 2 },{ 2, 2, 1 },};
public static int[][] invkeymat = new int[][]{{ -1, 0, 1 },{ 2, -1, 0 },{ -2, 2, -1},};
public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args)
{
String text,outtext ="",outtext1="";
int ch, n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Plain text for Encryption: ");
//String text=new String();
text=sc.next();
text = text.toUpperCase();
text = text.replaceAll("\\s",""); //removing spaces
n = text.length() % 3; if(n!=0)
{
for(int i = 1; i<= (3-n);i++)
{
text+= 'X';
}
}
System.out.println("Padded Text:" + text);
char[] ptextchars = text.toCharArray();
for(int i=0;i< text.length(); i+=3)
{
outtext += encrypt(ptextchars[i],ptextchars[i+1],ptextchars[i+2]);
}
System.out.println("Encrypted Message: " + outtext);
char[] ptextchars1 = outtext.toCharArray(); for(int i=0;i< outtext.length(); i+=3)
{
outtext1 += decrypt(ptextchars1[i],ptextchars1[i+1],ptextchars1[i+2]);
}
System.out.println("Decrypted Message: " + outtext1);
}
private static String encrypt(char a, char b, char c)
{
String ret = "";
int x,y, z;
int posa = (int)a - 65;
int posb = (int)b - 65;
int posc = (int)c - 65;
x = posa * keymat[0][0] + posb * keymat[1][0] + posc * keymat[2][0];
y = posa * keymat[0][1] + posb * keymat[1][1] + posc * keymat[2][1];
z = posa * keymat[0][2] + posb * keymat[1][2] + posc * keymat[2][2];
a = key.charAt(x%26);
b = key.charAt(y%26);
c = key.charAt(z%26); ret = "" + a + b + c; return ret;
}
private static String decrypt(char a, char b, char c)
{
String ret = "";
int x,y,z;
int posa = (int)a - 65;
int posb = (int)b - 65;
int posc = (int)c - 65;
x = posa * invkeymat[0][0]+ posb * invkeymat[1][0] + posc * invkeymat[2][0];
y = posa * invkeymat[0][1]+ posb * invkeymat[1][1] + posc * invkeymat[2][1];
z = posa * invkeymat[0][2]+ posb * invkeymat[1][2] + posc * invkeymat[2][2];
a = key.charAt((x%26<0)?(26+x%26):(x%26));
b = key.charAt((y%26<0)?(26+y%26):(y%26));
c = key.charAt((z%26<0)?(26+z%26):(z%26)); ret = "" + a + b + c;
return ret;
}
}

Input & Output:


Run1:
Enter the Plain text for Encryption:
Analytics
Padded Text:ANALYTICS
Encrypted Message: ANATCAWGE
Decrypted Message: ANALYTICS
Run2:
Enter the Plain text for Encryption:
velloreinstituteoftechnology
Padded Text:VELLOREINSTITUTEOFTECHNOLOGYXX
Encrypted Message: ZYOVUEUGHUFMTGAQILFCDJDVZYTMHP
Decrypted Message: VELLOREINSTITUTEOFTECHNOLOGYXX
Run3:
Enter the Plain text for Encryption:
cryptography
Padded Text:CRYPTOGRAPHY
Enrcypted Message: GZIDLPOLOZVB
Decrypted Message: CRYPTOGRAPHY

You might also like