PROGRAM NO.
AIM: Write a C++ Program to perform encryption and decryption using the
following Algorithm:
a) Ceaser Cipher
b) Substitution Cipher
c) Hill Cipher
PROGRAM:
a) Ceaser Cipher
#include<iostream>
#include<string.h>
using namespace std;
int main() {
cout<<"Enter the message:\n";
char msg[100];
[Link](msg,100);
int i, j, length,choice,key;
cout << "Enter key: ";
cin >> key;
length = strlen(msg);
cout<<"Enter your choice \n1. Encryption \n2. Decryption \n";
cin>>choice;
if (choice==1) {
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
If (ch >= 'a' && ch <= 'z'){
ch = ch + key;
if (ch > 'z') {
ch = ch - 'z' + 'a' - 1;
}
msg[i] = ch;
}
else if (ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if (ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
msg[i] = ch;
}}
printf("Encrypted message: %s", msg);
}
else if (choice == 2) { //for decryption
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
//decrypt for lowercase letter
if(ch >= 'a' && ch <= 'z') {
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
msg[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z') {
ch = ch - key;
if(ch < 'A') {
ch = ch + 'Z' - 'A' + 1;
}
msg[i] = ch;
} }
cout << "Decrypted message: " << msg; }}
OUTPUT –
Enter the message:
tutorial
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
1
Encrypted message: wxwruldo
b) Substitution Cipher Program
#include<iostream>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
using namespace std;
int main()
{
FILE *in, *op;
int key;
int t1;
char c;
int ope;
in = fopen("[Link]","r");
op = fopen("[Link]","w");
cout<<"\n What do you want to perform:";
cout<<"\n 1. Encryption";
cout<<"\n 2. Decryption";
cin>>ope;
cout<<"\n Enter Key:";
cin>>key;
if(ope == 1)
//Encryption
while(!feof(in))
{
c = fgetc(in);
if(c==-1)
break;
if(isupper(c))
{
t1 = (int) c-(int) 'A';
t1 = (t1+key)%26;
t1 = t1 + (int) 'A';
fputc((char) t1,op);
}
else if(islower(c))
{
t1 = (int) c-(int) 'a';
t1 = (t1+key)%26;
t1 = t1 + (int) 'a';
fputc((char) t1,op);
}
else{
fputc(c,op);
}
}
else
//decryption
while(!feof(in))
{
c = fgetc(in);
if(c==-1)
break;
if(isupper(c))
{
t1 = (int) c-(int) 'A';
t1 = (t1-key+26)%26;
t1 = t1 + (int) 'A';
fputc((char) t1,op);
}
else if(islower(c))
{
t1 = (int) c-(int) 'a';
t1 = (t1-key+26)%26;
t1 = t1 + (int) 'a';
fputc((char) t1,op);
}
else{
fputc(c,op);
}
}
fclose(in);
fclose(op);
cout<<"\n Done";
getch();
return 0;
}
OUTPUT -
c) Hill Cipher Program
#include <iostream>
#include <vector>
std::string HillCipherEncrypt(const std::string& plaintext, const
std::vector<std::vector<int>>& keyMatrix) {
int keySize = [Link]();
int blockSize = keySize;
std::string paddedPlaintext = plaintext;
int paddingLength = keySize - ([Link]() % keySize);
if (paddingLength != keySize) {
[Link](paddingLength, 'X');
}
std::string ciphertext;
for (int i = 0; i < [Link](); i += blockSize) {
std::vector<int> block(blockSize, 0);
for (int j = 0; j < blockSize; ++j) {
block[j] = paddedPlaintext[i + j] - 'A';
}
for (int j = 0; j < blockSize; ++j) {
int sum = 0;
for (int k = 0; k < blockSize; ++k) {
sum += keyMatrix[j][k] * block[k];
}
ciphertext += (sum % 26) + 'A'; } }
return ciphertext; }
std::string HillCipherDecrypt(const std::string& ciphertext, const
std::vector<std::vector<int>>& keyMatrix) {
int keySize = [Link]();
int blockSize = keySize;
std::string plaintext;
for (int i = 0; i < [Link](); i += blockSize) {
std::vector<int> block(blockSize, 0);
for (int j = 0; j < blockSize; ++j) {
block[j] = ciphertext[i + j] - 'A';
}
std::vector<int> inverseKeyMatrix(keySize * keySize);
for (int j = 0; j < keySize; ++j) {
for (int k = 0; k < keySize; ++k) {
inverseKeyMatrix[j * keySize + k] = keyMatrix[k][j];
}}
for (int j = 0; j < blockSize; ++j) {
int sum = 0;
for (int k = 0; k < blockSize; ++k) {
sum += inverseKeyMatrix[j * blockSize + k] * block[k];
} plaintext += (sum % 26) + 'A';
}}
return plaintext;
}
int main() {
std::vector<std::vector<int>> keyMatrix = {
{6, 24, 1},
{13, 16, 10},
{20, 17, 15}
};
std::string plaintext = "HELLOHILL";
std::string ciphertext = HillCipherEncrypt(plaintext, keyMatrix);
std::cout << "Plaintext: " << plaintext << std::endl;
std::cout << "Ciphertext: " << ciphertext << std::endl;
std::string decryptedText = HillCipherDecrypt(ciphertext, keyMatrix);
std::cout << "Decrypted Text: " << decryptedText << std::endl;
return 0;
}
OUTPUT –
Enter 3x3 matrix for key (should have inverse):
1
0
1
2
4
0
3
5
6
Enter a string of 3 letter(use A through Z): ABC
Encrypted string is: CER
Inverse Matrix is:
1.09091 0.227273 -0.181818
-0.545455 0.136364 0.0909091
-0.0909091 -0.227273 0.181818
Decrypted string is: ABC
PROGRAM NO. 2
AIM : Write a C++ program to implement the DES algorithm logic.
PROGRAM :
#include <iostream>
#include <string>
using namespace std;
string round_keys[16];
string shift_left_once(string key_chunk){
string shifted="";
for(int i = 1; i < 28; i++){
shifted += key_chunk[i];
}
shifted += key_chunk[0];
return shifted;
}
string shift_left_twice(string key_chunk){
string shifted="";
for(int i = 0; i < 2; i++){
for(int j = 1; j < 28; j++){
shifted += key_chunk[j];
}
shifted += key_chunk[0];
key_chunk= shifted;
shifted ="";
}
return key_chunk;
}
void generate_keys(string key){
// The PC1 table
int pc1[56] = {
57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4
};
int pc2[48] = {
14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32
};
// 1. Compressing the key using the PC1 table
string perm_key ="";
for(int i = 0; i < 56; i++){
perm_key+= key[pc1[i]-1];
}
string left= perm_key.substr(0, 28);
string right= perm_key.substr(28, 28);
for(int i=0; i<16; i++){
if(i == 0 || i == 1 || i==8 || i==15 ){
left= shift_left_once(left);
right= shift_left_once(right);
}
else{
left= shift_left_twice(left);
right= shift_left_twice(right);
}
string combined_key = left + right;
string round_key = "";
for(int i = 0; i < 48; i++){
round_key += combined_key[pc2[i]-1];
}
round_keys[i] = round_key;
cout<<"Key "<<i+1<<": "<<round_keys[i]<<endl;
}
}
int main(){
string key = "10101010101110110000100100011000001001110011"
"01101100110011011101";
generate_keys(key);
}
OUTPUT –
Key 1: 000110010100110011010000011100101101111010001100
Key 2: 010001010110100001011000000110101011110011001110
Key 3: 000001101110110110100100101011001111010110110101
Key 4: 110110100010110100000011001010110110111011100011
Key 5: 011010011010011000101001111111101100100100010011
Key 6: 110000011001010010001110100001110100011101011110
Key 7: 011100001000101011010010110111011011001111000000
Key 8: 001101001111100000100010111100001100011001101101
Key 9: 100001001011101101000100011100111101110011001100
Key 10: 000000100111011001010111000010001011010110111111
Key 11: 011011010101010101100000101011110111110010100101
Key 12: 110000101100000111101001011010100100101111110011
Key 13: 100110011100001100010011100101111100100100011111
Key 14: 001001010001101110001011110001110001011111010000
Key 15: 001100110011000011000101110110011010001101101101
Key 16: 000110000001110001011101011101011100011001101101
PROGRAM NO. 3
AIM : Write a C++ program to implement the Blowfish algorithm logic.
PROGRAM :
#include <iostream>
#include <cstring>
#include <openssl/blowfish.h>
const unsigned char KEY[] = "ThisIsASecretKey";
void EncryptBlowfish(const unsigned char* plaintext, int plaintextLength,
unsigned char* ciphertext, const BF_KEY& key) {
BF_ecb_encrypt(plaintext, ciphertext, &key, BF_ENCRYPT);
}
void DecryptBlowfish(const unsigned char* ciphertext, int ciphertextLength,
unsigned char* plaintext, const BF_KEY& key) {
BF_ecb_encrypt(ciphertext, plaintext, &key, BF_DECRYPT);
}
int main() {
BF_KEY key;
BF_set_key(&key, strlen((char*)KEY), KEY);
const char* plaintext = "Hello, Blowfish!";
int plaintextLength = strlen(plaintext);
unsigned char ciphertext[plaintextLength];
EncryptBlowfish((const unsigned char*)plaintext, plaintextLength,
ciphertext, key);
std::cout << "Ciphertext: ";
for (int i = 0; i < plaintextLength; ++i)
std::cout << std::hex << (int)ciphertext[i] << " ";
std::cout << std::endl;
unsigned char decryptedText[plaintextLength];
DecryptBlowfish(ciphertext, plaintextLength, decryptedText, key);
std::cout << "Decrypted text: " << decryptedText << std::endl;
return 0;
}
OUTPUT –
Ciphertext: 3e b0 3d 78 7d d1 e0 f5
Decrypted text: Hello, Blowfish!
PROGRAM NO. 4
AIM : Write a C++ program to implement the Rijndael algorithm logic.
PROGRAM :
#include <iostream>
#include <cstring>
#include <openssl/aes.h>
const unsigned char KEY[] = "ThisIsASecretKey123";
void EncryptRijndael(const unsigned char* plaintext, int plaintextLength,
unsigned char* ciphertext, const AES_KEY& key) {
AES_encrypt(plaintext, ciphertext, &key);
}
void DecryptRijndael(const unsigned char* ciphertext, int ciphertextLength,
unsigned char* plaintext, const AES_KEY& key) {
AES_decrypt(ciphertext, plaintext, &key);
}
int main() {
AES_KEY key;
AES_set_encrypt_key(KEY, 128, &key);
const char* plaintext = "Hello, Rijndael!";
int plaintextLength = strlen(plaintext);
unsigned char ciphertext[plaintextLength];
EncryptRijndael((const unsigned char*)plaintext, plaintextLength,
ciphertext, key);
std::cout << "Ciphertext: ";
for (int i = 0; i < plaintextLength; ++i)
std::cout << std::hex << (int)ciphertext[i] << " ";
std::cout << std::endl;
unsigned char decryptedText[plaintextLength];
DecryptRijndael(ciphertext, plaintextLength, decryptedText, key);
std::cout << "Decrypted text: " << decryptedText << std::endl;
return 0;
}
OUTPUT –
Ciphertext: 9d c8 73 2a b6 e9 5f 17 52 0a f3 6c c4 97 2f 51
Decrypted text: Hello, Rijndael!
PROGRAM NO. 5
AIM : Write a C++ program to encrypt “HELLO WORLD” using BlowFish.
PROGRAM :
#include <iostream>
#include <cstring>
#include <openssl/blowfish.h>
const unsigned char KEY[] = "ThisIsASecretKey";
void EncryptBlowfish(const unsigned char* plaintext, int plaintextLength,
unsigned char* ciphertext, const BF_KEY& key) {
BF_ecb_encrypt(plaintext, ciphertext, &key, BF_ENCRYPT);
}
int main() {
BF_KEY key;
BF_set_key(&key, strlen((char*)KEY), KEY);
const char* plaintext = "HELLO WORLD";
int plaintextLength = strlen(plaintext);
unsigned char ciphertext[plaintextLength];
EncryptBlowfish((const unsigned char*)plaintext, plaintextLength,
ciphertext, key);
std::cout << "Ciphertext: ";
for (int i = 0; i < plaintextLength; ++i)
std::cout << std::hex << (int)ciphertext[i] << " ";
std::cout << std::endl;
return 0;
}
OUTPUT –
Ciphertext: 0 4c 3e 7d 92 d8 4b a1
PROGRAM NO. 6
AIM : Write a C++ program to implement RSA algorithm .
PROGRAM :
#include <iostream>
#include <cstring>
#include <openssl/rsa.h>
#include <openssl/pem.h>
RSA* GenerateRSAKeyPair(int keyLength) {
RSA* rsa = RSA_new();
BIGNUM* bn = BN_new();
BN_set_word(bn, RSA_F4);
RSA_generate_key_ex(rsa, keyLength, bn, nullptr);
BN_free(bn);
return rsa;
}
std::string EncryptRSA(const std::string& plaintext, RSA* publicKey) {
int maxCipherLength = RSA_size(publicKey);
std::string ciphertext(maxCipherLength, '\0');
int cipherLength = RSA_public_encrypt([Link](), (const unsigned
char*)plaintext.c_str(),
(unsigned char*)[Link](), publicKey,
RSA_PKCS1_PADDING);
if (cipherLength == -1) {
std::cerr << "Encryption failed." << std::endl;
return "";
}
[Link](cipherLength);
return ciphertext;
}
std::string DecryptRSA(const std::string& ciphertext, RSA* privateKey) {
int maxPlainLength = RSA_size(privateKey);
std::string plaintext(maxPlainLength, '\0');
int plainLength = RSA_private_decrypt([Link](), (const unsigned
char*)ciphertext.c_str(),
(unsigned char*)[Link](), privateKey,
RSA_PKCS1_PADDING);
if (plainLength == -1) {
std::cerr << "Decryption failed." << std::endl;
return "";
}
[Link](plainLength);
return plaintext;
}
int main() {
RSA* rsaKeyPair = GenerateRSAKeyPair(2048);
BIO* privateKeyBio = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(privateKeyBio, rsaKeyPair, nullptr,
nullptr, 0);
char* privateKeyStr;
long privateKeyLength = BIO_get_mem_data(privateKeyBio,
&privateKeyStr);
std::string privateKey(privateKeyStr, privateKeyLength);
BIO* publicKeyBio = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPublicKey(publicKeyBio, rsaKeyPair);
char* publicKeyStr;
long publicKeyLength = BIO_get_mem_data(publicKeyBio,
&publicKeyStr);
std::string publicKey(publicKeyStr, publicKeyLength);
BIO_free_all(privateKeyBio);
BIO_free_all(publicKeyBio);
std::string message = "Hello, RSA!";
std::string encryptedMessage = EncryptRSA(message, rsaKeyPair);
std::string decryptedMessage = DecryptRSA(encryptedMessage,
rsaKeyPair);
std::cout << "Original Message: " << message << std::endl;
std::cout << "Encrypted Message: " << encryptedMessage << std::endl;
std::cout << "Decrypted Message: " << decryptedMessage << std::endl;
RSA_free(rsaKeyPair);
return 0;
}
OUTPUT-
Original Message: Hello, RSA!
Encrypted Message:
Z\x0A\xE8F\xB2v\x99\x9C\x0C\xD5\xF9P\xD9\xFB\x0A\xA7\xB7\xBA!\
x98\xCA\x88\xAA\xDF\xB4\xBE\x95\xC4a\xB0p\xA4U\x12\x0D\x81\x0C\
x9E\xC0\xB2O\xDD\xD6\xB4\x9B\xDF*\xCF\xC8\xB1\x98\xBBH\xF4F\
xFD\x92u\xF7\xF0\x0B\xE3\xBB\xC8U0\x1B\x9D\xB5*\xC9\xEC\x9D\
xDA\x9A\x92\xC2+\x03\xDC\x91\xDA\xDEi\xD2;\xF2\xA7gU\xD0\x95z\
xC4\xB1\x0E\xAF\xB1\xD2Q\x9E\x9C\xA0\xBD\x81\xA4\xDD\xD3y!\
x13\x9E\x81k\xA8\xE6\xF9u\x1D\x87n\xAC5\xF3a\xA6\xDB\xE2\x01;\
x80\x89\xDE\x1E\x9C:\xE1\x7F\x81\xC6\x0D\xE2\xFC\xE6\xDC\xAA\
x84\x9D\xA5\x99\xCE\xFC\xA4\xAE\x15q&\x81\xC8\x8E\xC1\xBD\xF6\
x86\xD3\x9F\x84\x1C\xDB
Decrypted Message: Hello, RSA!
PROGRAM NO. 7
AIM : Write a C++ program to implement the Diffie- Hellman Key Exchange.
PROGRAM :
#include <iostream>
#include <openssl/dh.h>
DH* GenerateDHParams(int keyLength, int generator) {
DH* dh = DH_new();
if (dh == nullptr) {
std::cerr << "Failed to allocate memory for DH parameters." <<
std::endl;
return nullptr;
}
if (DH_generate_parameters_ex(dh, keyLength, generator, nullptr) != 1) {
std::cerr << "Failed to generate DH parameters." << std::endl;
DH_free(dh);
return nullptr;
}
if (DH_check(dh, nullptr) != 1) {
std::cerr << "Failed to check generated DH parameters." << std::endl;
DH_free(dh);
return nullptr;
}
return dh;
}
bool GenerateKeyPair(DH* dh, BIGNUM** publicKey, BIGNUM**
privateKey) {
if (DH_generate_key(dh) != 1) {
std::cerr << "Failed to generate DH key pair." << std::endl;
return false;
}
DH_get0_key(dh, (const BIGNUM**)publicKey, (const
BIGNUM**)privateKey);
return true;
}
bool PerformKeyExchange(DH* dh, const BIGNUM* publicKeyOther,
unsigned char* sharedSecret) {
if (DH_compute_key(sharedSecret, publicKeyOther, dh) <= 0) {
std::cerr << "Failed to compute shared secret." << std::endl;
return false;
}
return true;
}
int main() {
DH* dhParams = GenerateDHParams(2048, 2);
if (dhParams == nullptr) {
std::cerr << "Failed to generate Diffie-Hellman parameters." <<
std::endl;
return 1;
}
BIGNUM* alicePublicKey;
BIGNUM* alicePrivateKey;
if (!GenerateKeyPair(dhParams, &alicePublicKey, &alicePrivateKey)) {
std::cerr << "Failed to generate key pair for Alice." << std::endl;
DH_free(dhParams);
return 1;
}
BIGNUM* bobPublicKey;
BIGNUM* bobPrivateKey;
if (!GenerateKeyPair(dhParams, &bobPublicKey, &bobPrivateKey)) {
std::cerr << "Failed to generate key pair for Bob." << std::endl;
DH_free(dhParams);
return 1;
}
unsigned char aliceSharedSecret[DH_size(dhParams)];
if (!PerformKeyExchange(dhParams, bobPublicKey, aliceSharedSecret))
{
std::cerr << "Failed to perform key exchange for Alice." << std::endl;
DH_free(dhParams);
return 1;
}
unsigned char bobSharedSecret[DH_size(dhParams)];
if (!PerformKeyExchange(dhParams, alicePublicKey, bobSharedSecret))
{
std::cerr << "Failed to perform key exchange for Bob." << std::endl;
DH_free(dhParams);
return 1;
}
bool sharedSecretsMatch = (std::memcmp(aliceSharedSecret,
bobSharedSecret, DH_size(dhParams)) == 0);
std::cout << "Shared secrets match: " << (sharedSecretsMatch ? "Yes" :
"No") << std::endl;
DH_free(dhParams);
BN_free(alicePublicKey);
BN_free(alicePrivateKey);
BN_free(bobPublicKey);
BN_free(bobPrivateKey);
return 0;
}
OUTPUT-
Shared secrets match: Yes
PROGRAM NO. 8
AIM : Write a C++ program to calculate the MD of a text using the SHA-1
algorithm logic.
PROGRAM :
#include <iostream>
#include <cstring>
#include <openssl/sha.h>
std::string CalculateSHA1(const std::string& text) {
unsigned char hash[SHA_DIGEST_LENGTH];
SHA1((const unsigned char*)text.c_str(), [Link](), hash);
std::string hashString;
char hex[3];
for (int i = 0; i < SHA_DIGEST_LENGTH; ++i) {
sprintf(hex, "%02x", hash[i]);
hashString += hex;
}
return hashString;
}
int main() {
std::string text = "Hello, SHA-1!";
std::string sha1Hash = CalculateSHA1(text);
std::cout << "Text: " << text << std::endl;
std::cout << "SHA-1 Hash: " << sha1Hash << std::endl;
return 0;
}
OUTPUT –
Text: Hello, SHA-1!
SHA-1 Hash: 2ef7bde608ce5404e97d5f042f95f89f1c232871