0% found this document useful (0 votes)
10 views3 pages

CS

The document discusses and provides code implementations for three cipher techniques: 1) A monoalphabetic cipher that uses a hashmap to encrypt and decrypt messages by substituting each letter for another based on the hashmap. 2) A polyalphabetic cipher that generates a key from the message and keyword, encrypts each letter by adding it to the corresponding key letter modulo 26, and decrypts by subtracting from the key letter. 3) A columnar transposition cipher that stores the message in a 3x4 matrix, encrypts by selecting columns to extract based on the key, and decrypts by reversing the process.

Uploaded by

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

CS

The document discusses and provides code implementations for three cipher techniques: 1) A monoalphabetic cipher that uses a hashmap to encrypt and decrypt messages by substituting each letter for another based on the hashmap. 2) A polyalphabetic cipher that generates a key from the message and keyword, encrypts each letter by adding it to the corresponding key letter modulo 26, and decrypts by subtracting from the key letter. 3) A columnar transposition cipher that stores the message in a 3x4 matrix, encrypts by selecting columns to extract based on the key, and decrypts by reversing the process.

Uploaded by

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

//monoalphabetic cipher

#include <bits/stdc++.h>
using namespace std;
unordered_map<char,char> hashMap;

string encrypt(string msg)


{
string ciphertext;
for(int i=0; i<msg.size(); i++)
{
ciphertext.push_back(hashMap[msg[i]]);
}

return ciphertext;
}

string decrypt(string msg)


{
string plaintext;
for(int i=0; i<msg.size(); i++)
{
plaintext.push_back(hashMap[msg[i]]);
}

return plaintext;
}

void hashFn(string a, string b)


{
hashMap.clear();
for(int i=0; i<a.size(); i++)
{
hashMap.insert(make_pair(a[i],b[i]));
}
}

int main()
{
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string substitution = "qwertyuiopasdfghjklzxcvbnm";
string msg = "absdhj";

hashFn(alphabet, substitution);

string cipher = encrypt(msg);


cout<<"Encrypted Cipher Text: "<<cipher<<endl;

hashFn(substitution, alphabet);
string plain = decrypt(cipher);
cout<<"Decrypted Plain Text: "<<plain<<endl;
}

//polyalphabetic cipher
#include<bits/stdc++.h>
using namespace std;
string generateKey(string str, string key)
{
int x = str.size();

for (int i = 0; ; i++)


{
if (x == i)
i = 0;
if (key.size() == str.size())
break;
key.push_back(key[i]);
}
return key;
}

string cipherText(string str, string key)


{
string cipher_text;

for (int i = 0; i < str.size(); i++)


{
// converting in range 0-25
char x = (str[i] + key[i]) %26;

// convert into alphabets(ASCII)


x += 'A';

cipher_text.push_back(x);
}
return cipher_text;
}

string originalText(string cipher_text, string key)


{
string orig_text;

for (int i = 0 ; i < cipher_text.size(); i++)


{
// converting in range 0-25
char x = (cipher_text[i] - key[i] + 26) %26;

// convert into alphabets(ASCII)


x += 'A';
orig_text.push_back(x);
}
return orig_text;
}

int main()
{
string str = "IAMBOSS";
string keyword = "LOL";

string key = generateKey(str, keyword);


string cipher_text = cipherText(str, key);

cout << "Ciphertext : "


<< cipher_text << "\n";
cout << "Original/Decrypted Text : "
<< originalText(cipher_text, key);
return 0;
}

//Column Transposition Cipher


#include <bits/stdc++.h>
using namespace std;

string encrypt(string key, char mat[][3]) {


int rows = sizeof(mat) / sizeof(mat[0]);
int cols = sizeof(mat[0]) / sizeof(mat[0][0]);

string result = "";


for(int i=0; i < cols; i++) {
int selCol = key[i]-'0'-1;
for(int j = 0; j < 4; j++) {
if(mat[j][selCol]==0) continue;
result+=mat[j][selCol];
}
}

return result;
}

int main()
{
string message = "HELLOWORLD";
string key = "231";
char mat[4][3];

cout<<"Message: "<<message<<endl;
cout<<"Key: "<<key<<endl;

int i=0, j=0, k=0;


while(k < message.length()) {
mat[i][j] = message[k];
k++;
j++;
if(j==3) {
j=0;
i++;
}
}

cout<<"Cipher: ";
string cipher = encrypt(key, mat);
cout<<cipher;

return 0;
}

You might also like