Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views9 pages

Crypto Lab Assessment 02

The document contains three programming tasks related to encryption algorithms: Hill Cipher, Data Encryption Standard (DES), and Advanced Encryption Standard (AES). Each task includes code snippets in C++ and Java that demonstrate how to encrypt and decrypt messages using the respective algorithms. The document provides a practical implementation of these encryption methods, showcasing their functionality and usage.

Uploaded by

Shirsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Crypto Lab Assessment 02

The document contains three programming tasks related to encryption algorithms: Hill Cipher, Data Encryption Standard (DES), and Advanced Encryption Standard (AES). Each task includes code snippets in C++ and Java that demonstrate how to encrypt and decrypt messages using the respective algorithms. The document provides a practical implementation of these encryption methods, showcasing their functionality and usage.

Uploaded by

Shirsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LAB ASSESSMENT – 2

NAME: SHIRSH HARISHANKAR BHAKTA


REG. NO: 22BCE3527

Q.1] Hill Cipher


Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Function to find the modular inverse of a matrix for decryption
bool findModInverseMatrix(vector<vector<int>>& keyMatrix, vector<vector<int>>&
inverseMatrix, int size) {
int determinant = 0;

// Calculate determinant for 2x2 matrix


if (size == 2) {
determinant = (keyMatrix[0][0] * keyMatrix[1][1] - keyMatrix[0][1] * keyMatrix[1][0])
% 26;
determinant = (determinant + 26) % 26; // Handle negative determinants
} else {
return false;
}

// Find modular multiplicative inverse of determinant mod 26


int determinantInverse = -1;
for (int i = 1; i < 26; ++i) {
if ((determinant * i) % 26 == 1) {
determinantInverse = i;
break;
}
}

if (determinantInverse == -1) {
return false;
}

// Compute inverse matrix for 2x2 case


inverseMatrix = {{keyMatrix[1][1], -keyMatrix[0][1]},
{-keyMatrix[1][0], keyMatrix[0][0]}};

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


for (int j = 0; j < size; ++j) {
inverseMatrix[i][j] = ((inverseMatrix[i][j] * determinantInverse) % 26 + 26) % 26;
}
}

return true;
}

// Function to multiply matrices mod 26


string multiplyMatrix(const vector<vector<int>>& keyMatrix, const string& text, int size) {
string result = "";
for (int i = 0; i < text.size(); i += size) {
vector<int> block(size, 0);
for (int j = 0; j < size; ++j) {
for (int k = 0; k < size; ++k) {
block[j] += keyMatrix[j][k] * (text[i + k] - 'a');
}
block[j] %= 26;
}
for (int j = 0; j < size; ++j) {
result += (block[j] + 'a');
}
}
return result;
}

int main() {
string input, key;
int size;

// Take input
cin >> input >> key >> size;

// Adjust input to fit block size


while (input.size() % size != 0) {
input += 'x'; // Padding with 'x'
}

// Generate the key matrix


vector<vector<int>> keyMatrix(size, vector<int>(size, 0));
int k = 0;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
keyMatrix[i][j] = key[k++] - 'a';
}
}
// Encrypt the input string
string cipherText = multiplyMatrix(keyMatrix, input, size);
cout << cipherText << endl;

// Decrypt to get the original string


vector<vector<int>> inverseMatrix(size, vector<int>(size, 0));
if (findModInverseMatrix(keyMatrix, inverseMatrix, size)) {
string originalText = multiplyMatrix(inverseMatrix, cipherText, size);
cout << originalText << endl;
}

return 0;
}

Output:
Q.2] Data Encryption Standard(DES)
Code:
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.util.Scanner;

public class des {

public static void main(String[] args) {


try {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the plaintext: ");
String plaintext = scanner.nextLine();

System.out.print("Enter the key (8 characters long): ");


String key = scanner.nextLine();
if (key.length() != 8) {
System.out.println("Key must be exactly 8 characters long!");
return;
}

DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());


SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

Cipher cipher = Cipher.getInstance("DES");

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());

String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);


System.out.println("Encrypted Text: " + encryptedText);

cipher.init(Cipher.DECRYPT_MODE, secretKey);

byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));


String decryptedText = new String(decryptedBytes);
System.out.println("Decrypted Text: " + decryptedText);

scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output:

Q.3] Advanced Encryption Standard(AES)


Code:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.Key;
import java.util.Base64;
import java.util.Scanner;

public class aes {

public static void main(String[] args) {


try {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the message: ");
String message = scanner.nextLine();
SecretKey secretKey = generateKey();
System.out.println("\nRound Keys:");
for (int i = 0; i <= 10; i++) {
System.out.println("RoundKey" + i + ": " +
Base64.getEncoder().encodeToString(secretKey.getEncoded()));
}
String encryptedText = encrypt(message, secretKey);
System.out.println("\nEncrypted Text: " + encryptedText);
String decryptedText = decrypt(encryptedText, secretKey);
System.out.println("Decrypted Text: " + decryptedText);

scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
return keyGenerator.generateKey();
}
public static String encrypt(String message, Key key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(message.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}

public static String decrypt(String encryptedText, Key key) throws Exception {


Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decryptedBytes);
}
}

Output:

You might also like