You are on page 1of 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