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

Name: C.

Rohith Register: 21BCE0810

Cryptography and Network Security


Lab assignment – 1

Name: C. Rohith
Register No: 21BCE0810
Slot: L13 + L 14
Name: C. Rohith Register: 21BCE0810

1. Implement the following substitution cipher techniques without using standard


cryptographic library Functions Hill Cipher.
A.
Aim: To implement hill cipher program without using standard cryptographic
library functions.
Algorithm:
1. inverseMatrix Function:
Input: A 2x2 matrix key and an integer m.
Output: The inverse of the key matrix modulo m.
Procedure:
Calculate the determinant of the key matrix.
Find the multiplicative inverse of the determinant modulo m.
Compute the elements of the inverse matrix using the determinant inverse.
2. hillCipherEncrypt Function:
Input: Plaintext string plaintext, 2x2 key matrix key, and an integer m.
Output: Ciphertext string obtained by encrypting the plaintext using the Hill Cipher
with the provided key.
Procedure:
Divide the plaintext into blocks of two characters.
For each block:
Convert the characters to their corresponding numerical values.
Perform matrix multiplication with the key.
Take the modulo m of each result.
Convert the resulting numerical values back to characters and append them to the
ciphertext.
3. hillCipherDecrypt Function:
Input: Ciphertext string ciphertext, 2x2 key matrix key, and an integer m.
Name: C. Rohith Register: 21BCE0810

Output: Plaintext string obtained by decrypting the ciphertext using the Hill Cipher
with the provided key.
Procedure:
Compute the inverse of the key matrix modulo m using the inverseMatrix function.
Divide the ciphertext into blocks of two characters.
For each block:
Convert the characters to their corresponding numerical values.
Perform matrix multiplication with the inverse key.
Take the modulo m of each result.
Convert the resulting numerical values back to characters and append them to the
plaintext.
4. main Function:
Input: None.
Output: None (prints the ciphertext and decrypted text).
Procedure:
Initialize a plaintext string and a 2x2 key matrix.
Encrypt the plaintext using the hillCipherEncrypt function and print the ciphertext.
Decrypt the ciphertext using the hillCipherDecrypt function and print the
decrypted text.
5. Output:
Print the ciphertext and decrypted text obtained from the encryption and decryption
processes.
Name: C. Rohith Register: 21BCE0810

Code:

#include <iostream>
#include <vector>

using namespace std;

vector<vector<int>> inverseMatrix(vector<vector<int>>& key, int


m) {
int det = key[0][0] * key[1][1] - key[0][1] * key[1][0];
int detInv = 0;
for (int i = 0; i < m; i++) {
if ((det * i) % m == 1) {
detInv = i;
break;
}
}
vector<vector<int>> inv(2, vector<int>(2, 0));
inv[0][0] = (key[1][1] * detInv) % m;
inv[0][1] = (-key[0][1] * detInv) % m;
inv[1][0] = (-key[1][0] * detInv) % m;
inv[1][1] = (key[0][0] * detInv) % m;
return inv;
}

string hillCipherEncrypt(string plaintext, vector<vector<int>>&


key, int m) {
string ciphertext = "";
for (int i = 0; i < plaintext.length(); i += 2) {
int x = plaintext[i] - 'a';
int y = plaintext[i + 1] - 'a';
int newX = (key[0][0] * x + key[0][1] * y) % m;
int newY = (key[1][0] * x + key[1][1] * y) % m;
ciphertext += (char)(newX + 'a');
ciphertext += (char)(newY + 'a');
}
return ciphertext;
}

string hillCipherDecrypt(string ciphertext, vector<vector<int>>&


key, int m) {
string plaintext = "";
vector<vector<int>> invKey = inverseMatrix(key, m);
for (int i = 0; i < ciphertext.length(); i += 2) {
int x = ciphertext[i] - 'a';
Name: C. Rohith Register: 21BCE0810

int y = ciphertext[i + 1] - 'a';


int newX = (invKey[0][0] * x + invKey[0][1] * y) % m;
int newY = (invKey[1][0] * x + invKey[1][1] * y) % m;
plaintext += (char)(newX + 'a');
plaintext += (char)(newY + 'a');
}
return plaintext;
}

int main() {
cout << "Name: C. Rohith" << endl;
cout << "Register No: 21BCE0810" << endl;
string plaintext = "hello";
vector<vector<int>> key = { {1, 2}, {3, 4} };
int m = 26;
string ciphertext = hillCipherEncrypt(plaintext, key, m);
cout << "Ciphertext: " << ciphertext << endl;
string decryptedText = hillCipherDecrypt(ciphertext, key,m);
cout << "Decrypted Text: " << decryptedText << endl;
return 0;
}

Output:
Name: C. Rohith Register: 21BCE0810

2. Find the ciphertext from plaintext and reconstruct the plain text from ciphertext
for the following using Playfair cipher manually
• Plain text: COME TO CNS LAB
• Key : THANK YOU

A.
Name: C. Rohith Register: 21BCE0810

3. Find the ciphertext from plaintext and reconstruct the plain text from ciphertext
for the following using Vigenere cipher manually
• Plain text: VELLORE INSTITUTE OF TECHNOLOGY
• Key : SCOPE

A.
Name: C. Rohith Register: 21BCE0810

4. Find the ciphertext from plaintext and reconstruct the plain text from ciphertext
for the following using Hill cipher manually
• Plain text: WELCOME TO VIT
• Key : NICE
Name: C. Rohith Register: 21BCE0810
Name: C. Rohith Register: 21BCE0810

5. Find the ciphertext from plaintext and reconstruct the plain text from ciphertext
for the following using Hill cipher manually
• Plain text: COMPUTER SCIENCE AND ENGINEERING
• Key : ANALYTICS
A.
Name: C. Rohith Register: 21BCE0810
Name: C. Rohith Register: 21BCE0810
Name: C. Rohith Register: 21BCE0810
Name: C. Rohith Register: 21BCE0810
Name: C. Rohith Register: 21BCE0810

You might also like