Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

wil 3-Microsoft Word : 1 2 Problem 3: Hill Cipher Consider the following method

ID: 3757376 • Letter: W

Question

wil 3-Microsoft Word : 1 2 Problem 3: Hill Cipher Consider the following method which is called the Hill cipher: V are column vectors and the square matrix d] is the key. c We encrypt the message by encryption function given as the matrix muktiplication: Y KX (mod n) Let's take a precise example:-El-S :1 mod26 a) Encrypt the plaintext "20,10,25,4 by taking 2 numbers at a time xl mod26 b) In order to find the decryption function: X=K1 Y (mod n) we need the inverse of K as KK11l mod 26) (Hint: Use the identity from linear algebra Write the decryption function: X? ,1 1 1/1 10.6CM Cr 14 1

Explanation / Answer

JAVA CODE

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg_2;

import java.util.Scanner;

/**
*
* @author Debasis
*/
public class Chegg_2 {

/**
* @param args the command line arguments
*/
public static int mulInverse(int a,int n)
{
int r1 = n,r2 = a;
int t1 = 0,t2 = 1;
int q,r,t,val;
while(r2 > 0)
{
q = r1 / r2;
r = r1 - q * r2;
r1 = r2;
r2 = r;

t = t1 - q * t2;
t1 = t2;
t2 = t;
}
if(r2 == 0)
{
if(t1>0)
val = t1;
else
val = t1 + n;
return val;
}
return -1;
}
public static int[][] matInv(int A[][],int n,int order)
{
int det = A[0][0] * A[1][1] - A[1][0] * A[0][1];
det = det % n;
if(det < 0)
det = det + n;
  
int detInverse = mulInverse(det, n);
  
if(det == 0 || detInverse == -1)
{
System.err.println("Inverse of matrix doesnot exist. ");
System.exit(0);
}
int cofactor[][] = new int[order][order];
cofactor[0][0] = A[1][1];
cofactor[0][1] = -A[1][0];
cofactor[1][0] = -A[0][1];
cofactor[1][1] = A[0][0];
  
int cofactorInv[][] = new int[order][order];
for(int x=0;x<order;x++)
{
for(int y=0;y<order;y++)
{
cofactorInv[x][y] = cofactor[y][x];
}
}
  
int invA[][] = new int[order][order];
for(int x=0;x<order;x++)
{
for(int y=0;y<order;y++)
{
invA[x][y] = (detInverse * cofactorInv[x][y]) % n ;
if(invA[x][y] < 0)
invA[x][y] = invA[x][y] + n;
}
}
  
return invA;
  
}
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int[][] key = new int[2][2];
int n = 26;
key[0][0] = 3;
key[0][1] = 0;
key[1][0] = 0;
key[1][1] = 3;   
int[][] invK = matInv(key, n,2);
System.out.println("Original key Matrix : ");
for(int x=0;x<2;x++)
{
for(int y=0;y<2;y++)
{
System.out.print(key[x][y]+" ");
}
System.out.println(" ");
}
System.out.println("Inverse of Key matrix : ");
for(int x=0;x<2;x++)
{
for(int y=0;y<2;y++)
{
System.out.print(invK[x][y]+" ");
}
System.out.println(" ");
}
int len;
System.out.println("Enter the length of plaintext : ");
len = sc.nextInt();
int[] input = new int[len];
int[][] plaintext= new int[2][len/2];
int[][] ciphertext= new int[2][len/2];
int[][] plaintext2= new int[2][len/2];
System.out.println("Enter the plain text : ");
for(int i=0;i<len/2;i++)   
{
plaintext[0][i] = sc.nextInt();
plaintext[1][i] = sc.nextInt();   
}
for(int i=0;i<2;i++)
{
for(int j=0;j<len/2;j++)
{
for(int k=0;k<2;k++)
{
ciphertext[i][j] += key[i][k] * plaintext[k][j];
}
ciphertext[i][j] %= n;
  
}
}
System.out.println("Encrypted text : ");
for(int i=0;i<2;i++)
{
for(int j=0;j<len/2;j++)
{
System.out.print(ciphertext[j][i]+" ");
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<len/2;j++)
{
for(int k=0;k<2;k++)
{
plaintext2[i][j] += invK[i][k] * ciphertext[k][j];
}
plaintext2[i][j] %= n;
}
}
System.out.println("Decrypted text : ");
for(int i=0;i<2;i++)
{
for(int j=0;j<len/2;j++)
{
System.out.print(plaintext2[j][i]+" ");
}
}   
  
}
  
}

Note : For any other query, Pls use the comment section.