Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Experiment 4: 1 2 3 4 5 Encrypt (P, Key) Encrypted I P Encrypted Append (Key (I) ) Encrypted 1 2 3 4 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

EXPERIMENT 4

Aim:
To implement a program to show encryption and decryption through Mono-Alphabetic ciphers.

Introduction and Theory:


A monoalphabetic cipher is any cipher in which the letters of the plaintext are mapped to
ciphertext letters based on a single alphabet key. Substitution ciphers work by replacing each
letter of the plaintext with another letter. For this reason, a monoalphabetic cipher is also
called a simple substitution cipher.
It relies on a fixed replacement structure, meaning the substitution is fixed for each letter of
the alphabet. Thus, if the letter “a” is encoded as letter “Q”, then every time the letter “a”
appears in the plaintext, it’s replaced with the letter “Q”.
There are many different monoalphabetic substitution ciphers, actually infinitely many, as
each letter can be encrypted to any symbol, not just another letter.

Algorithm:

1 encrypt (P, key)


2 encrypted = ""
3 for i in P
4 encrypted.append(key[i])
5 return encrypted

1 decrypt (E, key)


2 decrypted = ""
3 for i in E
4 decrypted.append( key-1[i] )
5 return decrypted
Code:
#include <iostream>
#include <cstring>
using namespace std;
int enc_array[] = {9, 5, 25, 11, 8, 16, 19, 12, 6, 10, 18, 15, 20,
14, 7, 2, 4, 21, 23, 17, 3, 22, 24, 0, 1, 13
};
int dec_array[26];
string Encrypt(string str, int n)
{
string ans = "";
for (int i = 0; i < n; i++)
{
ans += char('a' + enc_array[str[i] - 'a']);
}
return ans;
}
string Decrypt(string str, int n)
{
string ans = "";
for (int i = 0; i < n; i++)
{
ans += char('a' + dec_array[str[i] - 'a']);
}
return ans;
}
int main()
{
for (int i = 0; i < 26; ++i)
{
dec_array[enc_array[i]] = i;
}
string input, enc, dec;
cout << "Enter the string : ";
getline(cin, input);
enc = Encrypt(input, input.length());
cout << "Encoded string : " << enc << endl;
dec = Decrypt(enc, enc.length());
cout << "Decoded string : " << dec << endl;
for (int i = 0; i < 26; ++i)
{
cout << enc_array[i] << ' ' ;
}
cout << endl;
for (int i = 0; i < 26; ++i)
{
cout << dec_array[i] << ' ' ;
}
cout << endl;
return 0;
}

Results and Outputs:

Findings and Learnings:


1. We have implemented Monoalphabetic ciphers
2. A stream cipher is a monoalphabetic cipher if the value of ki does not depend on the
position of the plain text character in the plain text stream.
3. Monoalphabetic ciphers are not that stronger as compared to polyalphabetic cipher.

You might also like