Crypto Da2

You might also like

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

Name: Arun SR

Reg No:21BCT0093
Date:26/02/2024
Cryptography and network security Lab DA 2

1. For the following plaintext and key given below, apply the hill cipher technique for
encryption and decryption. Compare the simulated result with theoretical calculations.
Plaint text: Attack is Tonight

CODE:

#include <stdio.h>

#include <vector>

using namespace std;

// Function to calculate the determinant of a 2x2 matrix

int determinant(const vector<vector<int>>& matrix) {

return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];

// Function to calculate the modulo inverse of a number

int modInverse(int a, int m) {

a = a % m;

for (int x = 1; x < m; x++)

if ((a * x) % m == 1)

return x;

return -1;

}
// Function to calculate the inverse of a 2x2 matrix

vector<vector<int>> inverseMatrix(const vector<vector<int>>& matrix) {

int det = determinant(matrix);

int modInv = modInverse(det, 26);

vector<vector<int>> inverse(2, vector<int>(2, 0));

inverse[0][0] = matrix[1][1];

inverse[0][1] = -matrix[0][1];

inverse[1][0] = -matrix[1][0];

inverse[1][1] = matrix[0][0];

for (int i = 0; i < 2; ++i) {

for (int j = 0; j < 2; ++j) {

inverse[i][j] *= modInv;

inverse[i][j] = (inverse[i][j] % 26 + 26) % 26; // Ensure positive modulo result

return inverse;

// Function to perform matrix multiplication

vector<vector<int>> multiplyMatrix(const vector<vector<int>>& A, const vector<vector<int>>&


B) {

int rowsA = A.size();

int colsA = A[0].size();

int colsB = B[0].size();

vector<vector<int>> result(rowsA, vector<int>(colsB, 0));


for (int i = 0; i < rowsA; ++i) {

for (int j = 0; j < colsB; ++j) {

for (int k = 0; k < colsA; ++k) {

result[i][j] += A[i][k] * B[k][j];

return result;

// Function to perform decryption using Hill cipher

string decrypt(const string& ciphertext, const vector<vector<int>>& keyMatrix) {

int n = keyMatrix.size();

vector<int> numericalValues;

for (char c : ciphertext) {

if (isalpha(c)) {

numericalValues.push_back(tolower(c) - 'a');

vector<vector<int>> ciphertextMatrix;

for (size_t i = 0; i < numericalValues.size(); i += n) {

vector<int> row;

for (int j = 0; j < n; ++j) {

row.push_back(numericalValues[i + j]);

ciphertextMatrix.push_back(row);
}

vector<vector<int>> inverseKeyMatrix = inverseMatrix(keyMatrix);

vector<vector<int>> plaintextMatrix = multiplyMatrix(ciphertextMatrix, inverseKeyMatrix);

string plaintext;

for (const auto& row : plaintextMatrix) {

for (int num : row) {

plaintext += static_cast<char>((num % 26) + 'a');

return plaintext;

int main() {

string ciphertext = "nqdunfxbmesprmtt";

vector<vector<int>> keyMatrix = {

{3, 10},

{20, 9}

};

string plaintext = decrypt(ciphertext, keyMatrix);

cout << "Decrypted plaintext: " << plaintext << endl;

return 0;

OUTPUT:
2.

Encode and decode the given plaintext “Attack is Tonight” using vignere cipher. Assume that,
the key is “cipher”. Compare the simulated result with theoretical calculations.

Code:

#include <stdio.h

>

#include <string>

using namespace std;

// Function to encode plaintext using Vigenère cipher

string vigenereEncrypt(string plaintext, string key) {

string ciphertext = "";

int keyLength = key.length();

for (int i = 0; i < plaintext.length(); ++i) {

char plainChar = plaintext[i];

char keyChar = key[i % keyLength];

char encryptedChar;

if (isupper(plainChar)) {

encryptedChar = ((plainChar - 'A' + keyChar - 'a') % 26) + 'A';

} else if (islower(plainChar)) {

encryptedChar = ((plainChar - 'a' + keyChar - 'a') % 26) + 'a';

} else {

encryptedChar = plainChar;

ciphertext += encryptedChar;

return ciphertext;
}

// Function to decode ciphertext using Vigenère cipher

string vigenereDecrypt(string ciphertext, string key) {

string plaintext = "";

int keyLength = key.length();

for (int i = 0; i < ciphertext.length(); ++i) {

char encryptedChar = ciphertext[i];

char keyChar = key[i % keyLength];

char plainChar;

if (isupper(encryptedChar)) {

plainChar = ((encryptedChar - 'A' - (keyChar - 'a') + 26) % 26) + 'A';

} else if (islower(encryptedChar)) {

plainChar = ((encryptedChar - 'a' - (keyChar - 'a') + 26) % 26) + 'a';

} else {

plainChar = encryptedChar;

plaintext += plainChar;

return plaintext;

int main() {

string plaintext = "Attack is Tonight";

string key = "cipher";

// Encode plaintext

string ciphertext = vigenereEncrypt(plaintext, key);

cout << "Ciphertext: " << ciphertext << endl;


// Decode ciphertext

string decodedPlaintext = vigenereDecrypt(ciphertext, key);

cout << "Decoded Plaintext: " << decodedPlaintext << endl;

return 0;

Output:

You might also like