You are on page 1of 5

PROGRAM - 2

Aim :​ Write a program to implement Monoalphabetic decryption

Introduction:​ ​Monoalphabetic cipher is one where each symbol in plain text is 
mapped to a fixed symbol in cipher text. The relationship between a character in the 
plain text and the characters in the cipher text is one-to-one. Each alphabetic 
character of plain text is mapped onto a unique alphabetic character of a cipher text. 
It is a simple substitution cipher.Monoalphabetic Cipher is (cryptography) of a 
substitution cipher, using the same fixed mappings from plain text to cipher letters 
across the entire text.

Code:

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

string encoder(string key)


{
string encoded = "";
bool arr[26] = {0};
for (int i=0; i<key.size(); i++)
{
if(key[i] >= 'A' && key[i] <= 'Z')
{
if (arr[key[i]-65] == 0)
{
encoded += key[i];
arr[key[i]-65] = 1;
}
}
else if (key[i] >= 'a' && key[i] <= 'z')
{
if (arr[key[i]-97] == 0)
{
encoded += key[i] - 32;
arr[key[i]-97] = 1;
}
}
}

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


{
if(arr[i] == 0)
{
arr[i]=1;
encoded += char(i + 65);
}
}
return encoded;
}

string cipheredIt(string msg, string encoded)


{
string cipher="";

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


{
if (msg[i] >='a' && msg[i] <='z')
{
int pos = msg[i] - 97;
cipher += encoded[pos];
}
else if (msg[i] >='A' && msg[i] <='Z')
{
int pos = msg[i] - 65;
cipher += encoded[pos];
}
else
{
cipher += msg[i];
}
}
return cipher;
}

int main()
{
string key;
key = "Computer";
cout << "Keyword : " <<key << endl;
string encoded = encoder(key);

string message = "raghavpunjani";


cout << "Message before Ciphering : " << message << endl;
cout << "Ciphered Text : " << cipheredIt(message,encoded) << endl;

return 0;
}
Decryption:-

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

string plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

string encoder(string key)


{
string encoded = "";
bool arr[26] = {0};

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


{
if(key[i] >= 'A' && key[i] <= 'Z')
{
if (arr[key[i]-65] == 0)
{
encoded += key[i];
arr[key[i]-65] = 1;
}
}
else if (key[i] >= 'a' && key[i] <= 'z')
{
if (arr[key[i]-97] == 0)
{
encoded += key[i] - 32;
arr[key[i]-97] = 1;
}
}
}

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


{
if(arr[i] == 0)
{
arr[i]=1;
encoded += char(i + 65);
}
}
return encoded;
}

string decipheredIt(string msg, string encoded)


{
map <char,int> enc;
for(int i=0; i<encoded.size(); i++)
{
enc[encoded[i]]=i;
}

string decipher="";

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


{
if (msg[i] >='a' && msg[i] <='z')
{
int pos = enc[msg[i]-32];
decipher += plaintext[pos];
}
else if(msg[i] >='A' && msg[i] <='Z')
{
int pos = enc[msg[i]];
decipher += plaintext[pos];
}
else
{
decipher += msg[i];
}
}
return decipher;
}

int main()
{
string key;
key = "Computer";
cout << "Keyword : "<< key << endl;

string encoded = encoder(key);

string message = "UHEAHUULAHE";


cout << "Message before Deciphering : " << message << endl;

cout << "Deciphered Text : " << decipheredIt(message,encoded) << endl;

return 0;
}
Output:

Encryption:-

Decryption:-

Finding and Learning :​ ​ A keyword is used as the key, and it determines the letter 
matchings of the cipher alphabet to the plain alphabet. Repeats of letters in the word 
are removed, then the cipher alphabet is generated with the keyword matching to A, 
B, C etc. until the keyword is used up, whereupon the rest of the ciphertext letters are 
used in alphabetical order, excluding those already used in the key.

You might also like