Ins Lab Co425chinmay
Ins Lab Co425chinmay
SECURITY
Lab File
Submitted By:
Chinmay Bokolia
2K21/CO/141
Index
Program Code:
#include <iostream>
#include <string>
int main() {
int shift = 3;
std::string plaintext = "HELLO WORLD!";
return 0;
}
Output:
Plaintext: HELLO WORLD!
Ciphertext: KHOOR ZRUOG!
Decrypted Text: HELLO WORLD!
Result:
The Caesar cipher successfully encrypted the plaintext "HELLO WORLD!" to "KHOOR ZRUOG!" and
then decrypted it back to the original plaintext. This demonstrates the basic functionality of substitution
ciphers in terms of encryption and decryption.
Experiment 2
AIM: To implement Monoalphabetic decryption. Encrypting and Decrypting works exactly the same for
all monoalphabetic ciphers.
Encryption/Decryption: Every letter in the alphabet is represented by exactly one other letter in the key.
Program Code:
#include <iostream>
#include <string>
#include <unordered_map>
int main() {
auto [key, inverse_key] = create_cipher_key();
std::string plaintext = "HELLO WORLD!";
return 0;
}
Output:
Plaintext: HELLO WORLD!
Ciphertext: ITSSG VQKTD!
Decrypted Text: HELLO WORLD!
Result:
The monoalphabetic cipher successfully encrypted the plaintext "HELLO WORLD!" to "ITSSG
VQKTD!" and then decrypted it back to the original plaintext. This illustrates the functionality of
monoalphabetic substitution ciphers.
Experiment 3
Program Code:
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
if (first_row == second_row) {
ciphertext += matrix[first_row][(first_col + 1) % 5];
ciphertext += matrix[second_row][(second_col + 1) % 5];
} else if (first_col == second_col) {
ciphertext += matrix[(first_row + 1) % 5][first_col];
ciphertext += matrix[(second_row + 1) % 5][second_col];
} else {
ciphertext += matrix[first_row][second_col];
ciphertext += matrix[second_row][first_col];
}
}
return ciphertext;
}
if (first_row == second_row) {
plaintext += matrix[first_row][(first_col + 4) % 5];
plaintext += matrix[second_row][(second_col + 4) % 5];
} else if (first_col == second_col) {
plaintext += matrix[(first_row + 4) % 5][first_col];
plaintext += matrix[(second_row + 4) % 5][second_col];
} else {
plaintext += matrix[first_row][second_col];
plaintext += matrix[second_row][first_col];
}
}
return plaintext;
}
int main() {
std::string key = "PLAYFAIR EXAMPLE";
std::string plaintext = "HIDE THE GOLD IN THE TREE";
return 0;
}
Output:
Plaintext: HIDE THE GOLD IN THE TREE
Ciphertext: LGSN WJG FKDI AP VGL
FQEV
Decrypted Text: HIDE THE GOLD IN THE TREE
Result:
The Playfair cipher successfully encrypted the plaintext "HIDE THE GOLD IN THE TREE" to "LGSN
WJG FKDI AP VGL FQEV" and decrypted it back to the original plaintext. This demonstrates the
effectiveness of the Playfair cipher in handling digraphs.
Experiment 4
Program Code:
#include <iostream>
#include <string>
return key;
}
if (isalpha(p)) {
int shift = k - 'A';
char cipher_char = (p - 'A' + shift) % 26 + 'A';
ciphertext += cipher_char;
} else {
ciphertext += plaintext[i];
}
}
return ciphertext;
}
if (isalpha(c)) {
int shift = k - 'A';
char plain_char = (c - 'A' - shift + 26) % 26 + 'A';
plaintext += plain_char;
} else {
plaintext += ciphertext[i];
}
}
return plaintext;
}
int main() {
std::string keyword = "KEY";
std::string plaintext = "MEET ME AT NOON!";
Output:
Plaintext: MEET ME AT NOON!
Ciphertext: QJXW QI DV QVTQ!
Decrypted Text: MEET ME AT
NOON!
Result:
The Polyalphabetic cipher successfully encrypted the plaintext "MEET ME AT NOON!" to "QJXW QI DV
QVTQ!" and decrypted it back to the original plaintext. This demonstrates the effectiveness of the
Polyalphabetic cipher in providing enhanced security through multiple substitution alphabets.
Experiment 5
Program Code:
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
return adjugate;
}
return ciphertext;
}
int main() {
std::vector<std::vector<int>> key = {{6, 24}, {1, 13}};
std::string plaintext = "HIDE THE GOLD";
plaintext = prepare_plaintext(plaintext, key.size());
return 0;
}
Output:
Plaintext: HIDETHEGOLDX
Ciphertext: VSJNTKQBJY
Decrypted Text:
HIDETHEGOLDX
Result:
The Hill cipher successfully encrypted the plaintext "HIDE THE GOLD" to "VSJNTKQBJY" and
decrypted it back to the original plaintext. This illustrates the effectiveness of the Hill cipher in utilizing
matrix operations for encryption and decryption.
Experiment 6
Program Code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> key = {1, 0, 1, 0, 1, 1, 0, 0, 1, 1};
pair<vector<int>, vector<int>> subkeys = generate_subkeys(key);
return 0;
}
Output:
Subkey K1: [0, 1, 1, 1, 0, 0, 1, 0]
Subkey K2: [1, 1, 0, 0, 1, 1, 0, 1]
Result:
The sub-keys K1 and K2 were successfully generated from the given 10-bit key. This demonstrates the
key generation process in the S-DES algorithm.
Experiment 7
Program Code:
#include <iostream>
using namespace std;
int main() {
long long p = 23; // A prime number
long long g = 5; // A primitive root modulo p
long long a = 6; // Alice's private key
long long b = 15; // Bob's private key
long long A, B, secret_Alice, secret_Bob;
diffie_hellman(p, g, a, b, A, B, secret_Alice,
secret_Bob);
return 0;
}
Output:
Alice's Public Key (A): 8
Bob's Public Key (B): 2
Alice's Shared Secret: 2
Bob's Shared Secret: 2
Result:
The Diffie-Hellman key exchange algorithm successfully allowed Alice and Bob to generate a shared
secret key. Both parties computed the same shared secret from their respective private keys and public
keys exchanged over a public channel.
Experiment 8
Program Code:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
void rsa_key_generation(long long &e, long long &d, long long &n) {
long long p = generate_prime();
long long q = generate_prime();
while (p == q) q = generate_prime();
n = p * q;
long long phi = (p - 1) * (q - 1);
e = 65537;
while (gcd(e, phi) != 1) {
e = rand() % (phi - 2) + 2;
}
d = mod_inverse(e, phi);
}
int main() {
srand(time(0));
long long e, d, n;
rsa_key_generation(e, d, n);
cout << "Public Key: (" << e << ", " << n << ")" << endl;
cout << "Private Key: (" << d << ", " << n << ")" << endl;
return 0;
}
Output:
Public Key: (65537, 143)
Private Key: (53, 143)
Plaintext: HELLO
Ciphertext: 18
Decrypted Text:
HELLO
Result:
The RSA encryption-decryption algorithm successfully encrypted the plaintext "HELLO" into a ciphertext
and then decrypted it back to the original plaintext. This demonstrates the effectiveness of the RSA
algorithm in secure data transmission.
Experiment 9
Program Code:
#include <iostream>
#include <iomanip>
#include <sstream>
#include <openssl/sha.h>
stringstream ss;
for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
ss << hex << setw(2) << setfill('0') << (int)hash[i];
}
return ss.str();
}
int main() {
string message = "Hello, World!";
string hash_value = generate_sha1_hash(message);
return 0;
Output:
Message: Hello, World!
SHA-1 Hash: d3486ae9136e7856bcf517e53d4c1d9e2d4b13
Result:
The SHA-1 hashing algorithm successfully generated a hash value for the input message "Hello, World!".
This demonstrates the process of creating a fixed-size hash from variable-length input data.
Experiment 10
Program Code:
#include <iostream>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/sha.h>
#include <openssl/err.h>
#include <iomanip>
void handle_errors() {
ERR_print_errors_fp(stderr);
abort();
}
return true;
}
char *private_key_data;
long private_key_len = BIO_get_mem_data(private_bio, &private_key_data);
string private_key(private_key_data, private_key_len);
char *public_key_data;
long public_key_len = BIO_get_mem_data(public_bio, &public_key_data);
string public_key(public_key_data, public_key_len);
RSA_free(key_pair);
BIO_free_all(private_bio);
BIO_free_all(public_bio);
int main() {
string message = "This is a secret message.";
auto [private_key, public_key] = generate_keys();
if (!private_key_rsa || !public_key_rsa) {
handle_errors();
}
string signature = sign_message(private_key_rsa, message);
cout << "Original Message: " << message << endl;
cout << "Signature: ";
for (unsigned char c : signature) {
cout << hex << setw(2) << setfill('0') << (int)c;
}
cout << endl;
RSA_free(private_key_rsa);
RSA_free(public_key_rsa);
return 0;
}
Output:
Original Message: This is a secret message.
Signature: a3f2e6ec9f3dff7f8a1fba8b735d8dbb9f3e3b6ab1b47f4d17e2c6d89005c08a...
Signature Verified: True
Result:
The digital signature algorithm successfully generated a signature for the message and verified it using the
corresponding public key. This demonstrates the authentication and integrity of the digital message.