Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Name: Shreeyans Bahadkar

Sec: D
Branch: CSE
Reg. No.: 199302019
Date: 02-03-2022
ISS Lab 3

Aim: To implement a program for Hill Cipher.

Description: 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).

Advantages of using a Hill cipher include:

 First polygraphic cipher system that was built on the practical system using
more than three symbols or letters in one.

Disadvantages of using a Hill cipher include:

 The basic Hill cipher is vulnerable to a known-plaintext attack because it is


completely linear.

Working:
 Choose a key to encrypt the cipher. Input the key and string. If length of string
in n then key length should be n^2.
 Generate the Key Square (nxn) from the key. Input all the chars as indices of
the key in the matrix. Let it be Matrix K. If Key Matrix does not have an inverse
i.e |K| == 0 , key cannot be used for decryption.
 Generate the Text Matrix (nx1) from the key. Input all the chars as indices of
the plaintext in the matrix. Let it be Matrix P
 Let Encrypted Text be Matrix E. Hence it can be found by,
E = ( K x P ) mod 26
 Let Decrypted Text be Matrix D. Hence it can be found by,
-1
D = ( K x E ) mod 26
 Resultant text can be found by converting the matrix back to text.
Code:

#include <iostream>
#include <math.h>
#include <strings.h>
#define SIZE 50;

using namespace std;

float en[3][1], de[3][1], a[3][3], b[3][3], msg[3][1], m[3][3];


// vars for encrypted text, decrypted text, key matrix, inverse key matrix
and temp key matrix respectively.

void keyToMatrix(char key[9]) { // to convert string to matrix


        char keyUpper[9];
        for (int i = 0; i < 9; i++) {
                keyUpper[i] = toupper(key[i]); // convert string to uppercase
    }
        int index = 0;
        for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++) {
                        a[i][j] = keyUpper[index++] - 65;
                        m[i][j] = a[i][j];
        }
}

void getKeyMatrix() { //get key and message from user


        char plainText[3];
        char key[9];
        cout << "Enter 9 letter key (should have inverse):\n";
        cin >> key;
        keyToMatrix(key);
        cout << "\nEnter a string of 3 letter(A through Z): ";
        cin >> plainText;
        for (int i = 0; i < 3; i++)
                msg[i][0] = plainText[i] - 65;
}

void encrypt() { //encrypts the message


        for (int i = 0; i < 3; i++)
                for (int j = 0; j < 1; j++)
                        for (int k = 0; k < 3; k++)
                                en[i][j] = en[i][j] + a[i][k] * msg[k][j]; // matrix
multiplication to get encrypted matrix
        cout << "\nEncrypted string is: ";
        for (int i = 0; i < 3; i++)
                cout << (char)(fmod(en[i][0], 26) + 65); //modulo 26 is taken for
each element of the matrix obtained by multiplication
}

void inverseMatrix() { //find inverse of key matrix


        int i, j, k;
        float p, q;
        for (i = 0; i < 3; i++)
                for (j = 0; j < 3; j++) {
                        if (i == j)
                                b[i][j] = 1;
                        else
                                b[i][j] = 0;
        }
        for (k = 0; k < 3; k++) {
                for (i = 0; i < 3; i++) {
                        p = m[i][k];
                        q = m[k][k];
                        for (j = 0; j < 3; j++) {
                                if (i != k) {
                                        m[i][j] = m[i][j] * q - p * m[k][j];
                                        b[i][j] = b[i][j] * q - p * b[k][j];
                }
            }
        }
    }
        for (i = 0; i < 3; i++)
                for (j = 0; j < 3; j++)
                        b[i][j] = b[i][j] / m[i][i];
        cout << "\n\nInverse Matrix is:\n";
        for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++)
                        cout << b[i][j] << " ";
                cout << "\n";
    }
}
void decrypt() { //decrypt the message
        int i, j, k;
        inverseMatrix();
        for (i = 0; i < 3; i++)
                for (j = 0; j < 1; j++)
                        for (k = 0; k < 3; k++)
                                de[i][j] = de[i][j] + b[i][k] * en[k][j];
        cout << "\nDecrypted string is: ";
        for (i = 0; i < 3; i++)
                cout << (char)(fmod(de[i][0], 26) + 65); //modulo 26 is taken to
get the original message
        cout << "\n";
}

int main() {
        getKeyMatrix();
        encrypt();
        decrypt();
        return 0;
}

Output:

You might also like