//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<[Link](); i++)
{
ciphertext.push_back(hashMap[msg[i]]);
}
return ciphertext;
}
string decrypt(string msg)
{
string plaintext;
for(int i=0; i<[Link](); i++)
{
plaintext.push_back(hashMap[msg[i]]);
}
return plaintext;
}
void hashFn(string a, string b)
{
[Link]();
for(int i=0; i<[Link](); i++)
{
[Link](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 = [Link]();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if ([Link]() == [Link]())
break;
key.push_back(key[i]);
}
return key;
}
string cipherText(string str, string key)
{
string cipher_text;
for (int i = 0; i < [Link](); 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 < [Link]()) {
mat[i][j] = message[k];
k++;
j++;
if(j==3) {
j=0;
i++;
}
}
cout<<"Cipher: ";
string cipher = encrypt(key, mat);
cout<<cipher;
return 0;
}