3-Consider A Sender and Receiver Who Need
3-Consider A Sender and Receiver Who Need
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;
}
}