Professional Documents
Culture Documents
Ins Practical-1 Ins1,2,3
Ins Practical-1 Ins1,2,3
Ins Practical-1 Ins1,2,3
For encryption, each letter in the plaintext is replaced by a letter some fixed number of
positions down the alphabet.
E(x) = (x + n) mod 26
where:
For decryption, each letter in the ciphertext is replaced by a letter some fixed number of
positions up the alphabet.
where:
CODE:
#include <iostream>
#include <string>
The Monoalphabetic cipher is a type of substitution cipher in which each letter of the
plaintext is replaced by a corresponding letter from a fixed permutation of the alphabet. The
same substitution is used for the entire message, hence the term "monoalphabetic."
Key Concepts
1. Alphabet: The standard set of letters (e.g., the 26 letters of the English alphabet).
2. Key: A permutation of the alphabet used for substitution. Each letter in the alphabet
maps to a unique letter in the key.
3. Plaintext: The original message that needs to be encrypted.
4. Ciphertext: The encrypted message.
Encryption Process
To encrypt a message:
1. Create a key map: Construct a dictionary that maps each letter of the alphabet to a
corresponding letter in the key.
2. Substitute letters: Replace each letter in the plaintext with the corresponding letter
from the key map.
Decryption Process
To decrypt a message:
1. Create an inverse key map: Construct a dictionary that maps each letter of the key
back to the corresponding letter in the alphabet.
2. Substitute letters: Replace each letter in the ciphertext with the corresponding letter
from the inverse key map.
PROGRAM:
#include <iostream>
#include <string>
#include <unordered_map>
// Map each letter of the alphabet to the corresponding letter in the key
for (size_t i = 0; i < alphabet.size(); ++i) {
key_map[alphabet[i]] = key[i];
}
std::string ciphertext;
for (char c : plaintext) {
if (key_map.find(tolower(c)) != key_map.end()) {
// Preserve the case of the original letter
ciphertext += isupper(c) ? toupper(key_map[tolower(c)]) : key_map[tolower(c)];
} else {
ciphertext += c; // Non-alphabetic characters remain unchanged
}
}
return ciphertext;
}
// Map each letter of the key back to the corresponding letter in the alphabet
for (size_t i = 0; i < key.size(); ++i) {
inverse_key_map[key[i]] = alphabet[i];
}
return plaintext;
}
int main() {
std::string plaintext = "Hello World";
std::string key = "qazwsxedcrfvtgbyhnujmikolp"; // Example key (must be a permutation
of the alphabet)
return 0;
}
Playfair Cypher
It initially creates a key-table of 5*5 matrix. The matrix contains alphabets that act as the key
for encryption of the plaintext. Note that any alphabet should not be repeated. Another point to
note that there are 26 alphabets and we have only 25 blocks to put a letter inside it. Therefore,
one letter is excess so, a letter will be omitted (usually J) from the matrix. Nevertheless, the
plaintext contains J, then J is replaced by I. It means treat I and J as the same letter,
accordingly.
Since Playfair cipher encrypts the message digraph by digraph. Therefore, the Playfair cipher
is an example of a digraph substitution cipher.
Terminology
Code: -
#include <bits/stdc++.h>
#define SIZE 30
// Fill the remaining spaces in the key table with other characters
for (int k = 0; k < 26; k++) {
if (!dicty[k]) {
keyT[i][j++] = 'a' + k;
if (j == 5) {
i++;
j = 0;
}
}
}
}
int mod5(int a) {
return (a % 5 + 5) % 5; // To handle negative values correctly
}
int main() {
char str[SIZE] = "instruments", key[SIZE] = "Monarchy";
cout << "Key text: " << key << "\nPlain text: " << str << "\n";
return 0;
}
OUTPUT: