Ins Practical-1 Ins1,2,3

You might also like

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

Faculty of Engineering & Technology

B.Tech CSE – 4rd Year 7th Sem [7A9]


203105311-
Information and Network Security Laboratory
PRACTICAL – 1

Aim: Implement Caesar cipher encryption-decryption.


The Caesar cipher is one of the simplest and most widely known encryption techniques.
It is a type of substitution cipher in which each letter in the plaintext is shifted a certain
number of places down or up the alphabet.

Caesar Cipher Encryption

For encryption, each letter in the plaintext is replaced by a letter some fixed number of
positions down the alphabet.

Formula for encryption:

E(x) = (x + n) mod 26

where:

• E(x) is the encrypted letter,


• xxx is the position of the plaintext letter in the alphabet (0-indexed, i.e., A=0, B=1, ...,
Z=25),
• n is the shift amount,
• mod 26 ensures that the result wraps around if it exceeds 25.

Caesar Cipher Decryption

For decryption, each letter in the ciphertext is replaced by a letter some fixed number of
positions up the alphabet.

Formula for decryption:

D(x) = (x − n + 26) mod 26

where:

• D(x) is the decrypted letter,


• x is the position of the ciphertext letter in the alphabet (0-indexed),
• n is the shift amount,
• +26ensures that the result is positive before applying the modulo operation.

CODE:
#include <iostream>
#include <string>

Page no. 1 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory

using namespace std;


string caesarEncrypt(const string& plaintext, int shift) {
string encryptedText = "";
for (char c : plaintext) {
if (isalpha(c)) {
char offset = islower(c) ? 'a' : 'A';
char encryptedChar = (c - offset + shift) % 26 + offset;
encryptedText += encryptedChar;
} else {
encryptedText += c; // Non-alphabetic characters are not changed
}
}
return encryptedText;
}

string caesarDecrypt(const string& ciphertext, int shift) {


string decryptedText = "";
for (char c : ciphertext) {
if (isalpha(c)) {
char offset = islower(c) ? 'a' : 'A';
char decryptedChar = (c - offset - shift + 26) % 26 + offset;
decryptedText += decryptedChar;
} else {
decryptedText += c; // Non-alphabetic characters are not changed
}
}
return decryptedText;
}
int main() {

Page no. 2 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
string plaintext = "Hello, World!";
int shift = 3;
string encrypted = caesarEncrypt(plaintext, shift);
string decrypted = caesarDecrypt(encrypted, shift);

cout << "Plaintext: " << plaintext << endl;


cout << "Encrypted: " << encrypted << endl;
cout << "Decrypted: " << decrypted << endl;
return 0;
}

Page no. 3 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
PRACTICAL – 2
AIM: Implement Monoalphabetic cipher encryption-decryption

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>

// Encrypt the plaintext using the given key


std::string encrypt(const std::string& plaintext, const std::string& key) {
std::string alphabet = "abcdefghijklmnopqrstuvwxyz";

Page no. 4 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
std::unordered_map<char, char> key_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;
}

// Decrypt the ciphertext using the given key


std::string decrypt(const std::string& ciphertext, const std::string& key) {
std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
std::unordered_map<char, char> inverse_key_map;

// 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];
}

Page no. 5 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
std::string plaintext;
for (char c : ciphertext) {
if (inverse_key_map.find(tolower(c)) != inverse_key_map.end()) {
// Preserve the case of the original letter
plaintext += isupper(c) ? toupper(inverse_key_map[tolower(c)]) :
inverse_key_map[tolower(c)];
} else {
plaintext += c; // Non-alphabetic characters remain unchanged
}
}

return plaintext;
}

int main() {
std::string plaintext = "Hello World";
std::string key = "qazwsxedcrfvtgbyhnujmikolp"; // Example key (must be a permutation
of the alphabet)

// Encrypt the plaintext


std::string ciphertext = encrypt(plaintext, key);
std::cout << "Encrypted: " << ciphertext << std::endl;

// Decrypt the ciphertext


std::string decrypted_text = decrypt(ciphertext, key);
std::cout << "Decrypted: " << decrypted_text << std::endl;

return 0;
}

Page no. 6 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
OUTPUT

Page no. 7 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
PRACTICAl-3
Aim: Implement of Playfair Cipher encryption-decryption

Playfair Cypher

Playfair cipher is an encryption algorithm to encrypt or encode a message. It is the same as a


traditional cipher. The only difference is that it encrypts a digraph (a pair of two letters) instead
of a single letter.

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

o Plaintext: It is the original message that is to be encrypted. It is also known as


a message.
o Ciphertext: It is an encrypted message.
o Cipher: It is an algorithm for transforming plaintext to ciphertext.
o Key: It is the key to encrypt or decrypt the plaintext. It is known only to the sender and
receiver. It is filled character by character in the matrix that is called key-table or key-
matrix.
o Encipher: The process of converting plaintext into ciphertext is called encipher.
o Decipher: The process of removing ciphertext from plaintext is called decipher.
o Cryptanalysis: It is the study of the methods and principles of deciphering ciphertext
without knowing the key.

Code: -
#include <bits/stdc++.h>

Page no. 8 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
using namespace std;

#define SIZE 30

char keyT[5][5]; // Declare keyT globally

void toLowerCase(char plain[], int ps) {


for (int i = 0; i < ps; i++) {
if (plain[i] >= 'A' && plain[i] <= 'Z')
plain[i] += 32;
}
}

int removeSpaces(char* plain, int ps) {


int count = 0;
for (int i = 0; i < ps; i++)
if (plain[i] != ' ')
plain[count++] = plain[i];
plain[count] = '\0';
return count;
}

void generateKeyTable(char key[], int ks, char keyT[5][5]) {


bool dicty[26] = {false};
int i = 0, j = 0;

// Mark characters present in the key


for (int k = 0; k < ks; k++) {
if (key[k] != 'j' && !dicty[key[k] - 'a']) {
dicty[key[k] - 'a'] = true;

Page no. 9 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
keyT[i][j++] = key[k];
if (j == 5) {
i++;
j = 0;
}
}
}

// Mark 'j' as present


dicty['j' - 'a'] = true;

// 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;
}
}
}
}

void printKeyTable(char keyT[5][5]) {


cout << "Key Table:\n";
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << keyT[i][j] << ' ';
}
cout << '\n';

Page no. 10 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
}
}

void search(char keyT[5][5], char a, char b, int arr[]) {


if (a == 'j') a = 'i';
if (b == 'j') b = 'i';
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}

int mod5(int a) {
return (a % 5 + 5) % 5; // To handle negative values correctly
}

int prepare(char str[], int ps) {


if (ps % 2 != 0) {
// Only append 'z' if the plaintext length is odd
str[ps++] = 'z';
str[ps] = '\0';
}
return ps;

Page no. 11 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
}

void encrypt(char str[], int ps) {


int a[4];
for (int i = 0; i < ps; i += 2) {
search(keyT, str[i], str[i + 1], a);
if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] + 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];
} else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] + 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];
} else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}

void decrypt(char str[], int ps) {


int a[4];
for (int i = 0; i < ps; i += 2) {
search(keyT, str[i], str[i + 1], a);
if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] - 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] - 1)];
} else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] - 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] - 1)][a[1]];
} else {

Page no. 12 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}

void encryptByPlayfairCipher(char str[], char key[]) {


int ks = removeSpaces(key, strlen(key));
toLowerCase(key, ks);
int ps = removeSpaces(str, strlen(str));
toLowerCase(str, ps);
ps = prepare(str, ps); // Prepare the plaintext
generateKeyTable(key, ks, keyT);
printKeyTable(keyT); // Print the key table
encrypt(str, ps);
}

int main() {
char str[SIZE] = "instruments", key[SIZE] = "Monarchy";
cout << "Key text: " << key << "\nPlain text: " << str << "\n";

// Encrypt using Playfair cipher


encryptByPlayfairCipher(str, key);
cout << "Cipher text: " << str << "\n";

// Decrypt using Playfair cipher


decrypt(str, strlen(str));
// Remove extra 'z' if present
int len = strlen(str);
if (str[len - 1] == 'z') {

Page no. 13 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
str[len - 1] = '\0';
}
cout << "Decrypted text: " << str << "\n";

return 0;
}
OUTPUT:

Page no. 14 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory

Page no. 15 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory

Page no. 16 enrollment no.-2203051057106

You might also like