INS Lab File
INS Lab File
Maneesh kumar
2K21/CO/268
Submitted to :
Prof. Shailender Kumar
INDEX
9.
Write a program to generate SHA-1 hash.
AIM : To implement Caesar cipher encryption and decryption in C++, where each letter in the
plaintext is shifted by a fixed number of places down the alphabet for encryption, and each letter
in the ciphertext is shifted up by the same number for decryption.
CODE:
#include <iostream>
#include <string>
using namespace std;
int main() {
string text;
int shift;
cout << "Enter text: ";
getline(cin, text);
cout << "Enter shift (key): ";
cin >> shift;
string encryptedText = encrypt(text, shift);
cout << "Encrypted Text: " << encryptedText << endl;
string decryptedText = decrypt(encryptedText, shift);
cout << "Decrypted Text: " << decryptedText << endl;
return 0;
}
OUTPUT:
EXPERIMENT : 2
CODE:
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
int main() {
string text;
string encryptionKey = "QWERTYUIOPASDFGHJKLZXCVBNM"; // Monoalphabetic
encryption key
unordered_map<char, char> decryptionKey = generateDecryptionKey(encryptionKey);
cout << "Enter text: ";
getline(cin, text);
string encryptedText = encrypt(text, encryptionKey);
cout << "Encrypted Text: " << encryptedText << endl;
string decryptedText = decrypt(encryptedText, decryptionKey);
cout << "Decrypted Text: " << decryptedText << endl;
return 0;
}
OUTPUT:
EXPERIMENT : 3
AIM : To implement encryption and decryption using the Playfair cipher in C++. The Playfair
cipher uses a 5x5 matrix of letters for encryption, where each pair of letters in the plaintext is
encrypted by locating them in the matrix and replacing them according to specific rules.
CODE:
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
OUTPUT:
EXPERIMENT : 4
AIM : To implement encryption and decryption using the Polyalphabetic cipher (specifically
the Vigenère cipher) in C++, which is based on substitution and uses multiple substitution
alphabets based on a keyword.
CODE:
#include <iostream>
#include <string>
using namespace std;
// Function to generate the key by repeating it to match the length of the plaintext
string generateKey(string text, string key) {
int textLen = text.length();
int keyLen = key.length();
string extendedKey = key;
for (int i = 0; i < textLen - keyLen; i++) {
extendedKey += key[i % keyLen]; }
return extendedKey; }
// Function to encrypt the text using the Vigenère cipher
string encrypt(string text, string key) {
string result = "";
key = generateKey(text, key);
for (int i = 0; i < text.length(); i++) {
if (isupper(text[i])) {
result += char(((text[i] - 'A') + (key[i] - 'A')) % 26 + 'A'); }
else if (islower(text[i])) {
result += char(((text[i] - 'a') + (key[i] - 'a')) % 26 + 'a'); }
else {
result += text[i]; // Non-alphabet characters remain the same } }
return result; }
// Function to decrypt the text using the Vigenère cipher
string decrypt(string text, string key) {
string result = "";
key = generateKey(text, key);
for (int i = 0; i < text.length(); i++) {
if (isupper(text[i])) {
result += char(((text[i] - 'A') - (key[i] - 'A') + 26) % 26 + 'A'); }
else if (islower(text[i])) {
result += char(((text[i] - 'a') - (key[i] - 'a') + 26) % 26 + 'a'); }
else {
result += text[i]; // Non-alphabet characters remain the same } }
return result; }
int main() {
string text, keyword;
cout << "Enter text: ";
getline(cin, text);
cout << "Enter keyword: ";
getline(cin, keyword);
string encryptedText = encrypt(text, keyword);
cout << "Encrypted Text: " << encryptedText << endl;
string decryptedText = decrypt(encryptedText, keyword);
cout << "Decrypted Text: " << decryptedText << endl;
return 0;
}
OUTPUT:
EXPERIMENT : 5
AIM : To implement encryption and decryption using the Hill cipher in C++. The Hill cipher is a
polygraphic substitution cipher that uses linear algebra and matrix multiplication for encryption and
decryption.
CODE:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int MOD = 26;
int main() {
string text;
vector<vector<int>> keyMatrix = {{3, 3}, {2, 5}}; // Example 2x2 key matrix
cout << "Enter text (in pairs, uppercase): ";
cin >> text;
// Ensure input has even length by adding 'X' if needed
if (text.length() % 2 != 0) {
text += 'X'; }
// Encrypt the text
string encryptedText = encrypt(text, keyMatrix);
cout << "Encrypted Text: " << encryptedText << endl;
// Generate inverse matrix for decryption
vector<vector<int>> invKeyMatrix = inverseMatrix(keyMatrix);
// Decrypt the text
string decryptedText = decrypt(encryptedText, invKeyMatrix);
cout << "Decrypted Text: " << decryptedText << endl;
return 0;
}
OUTPUT:
EXPERIMENT : 6
AIM : To implement the subkey generation process for Simplified Data Encryption Standard (S-DES),
which is a smaller and simpler version of the DES algorithm. This involves generating two 8-bit subkeys
from a 10-bit key using specific permutation and shifting operations.
CODE:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Input 10-bit key (example: 1010000010)
vector<int> key = {1, 0, 1, 0, 0, 0, 0, 0, 1, 0};
// Generate subkeys K1 and K2
auto [K1, K2] = generateSubkeys(key);
cout << "Subkey K1: ";
printKey(K1);
cout << "Subkey K2: ";
printKey(K2);
return 0;
}
OUTPUT:
EXPERIMENT : 7
AIM : To implement the Diffie-Hellman Key Exchange algorithm in C++. This algorithm allows two
parties to securely share a secret key over a public channel.
CODE:
#include <iostream>
#include <cmath>
using namespace std;
EXPERIMENT : 8
AIM : To implement RSA (Rivest-Shamir-Adleman) encryption and decryption algorithm in C++. This
algorithm is used in public-key cryptography to securely transmit data by encrypting messages with a
public key and decrypting them with a private key.
CODE:
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
// Function to compute the greatest common divisor
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp; }
return a; }
// Function to perform modular exponentiation
long long modExp(long long base, long long exp, long long mod) {
long long result = 1;
base = base % mod;
while (exp > 0) {
if (exp % 2 == 1) {
result = (result * base) % mod; }
exp = exp >> 1;
base = (base * base) % mod; }
return result; }
// Function to compute modular inverse of e mod phi(n)
int modInverse(int e, int phi) {
for (int x = 1; x < phi; x++) {
if ((e * x) % phi == 1) {
return x; } }
return -1; // No modular inverse found
}
int main() {
// Step 1: Generate keys
int p, q;
cout << "Enter two prime numbers (p and q): ";
cin >> p >> q;
// Compute n and phi(n)
int n = p * q;
int phi = (p - 1) * (q - 1);
// Choose e such that 1 < e < phi and e is coprime to phi
int e;
cout << "Enter a value for public exponent (e) such that 1 < e < " << phi << " and gcd(e, " <<
phi << ") = 1: ";
cin >> e;
if (gcd(e, phi) != 1) {
cout << "Invalid value for e. It must be coprime with phi(n)." << endl;
return -1; }
// Compute the private key d
int d = modInverse(e, phi);
if (d == -1) {
cout << "No modular inverse found for e. Try another e." << endl;
return -1; }
cout << "Public key (e, n): (" << e << ", " << n << ")" << endl;
cout << "Private key (d, n): (" << d << ", " << n << ")" << endl;
// Step 2: Encryption
int message;
cout << "Enter the message to encrypt (as an integer): ";
cin >> message;
if (message < 0 || message >= n) {
cout << "Message must be in range 0 to " << n - 1 << "." << endl;
return -1;
}
// Encrypt message
int ciphertext = modExp(message, e, n);
cout << "Encrypted message (ciphertext): " << ciphertext << endl;
// Step 3: Decryption
int decryptedMessage = modExp(ciphertext, d, n);
cout << "Decrypted message: " << decryptedMessage << endl;
return 0;
}
OUTPUT:
EXPERIMENT : 9
AIM : To implement a program that generates the SHA-1 (Secure Hash Algorithm 1) hash of a given
input string. SHA-1 is a cryptographic hash function that takes an input and produces a 160-bit (20-byte)
hash value, typically rendered as a 40-character hexadecimal number.
CODE:
#include <iostream>
#include <openssl/sha.h>
#include <iomanip>
#include <sstream>
using namespace std;
int main() {
string input;
cout << "Enter the input string: ";
getline(cin, input);
string sha1HashValue = sha1Hash(input);
cout << "SHA-1 hash: " << sha1HashValue << endl;
return 0;
}
OUTPUT:
EXPERIMENT : 10
AIM : To implement the Digital Signature Algorithm (DSA) in C++ to securely sign a message and
verify its authenticity using public and private keys.
CODE:
#include <iostream>
#include <openssl/dsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <cstring>
using namespace std;
OUTPUT: