0% found this document useful (0 votes)
20 views39 pages

Ins Final Manual

The document describes experiments conducted for an Information and Network Security laboratory course. It includes implementations of classical encryption algorithms like the Caesar cipher, monoalphabetic cipher, Playfair cipher, and Hill cipher. It also covers implementations of the Diffie-Hellman key exchange, RSA encryption/decryption, and verifying digital signatures using Cryptool. For each experiment, the student is required to write code implementing the encryption and decryption algorithms, provide sample inputs/outputs, and get the work certified by instructors.

Uploaded by

adivaibhavi4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views39 pages

Ins Final Manual

The document describes experiments conducted for an Information and Network Security laboratory course. It includes implementations of classical encryption algorithms like the Caesar cipher, monoalphabetic cipher, Playfair cipher, and Hill cipher. It also covers implementations of the Diffie-Hellman key exchange, RSA encryption/decryption, and verifying digital signatures using Cryptool. For each experiment, the student is required to write code implementing the encryption and decryption algorithms, provide sample inputs/outputs, and get the work certified by instructors.

Uploaded by

adivaibhavi4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

FACULTY OF ENGINEERING AND TECHNOLOGY

BACHELOR OF TECHNOLOGY

INFORMATION AND NETWORK SECURITY


(203105311)

SEMESTER VII

Computer Science & Engineering


Department

Laboratory Manual
CERTIFICATE

This is to certify that Mr. Wagh Deepkumar Atul with enrollment no.

200303105170 has successfully completed his laboratory experiments in the

Information and Network Security (203105311) from the department of

Computer Science & Engineering during the academic year 2023-24.

Date of Submission: ......................... Staff In charge: ...........................

Head Of Department: ...........................................


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

TABLE OF CONTENT

Page No
Date Date of Marks
Sr.
Experiment Title of Comple Sign (out of
No
Start tion 10)
From To

Implement Caesar Cipher


1. 1 encryption-decryption.

2. Implement Monoalphabetic
cipher encryption-decryption.

3. Implement Playfair cipher


encryption-decryption.

Implement Hill cipher


4. encryption-decryption.

5. Implement Polyalphabetic cipher


encryption-decryption.

6. Implement Simple Transposition


encryption-decryption.

Implement One time pad


7. encryption-decryption.

Implement Diffi-Hellmen Key


8. exchange Method.

Implement RSA encryption-


9. decryption algorithm.

10. Demonstrate working of Digital


Signature using Cryptool.
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

PRACTICAL-1
Aim: Implement Caesar Cipher encryption-decryption.
Theory:
The encryption process takes each letter in the text message and replaces it with a letter that appears
in the three alphabets. For example, the word "HELLO" would be encrypted as "KHOOR" with move
3. The same process is applied for decryption, but in reverse. Each letter of the encrypted message is
reshuffled by the same amount that restores the original plaintext. The Caesar cipher works on the
principle of modular arithmetic, where the transmission revolves around the alphabet. For example, if
the offset value is 3 and the letter is "X", which is at the end of the alphabet, it will be broken to the
first letter "A". This wrapper ensures consistent encryption and decryption. Although the Caesar cipher
is simple and easy to understand, it is also very vulnerable to attack. Since there are only 26 possible
transitions, a brute-force attack can easily try all combinations, rendering encryption ineffective.
Therefore, the Caesar cipher is considered a weak form of encryption and is not suitable for protecting
sensitive information in today's context. However, it is still a valuable learning tool for introducing
the concepts of encryption and decryption.

Encryption Code:

#include<iostream>
using namespace std;

int main()
{
char message[100], ch;
int i, key;
cout << "Enter a message to encrypt: ";
cin.getline(message, 100);
cout << "Enter key: ";
cin >> key;
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

}
}
cout << "Encrypted message: " << message;
return 0;
}

OUTPUT :

Decryption Code:

#include<iostream>
using namespace std;
int main()
{
char message[100], ch;
int i, key;
cout << "Enter a message to decrypt: ";
cin.getline(message, 100);
cout << "Enter key: ";
cin >> key;
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch > 'a'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
cout << "Decrypted message: " << message;
return 0;
}
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

Output:
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

PRACTICAL – 2

Aim : Implement Monoalphabetic cipher encryption-decryption.

Theory:
A monoalphabetic cipher is a basic form of substitution cipher where each letter in the plaintext is
replaced by a corresponding letter from the ciphertext alphabet. The substitution is based on a fixed
mapping between the letters of the two alphabets.

Choose a secret key that is a permutation of the letters of the alphabet. Take the text message and
replace each letter with the corresponding key letter. The resulting ciphertext is an encrypted
message. Decryption: Get the secret key used for encryption. Take the secret text message and
replace each letter with the corresponding key letter in reverse order. The resulting plaintext is the
decrypted message. For example, suppose our secret key is
"QWERTYUIOPASDFGHJKLZXCVBNM", where every letter of the alphabet is associated with
that letter. corresponding letter in the key. Using this key, the plaintext "HELLO" is encrypted as
"QEBZY" (H -> Q, E -> E, L -> B, L -> Z, O -> Y). To decrypt the message, we would use the same
key in reverse, so "QEBZY" becomes "HELLO" again.

Encryption Code:
#include <bits/stdc++.h>
using namespace std;
unordered_map<char,char> hashmap;
string encrypt(string msg)
{
string ciphertext;
for(int i=0; i<msg.size(); i++)
{
ciphertext.push_back(hashmap[msg[i]]);
}
return ciphertext;
}
void hashFn(string a, string b)
{
hashmap.clear();
for(int i=0; i<a.size(); i++)
{
hashmap.insert(make_pair(a[i],b[i]));
}
}

int main()
{
string alphabet = "abcdefghijklmnopqrstuvwxyz";
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

string substitution = "qwertyuiopasdfghjklzxcvbnm";


string msg = "koushal";
hashFn(alphabet, substitution);
string cipher = encrypt(msg);
cout<<"plain text:"<<msg<<endl;
cout<<"Encrypted Cipher Text: "<<cipher<<endl;
}

OUTPUT :

Decryption Code:
#include <bits/stdc++.h>
using namespace std;
unordered_map<char,char> hashmap;
string decrypt(string msg)
{
string plaintext;
for(int i=0; i<msg.size(); i++)
{
plaintext.push_back(hashmap[msg[i]]);
}
return plaintext;
}
void hashFn(string a, string b)
{
hashmap.clear();
for(int i=0; i<a.size(); i++)
{
hashmap.insert(make_pair(a[i],b[i]));
}
}
int main()
{
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string substitution = "qwertyuiopasdfghjklzxcvbnm";
string msg = "agxliqs";
hashFn(substitution, alphabet);
string plain = decrypt(msg);
cout<<"encryted text:"<<msg<<endl;
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

cout<<"Decrypted Plain Text: "<<plain<<endl;


}

OUTPUT :
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

PRACTICAL – 3

Aim: Implement Playfair cipher encryption-decryption.

Theory:

The Playfair Cipher is a polygraph substitution cipher invented by Charles Wheatstone in the 1800s
but popularized by Lord Playfair during the Crimean War. It provides a more secure alternative to
single-letter ciphers by using a 5 x 5 grid of letters for encryption and decryption. Choose a keyword
or phrase without repeating letters.
Create a 5x5 grid (matrix) using the letters of the keyword followed by the rest of the alphabet
(except 'J' as it is usually combined with 'I'). Playfair encryption offers a better level of protection
than single letter encryption. ciphers because it removes some of the vulnerabilities associated with
frequency analysis. However, it is still vulnerable to certain types of attacks such as known plaintext
attacks and selected plaintext attacks.

Encryption Code:
#include <iostream>
#include <vector>
#include <algorithm>

// Function to generate the Playfair cipher key matrix


std::vector<std::vector<char>> generate_key_matrix(const std::string& key) {
std::string modified_key = key;
std::transform(modified_key.begin(), modified_key.end(), modified_key.begin(), ::toupper);
modified_key.erase(std::remove_if(modified_key.begin(), modified_key.end(), ::isspace),
modified_key.end());
modified_key.erase(std::remove_if(modified_key.begin(), modified_key.end(), ::isdigit),
modified_key.end());
std::string unique_characters = "";
for (char c : modified_key) {
if (unique_characters.find(c) == std::string::npos) {
unique_characters += c;
}
}

std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


for (char c : alphabet) {
if (unique_characters.find(c) == std::string::npos) {
unique_characters += c;
}
}

std::vector<std::vector<char>> key_matrix(5, std::vector<char>(5));

int index = 0;
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

for (int i = 0; i < 5; ++i) {


for (int j = 0; j < 5; ++j) {
key_matrix[i][j] = unique_characters[index++];
}
}

return key_matrix;
}

// Function to find the position of a character in the key matrix


std::pair<int, int> find_position(const std::vector<std::vector<char>>& key_matrix, char c) {
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
if (key_matrix[i][j] == c) {
return {i, j};
}
}
}
return {-1, -1};
}

// Function to perform Playfair cipher encryption


std::string playfair_encrypt(const std::vector<std::vector<char>>& key_matrix, const std::string&
plaintext) {
std::string ciphertext = "";
std::string modified_plaintext = plaintext;
std::transform(modified_plaintext.begin(), modified_plaintext.end(), modified_plaintext.begin(),
::toupper);
modified_plaintext.erase(std::remove_if(modified_plaintext.begin(), modified_plaintext.end(),
::isspace), modified_plaintext.end());
modified_plaintext.erase(std::remove_if(modified_plaintext.begin(), modified_plaintext.end(),
::isdigit), modified_plaintext.end());

int length = modified_plaintext.length();


int i = 0;

while (i < length) {


char c1 = modified_plaintext[i];
char c2 = (i + 1 < length) ? modified_plaintext[i + 1] : 'X';

if (c1 == c2) {
c2 = 'X';
--i;
++length;
}

std::pair<int, int> pos1 = find_position(key_matrix, c1);


std::pair<int, int> pos2 = find_position(key_matrix, c2);
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

if (pos1.first == pos2.first) { // Same row


ciphertext += key_matrix[pos1.first][(pos1.second + 1) % 5];
ciphertext += key_matrix[pos2.first][(pos2.second + 1) % 5];
} else if (pos1.second == pos2.second) { // Same column
ciphertext += key_matrix[(pos1.first + 1) % 5][pos1.second];
ciphertext += key_matrix[(pos2.first + 1) % 5][pos2.second];
} else { // Different row and column
ciphertext += key_matrix[pos1.first][pos2.second];
ciphertext += key_matrix[pos2.first][pos1.second];
}

i += 2;
}

return ciphertext;
}

// Function to perform Playfair cipher decryption


std::string playfair_decrypt(const std::vector<std::vector<char>>& key_matrix, const std::string&
ciphertext) {
std::string plaintext = "";
int length = ciphertext.length();
int i = 0;

while (i < length) {


char c1 = ciphertext[i];
char c2 = ciphertext[i + 1];

std::pair<int, int> pos1 = find_position(key_matrix, c1);


std::pair<int, int> pos2 = find_position(key_matrix, c2);

if (pos1.first == pos2.first) { // Same row


plaintext += key_matrix[pos1.first][(pos1.second - 1 + 5) % 5];
plaintext += key_matrix[pos2.first][(pos2.second - 1 + 5) % 5];
} else if (pos1.second == pos2.second) { // Same column
plaintext += key_matrix[(pos1.first - 1 + 5) % 5][pos1.second];
plaintext += key_matrix[(pos2.first - 1 + 5) % 5][pos2.second];
} else { // Different row and column
plaintext += key_matrix[pos1.first][pos2.second];
plaintext += key_matrix[pos2.first][pos1.second];
}

i += 2;
}

return plaintext;
}

// Function to print the 5x5 matrix


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

void print_matrix(const std::vector<std::vector<char>>& matrix) {


for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
}

int main() {
std::string plaintext;
std::string key;

std::cout << "Enter the plaintext: ";


std::getline(std::cin, plaintext);

std::cout << "Enter the key: ";


std::getline(std::cin, key);

// Generate the key matrix


std::vector<std::vector<char>> key_matrix = generate_key_matrix(key);

// Menu
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << "1. Encrypt" << std::endl;
std::cout << "2. Decrypt" << std::endl;
int choice;
std::cout << "Enter your choice: ";
std::cin >> choice;

std::cin.ignore(); // Ignore the newline character after reading the choice.

if (choice == 1) {
std::string ciphertext = playfair_encrypt(key_matrix, plaintext);
std::cout << "----------------------------------------------" << std::endl;
std::cout << "5x5 Matrix:" << std::endl;
print_matrix(key_matrix);
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Ciphertext: " << ciphertext << std::endl;
} else if (choice == 2) {
std::string decrypted_text = playfair_decrypt(key_matrix, plaintext);
std::cout << "----------------------------------------------" << std::endl;
std::cout << "5x5 Matrix:" << std::endl;
print_matrix(key_matrix);
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Decrypted Text: " << decrypted_text << std::endl;
} else {
std::cout << "Invalid option selected. Please try again." << std::endl;
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

return 0;
}

OUTPUT:
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

PRACTICAL – 4

Aim : Implement Hill-cipher encryption-decryption.

Theory:
Hill encryption is an encryption algorithm used to both encrypt and decrypt data. It falls under the
category of surrogate crypto and offers a higher level of security compared to traditional surrogate
crypto such as Caesar or Playfair crypto. Due to its mathematical nature and block-based encryption,
the Hill cipher is considered more secure than simple substitution ciphers. However, it is not widely
used in modern encryption systems due to its limitations and vulnerability to certain attacks. Instead,
practical applications generally use stronger and more efficient encryption algorithms, such as the
Advanced Encryption Standard (AES).

Code:

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

int key[3][3] ; // Global

int mod26(int x)
{
return x >= 0 ? (x%26) : 26-(abs(x)%26) ;
}

/* findDet(matrix , order_of_matrix) */
int findDet(int m[3][3] , int n)
{
int det;
if(n == 2)
{
det = m[0][0] * m[1][1] - m[0][1]*m[1][0] ;
}
else if (n == 3)
{
det = m[0][0]*(m[1][1]*m[2][2] - m[1][2]*m[2][1]) - m[0][1]*(m[1][0]*m[2][2] -
m[2][0]*m[1][2] ) + m[0][2]*(m[1][0]*m[2][1] - m[1][1]*m[2][0]);
}
else det = 0 ; // invalid input
return mod26(det);
}

int findDetInverse(int R , int D = 26) // R is the remainder or determinant


{
int i = 0 ;
int p[100] = {0,1};
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

int q[100] = {0} ; // quotient

while(R!=0)
{
q[i] = D/R ;
int oldD = D ;
D=R;
R = oldD%R ;
if(i>1)
{
p[i] = mod26(p[i-2] - p[i-1]*q[i-2]) ;
}
i++ ;
}
if (i == 1) return 1;
else return p[i] = mod26(p[i-2] - p[i-1]*q[i-2]) ;
}

void multiplyMatrices(int a[1000][3] , int a_rows , int a_cols , int b[1000][3] , int b_rows , int b_cols
, int res[1000][3])
{
for(int i=0 ; i < a_rows ; i++)
{
for(int j=0 ; j < b_cols ; j++)
{
for(int k=0 ; k < b_rows ; k++)
{
res[i][j] += a[i][k]*b[k][j] ;
}
res[i][j] = mod26(res[i][j]) ;
}
}
}

/* Inverse = (matrix * detInverse) mod 26 */


/* findInverse(matrix , order_of_matrix , result_matrix) */
void findInverse(int m[3][3] , int n , int m_inverse[3][3] )
{
int adj[3][3] = {0};

int det = findDet(m , n); // findDet(matrix , order_of_matrix)


int detInverse = findDetInverse(det);

if(n==2)
{
adj[0][0] = m[1][1];
adj[1][1] = m[0][0];
adj[0][1] = -m[0][1];
adj[1][0] = -m[1][0];
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

}
else if(n==3)
{
int temp[5][5] = {0} ;
// fill the 5x5 matrix
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
temp[i][j] = m[i%3][j%3] ;
}
}
/* except first row and first column, multiply elements along rows and place them
along columns */
for(int i=1; i<=3 ; i++)
{
for(int j=1; j<=3 ; j++)
{
adj[j-1][i-1] = temp[i][j]*temp[i+1][j+1] - temp[i][j+1]*temp[i+1][j];
}
}
}

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


{
for(int j=0; j<n ; j++)
{
m_inverse[i][j] = mod26(adj[i][j] * detInverse) ;
}
}
}

// C = PK
string encrypt(string pt, int n)
{
int P[1000][3] = {0} ; // plaintext
int C[1000][3] = {0} ; // cipher text
int ptIter = 0 ;

while(pt.length()%n != 0)
{
pt += "x" ; // pad extra x
}
int row = (pt.length())/n; // number of rows in P

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


{
for(int j=0; j<n; j++)
{
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

P[i][j] = pt[ptIter++]-'a' ;
}
}

// multiplyMatrices(mat_a , row_a , col_a , mat_b, row_b, col_b , mat_result)


multiplyMatrices(P, row , n , key , n , n , C) ;

string ct = "" ;
for(int i=0 ; i<row ; i++)
{
for(int j=0 ; j<n ;j++)
{
ct += (C[i][j] + 'a');
}
}
return ct ;
}

// P = C*(k_inverse)
string decrypt(string ct, int n)
{
int P[1000][3] = {0} ; // plaintext
int C[1000][3] = {0} ; // cipher text
int ctIter = 0 ;

int row = ct.length()/n; // number of rows in C

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


{
for(int j=0; j<n; j++)
{
C[i][j] = ct[ctIter++]-'a' ;
}
}

int k_inverse[3][3] = {0};


/* findInverse(matrix , order_of_matrix , result_matrix) */
findInverse(key, n , k_inverse);

/* multiplyMatrices(mat_a , row_a , col_a , mat_b, row_b, col_b , mat_result) */


multiplyMatrices(C, row , n , k_inverse , n , n , P) ;

string pt = "" ;
for(int i = 0 ; i<row ; i++)
{
for(int j=0 ; j<n ; j++)
{
pt += (P[i][j] + 'a');
}
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

}
return pt ;
}

int main(void)
{
string pt ;
int n ;

cout << "Enter the text to be encrypted :";


cin >> pt;

cout << "Enter order of key matrix : ";


cin >> n ;

cout<<"Enter key matrix: " <<endl;


for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cin >> key[i][j];
}
}

cout << "\nOriginal text : " << pt << endl;

string ct = encrypt(pt, n) ;
cout << "Encrypted text : " << ct << endl;

string dt = decrypt(ct, n);


cout << "Decrypted text : " << dt << endl;
}

Output:
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

Practical – 5

Aim: Implement Polyalphabetic cipher encryption-decryption.

Theory:
Polyalphabetic cipher is a type of cipher that uses multiple alphabets or substitution rules to encode
plaintext. Unlike monoalphabetic ciphers, where each letter is consistently replaced by its
corresponding letter, polyalphabetic ciphers add variety by using different substitution rules based on
the letter's position in the plaintext. Multi-character ciphers, such as Vigenère ciphers, offer better
security than single-character ciphers. encryptions The variability caused by multiple substitution rules
makes it difficult for attackers to perform frequency analysis or other known plaintext attacks.
However, polyalphabetic ciphers can be vulnerable to cryptanalysis techniques, such as the Kasiski
and Friedman tests, which aim to identify patterns and break the cipher.

Code:
#include <iostream>

#include <string>

using namespace std;

string vigenere_encrypt(const string& plain_text, const string& key) {

string encrypted_text = plain_text;

for (size_t i = 0; i < plain_text.length(); i++) {

if (isalpha(plain_text[i])) {

int shift = toupper(key[i % key.length()]) - 'A';

char base_char = islower(plain_text[i]) ? 'a' : 'A';

encrypted_text[i] = (plain_text[i] - base_char + shift) % 26 + base_char;

return encrypted_text;

}
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

string vigenere_decrypt(const string& encrypted_text, const string& key) {

string decrypted_text = encrypted_text;

for (size_t i = 0; i < encrypted_text.length(); i++) {

if (isalpha(encrypted_text[i])) {

int shift = toupper(key[i % key.length()]) - 'A';

char base_char = islower(encrypted_text[i]) ? 'a' : 'A';

decrypted_text[i] = (encrypted_text[i] - base_char - shift + 26) % 26 + base_char;

return decrypted_text;

int main() {

while (true) {

cout << "1. Encrypt" << endl;

cout << "2. Decrypt" << endl;

cout << "3. Exit" << endl;

cout << "Enter your choice (1/2/3): ";

char choice;

cin >> choice;

cin.ignore(); // Ignore any trailing newline character.

switch (choice) {

case '1': {
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

string plaintext, key;

cout << "Enter the plaintext: ";

getline(cin, plaintext);

cout << "Enter the key: ";

getline(cin, key);

string encrypted_text = vigenere_encrypt(plaintext, key);

cout << "Encrypted: " << encrypted_text << endl;

break;

case '2': {

string encrypted_text, key;

cout << "Enter the encrypted text: ";

getline(cin, encrypted_text);

cout << "Enter the key: ";

getline(cin, key);

string decrypted_text = vigenere_decrypt(encrypted_text, key);

cout << "Decrypted: " << decrypted_text << endl;

break;

case '3':

cout << "Exiting..." << endl;

return 0;

default:

cout << "Invalid choice. Please try again." << endl;

}
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

return 0;

Output:
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

Practical – 6
Aim: Implement Simple Transposition encryption-decryption.

Theory:
Simple transfer is a basic technique used in cryptography to encrypt plaintext by rearranging its
characters according to a specific pattern. This is a type of symmetric encryption, which means that
the same key is used for both encryption and decryption. This method involves changing the characters
of the plaintext based on a defined rule, creating ciphertext that appears scrambled. Due to its
simplicity and lack of strong security, Simple Transposition is not recommended for today's encryption
needs. Instead, more powerful and advanced encryption algorithms such as AES (Advanced
Encryption Standard) are used to protect sensitive data and communications.

Encryption code:
#include <iostream>

#include <string>

#include <vector>

using namespace std;

string encrypt(string plaintext, int key) {

int numRows = (plaintext.length() + key - 1) / key;

// Create a 2D grid to hold the characters

vector<vector<char>> grid(numRows, vector<char>(key));

// Fill the grid with the plaintext characters

int index = 0;

for (int i = 0; i < numRows; i++) {

for (int j = 0; j < key; j++) {

if (index < plaintext.length()) {

grid[i][j] = plaintext[index++];

else {
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

// Fill any empty cells with 'X'

grid[i][j] = 'X';

string ciphertext;

for (int j = 0; j < key; j++) {

for (int i = 0; i < numRows; i++) {

if (grid[i][j] != 'X') {

ciphertext += grid[i][j];

return ciphertext;

int main() {

string plaintext, ciphertext;

int key;

cout << "Enter the plaintext: ";

getline(cin, plaintext);

cout << "Enter the key (an integer): ";

cin >> key;

ciphertext = encrypt(plaintext, key);

cout << "Ciphertext: " << ciphertext << endl;

return 0;

}
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

Output:

Decryption Code:
#include <iostream>

#include <string>

#include <vector>

using namespace std;

// Function to decrypt using columnar transposition

string decrypt(string ciphertext, int key) {

// Calculate the number of rows required

int numRows = (ciphertext.length() + key - 1) / key;

// Calculate the number of columns based on numRows and ciphertext length

int numCols = (ciphertext.length() + numRows - 1) / numRows;

// Create a 2D grid to hold the characters

vector<vector<char>> grid(numRows, vector<char>(numCols));

// Fill the grid with the ciphertext characters

int index = 0;

for (int j = 0; j < numCols; j++) {

for (int i = 0; i < numRows; i++) {

if (index < ciphertext.length()) {

grid[i][j] = ciphertext[index++];
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

// Read the characters from the grid row by row to form plaintext

string plaintext;

for (int i = 0; i < numRows; i++) {

for (int j = 0; j < numCols; j++) {

plaintext += grid[i][j];

return plaintext;

int main() {

string ciphertext, plaintext;

int key;

cout << "Enter the ciphertext: ";

getline(cin, ciphertext);

cout << "Enter the key (an integer): ";

cin >> key;

cin.ignore(); // Clear the newline character from the input buffer

plaintext = decrypt(ciphertext, key);

cout << "Decrypted plaintext: " << plaintext << endl;

return 0;

}
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

OUTPUT :
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

PRACTICAL 7
Aim: Implement One-Time Pad Encryption Decryption

Theory:
The One-Time Pad (OTP) is a cryptographic technique that offers perfect secrecy when implemented
correctly. It involves using a random and secret key, with the same length as the plaintext, to encrypt
the message. Each key is used only once, hence the name "one-time pad." XOR (exclusive OR)
operation combines the plaintext and the key, making decryption impossible without the same key.
OTP is unbreakable in theory, as any ciphertext can correspond to any plaintext, providing no
statistical patterns for attackers to exploit. However, it faces practical challenges, such as key
distribution and key secrecy, making it rarely used for large-scale communications today.

Encryption Code:
#include <iostream>

#include <string>

#include <cstdlib>

#include <ctime>

using namespace std;

string generateRandomKey(int length) {

string key;

for (int i = 0; i < length; i++) {

key += static_cast<char>('A' + rand() % 26); // Generate a random uppercase letter

return key;

string oneTimePadEncrypt(const string& plaintext, const string& key) {

if (plaintext.length() != key.length()) {

cerr << "Error: Plaintext and key must have the same length." << endl;

exit(1);

}
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

string ciphertext;

for (size_t i = 0; i < plaintext.length(); i++) {

char encryptedChar = (plaintext[i] ^ key[i]) + 'A'; // XOR operation and convert to uppercase

ciphertext += encryptedChar;

return ciphertext;

int main() {

srand(static_cast<unsigned>(time(nullptr))); // Seed the random number generator

string plaintext;

cout << "Enter the plaintext: ";

getline(cin, plaintext);

string key = generateRandomKey(plaintext.length());

string ciphertext = oneTimePadEncrypt(plaintext, key);

cout << "Ciphertext: " << ciphertext << endl;

return 0;

Output:
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

Practical 8
Aim: Implement Diffi-Hellmen Key exchange Method.

Theory:
Diffie-Hellman is a key exchange algorithm. It allows two parties to securely agree on a shared secret
key over an insecure communication channel. Once they have the shared secret key, they can use it
for encryption and decryption using symmetric encryption algorithms like AES

Input:
#include <iostream>

#include <cmath>

#include <cstdlib>

#include <ctime>

using namespace std;

// Calculate (base^exponent) % modulo using modular exponentiation

long long modExp(long long base, long long exponent, long long modulo) {

if (exponent == 0)

return 1;

long long result = 1;

base = base % modulo;

while (exponent > 0) {

if (exponent % 2 == 1)

result = (result * base) % modulo;

exponent = exponent >> 1; // Equivalent to exponent /= 2

base = (base * base) % modulo;

return result;

int main() {
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

srand(static_cast<unsigned>(time(nullptr)));

// Shared prime number and base/generator

long long prime = 23; // You can choose a larger prime for increased security

long long base = 5;

// Private keys for Alice and Bob

long long privateAlice = rand() % (prime - 1) + 1;

long long privateBob = rand() % (prime - 1) + 1;

// Calculate public keys

long long publicAlice = modExp(base, privateAlice, prime);

long long publicBob = modExp(base, privateBob, prime);

// Exchange public keys over an insecure channel

// Calculate the shared secret

long long secretAlice = modExp(publicBob, privateAlice, prime);

long long secretBob = modExp(publicAlice, privateBob, prime);

// Both Alice and Bob should have the same shared secret

if (secretAlice == secretBob) {

cout << "Shared secret: " << secretAlice << endl;

} else {

cerr << "Key exchange failed!" << endl;

return 0;

Output:
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

Practical 9
Aim: Implement RSA encryption-decryption algorithm.

Theory:
RSA (Rivest-Shamir-Adleman) is a widely used public-key encryption algorithm. In RSA, two keys
are generated—a public key for encryption and a private key for decryption. The security of RSA
relies on the difficulty of factoring large numbers into their prime components. To encrypt a message,
the sender uses the recipient's public key. Only the recipient, holding the private key, can decrypt the
message. RSA provides confidentiality and authentication in secure communications, digital
signatures, and secure key exchange. Its strength lies in the challenge of factoring large semiprime
numbers, making it secure for practical applications when using sufficiently long keys.

Input:
#include<iostream>
#include<math.h>
using namespace std;
// find gcd
int gcd(int a, int b) {
int t;
while(1) {
t= a%b;
if(t==0)
return b;
a = b;
b= t;
}
}
int main() {
//2 random prime numbers
double p = 13;
double q = 11;
double n=p*q;//calculate n
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

double track;
double phi= (p-1)*(q-1);//calculate phi
//public key
//e stands for encrypt
double e=7;
//for checking that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.
while(e<phi) {
track = gcd(e,phi);
if(track==1)
break;
else
e++;
}
//private key
//d stands for decrypt
//choosing d such that it satisfies d*e = 1 mod phi
double d1=1/e;
double d=fmod(d1,phi);
double message = 9;
double c = pow(message,e); //encrypt the message
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);
cout<<"Original Message = "<<message;
cout<<"\n"<<"p = "<<p;
cout<<"\n"<<"q = "<<q;
cout<<"\n"<<"n = pq = "<<n;
cout<<"\n"<<"phi = "<<phi;
cout<<"\n"<<"e = "<<e;
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

cout<<"\n"<<"d = "<<d;
cout<<"\n"<<"Encrypted message = "<<c;
cout<<"\n"<<"Decrypted message = "<<m;
return 0;
}
Output:
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

Practical 10
Aim: Demonstrate working of Digital Signature using Cryptool.

Theory:
Digital signatures are a cryptographic technique used to verify the authenticity and integrity of digital
messages or documents. They involve two main operations: signing and verifying.

Overview of the process:

Key Pair Generation:

• A user generates a key pair consisting of a private key and a public key.
• The private key is kept secret and is used for signing.
• The public key is shared with others and is used for verifying the signatures.

Signing:

• The sender computes a cryptographic hash (e.g., SHA-256) of the message.


• The sender encrypts the hash with their private key, creating a digital signature.
• The digital signature is attached to the message and sent to the recipient.

Verifying:

• The recipient receives the message and the attached digital signature.
• The recipient computes the hash of the received message using the same hash function.
• The recipient decrypts the digital signature using the sender's public key, obtaining a hash.
• If the computed hash matches the decrypted hash, the signature is valid, and the message is
considered authentic and unaltered.

To demonstrate digital signatures using Cryptool:

Key Generation:

• Use Cryptool to generate a key pair, which includes a private key and a corresponding public
key.
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

Signing:

• Input the message you want to sign into Cryptool.


• Cryptool will apply a hash function to generate a hash of the message.
• The tool will then use your private key to create a digital signature by encrypting the hash.

Verifying:

• Input the received message, digital signature, and the sender's public key into Cryptool.
• Cryptool will compute the hash of the received message.
• It will then decrypt the digital signature using the sender's public key, obtaining a hash.
• If the computed hash matches the hash of the received message, Cryptool will confirm that the
signature is valid.
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
INFORMATION AND NETWORK SECURITY (203105311) B. Tech. 4th YEAR
ENROLLMENT NO: 200303105170

You might also like