# Name: Dil Prasad Kuwor Roll no. : 02/064 Subject: Cryptography Assignment no. : Lab No.

2

Date: 10/5/2010

Q.5 Generate the Hill cipher from the given plain text using 3 × 3 matrix. No need to
decrypt. Testing Data Plain Text :- paymoremoney Cipher Text:- LNSHDLEWMTRW Java Code: import java.util.*; class HillCipher { public static void main (String[] args) { String letter1[] = {"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" }; Scanner in = new Scanner(System.in); System.out.println("***** Hill Cipher *****"); String plainText,cipherText=""; int p[][]=new int [3][3]; int key[][]={{17,17,5},{21,18,21},{2,2,19}}; int c[][]= new int [3][3]; int x=0,index = 0; System.out.println("Enter the plaintext:"); plainText=in.nextLine(); for(int i=0;i<plainText.length();i+=3) { for(int j=0;j<3;j++) { String index1; if(i+j<plainText.length()) { char a= plainText.charAt(i+j); String va = String.valueOf(a); index1=findIndex(va); int k=0; p[j][k]=Integer.parseInt(String.valueOf(index1)); } } int sum=0; for(int l=0; l<3; l++) { for(int j=0; j<1; j++) { 1

for(int k=0; k<3; k++) { sum = sum + key[l][k] * p[k][j]; } c[l][j] = sum % 26;//converting to ciphert etxt cipherText = cipherText + letter1[sum % 26]; sum = 0; } } } System.out.println("The cipher text of above plaintext is:\n"+cipherText); } public static String findIndex(String test) { String letter[] = {"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" }; String k; for(int j=0; j<letter.length; j++) { if(test.equalsIgnoreCase(letter[j])) { k=String.valueOf(j); return k; } } return null; } } Output:

The End 
2