Enter 2x2 size key matrix
* Enter 2x2 size invertible matrix.
static void getKeyMatrix(int keyMatrix[][])
{
    int j;
    Scanner input = new Scanner(System.in);
    for (int i = 0; i < 2; i++)
    {
    for (int j = 0; j < 2; j++)
       {
            j= input.nextInt();
            keyMatrix[i][j] = j
       }
    }
    }
public static void main(String[] args)
{
    getKeyMatrix();
    if(keyMatrix[0][0]*keyMatrix[1][1]-keyMatrix[0][1]*keyMatrix[1][0])
   {
        System.out.print("Please enter invertible matrix");
   }
    else
    {
        Scanner sc= new Scanner(System.in);
        String plaintext=sc.nextLine();
          HillCipher(plaintext);
        }
}
Use next button to get the logical explaination.
static void HillCipher(String message)
{
    if(message.length()%2!=0){
        message=message.concat("X");
    }
    encrypt(message);
    System.out.print(" Ciphertext:" + CipherText);
    System.out.print("Do you want to go for decryption?");
    ScannerScanner sc= new Scanner(System.in);
    String decide=sc.nextLine();
    if(decide=="Y"){
        decrypt(CipherText);
    }
    else
    {
        System.out.print("Thankyou! ..Hope you understand implementation of encryption using hill cipher.");
    }
}
static void encrypt(String message)
{
    int x, i, j,pos=0;
    for(int k=0;k<(message.length())/2;k++){
        messageVector[0][0]=int(message[pos]);
        messageVector[1][0]=int(message[pos+1]);
        for (i = 0; i < 2; i++){
        for (j = 0; j < 1;j++){
      cipherMatrix[i][j] = 0;
                for (x = 0; x < 2; x++) {
                    cipherMatrix[i][j] += keyMatrix[i][x] * messageVector[x][j];
                }
                cipherMatrix[i][j] = cipherMatrix[i][j] % 26;
                CipherText+=charAt(cipherMatrix[i][j]+65);
            }
            pos=pos+2;
        }
}
Do you want to decrypt the generated cipher text?
static void HillCipher(String message)
{
    if(message.length()%2!=0){
        message=message.concat("X");
    }
    encrypt(message);
    System.out.print(" Ciphertext:" + CipherText);
    System.out.print("Do you want to go for decryption?");
    ScannerScanner sc= new Scanner(System.in);
    String decide=sc.nextLine();
    if(decide=="Y"){
        decrypt(CipherText);
    }
    else
    {
        System.out.print("Thankyou! ..Hope you understand implementation of encryption using hill cipher.");
    }
}
static void decrypt()
{
    inverse();
    int x, i, j,pos=0;
    for(int k=0;k<(CipherText.length())/2;k++)
    {
        cipherVector[0][0]=int(CipherText[pos]);
        cipherVector[1][0]=int(CipherText[pos+1]);
        for (i = 0; i < 2; i++){
        for (j = 0; j < 1;j++){
            PlainMatrix[i][j]=0;
                for (x = 0; x < 2; x++)
                {
                    PlainMatrix[i][j] += decryptMatrix[i][x] * cipherVector[x][j];
                }
                PlainMatrix[i][j] =PlainMatrix[i][j] % 26;
                Plainreturn=charAt(PlainMatrixMatrix[i][j]+65);
            }
            pos=pos+2;
        }
}
public static void main(String[] args)
{
    getKeyMatrix();
    if(keyMatrix[0][0]*keyMatrix[1][1]-keyMatrix[0][1]*keyMatrix[1][0])
   {
        System.out.print("Please enter invertible matrix");
   }
    else
    {
        Scanner sc= new Scanner(System.in);
        String plaintext=sc.nextLine();
          HillCipher(plaintext);
        }
}
Key matrix entered by user:
Please enter invertible matrix
A matrix M is not invertible if |M|=0.eg.
Could we invert a 2x2 matrix such as A?
|A|=(7*10)-(14*5)=0
The determinant of matrix A is equal to zero, therefore, this matrix is NOT invertible.
Please enter only numbers between 0 to 25.
Plain text:
{"A":0, "B":1, "C":2, "D":3, "E":4, "F":5, "G":6, "H":7, "I":8, "J":9, "K":10, "L":11, "M":12, "N":13, "O":14, "P":15, "Q":16, "R":17, "S":18, "T":19, "U":20, "V":21, "W":22, "X":23, "Y":24, "Z":25}
x | p00 | = | m00 | ||
p10 | m10 |
Cipher text:
Cipher text:
Decrypted cipher text:
* Please attempt post test quiz given below.
Thankyou! ..Hope you understand implementation of encryption using hill cipher.
* Please attempt post test quiz given below.
Please enter valid key matrix.
Please enter valid plaintext.