You are on page 1of 9

Cryptography & Information Security

A Lab Manual
Submitted in Partial Fulfillment of the Requirement for the Award of the Degree of
BACHELOR OF TECHNOLOGY
in
COMPUTER SCIENCE & ENGINEERING
SUBMITTED TO

Rajiv Gandhi Proudyogiki Vishwavidyalaya Bhopal (M.P.)

SUBMITTED BY
Amit Bhattacharya (Enrollment No.: 0176CS201027)
Lakshmi Narain College of Technology Excellence, Bhopal (M.P.), India

UNDER THE SUPERVISION OF


Prof. Kirti Verma
Professor, Department of Computer Science & Engineering
Lakshmi Narain College of Technology Excellence, Bhopal (M.P.)
India

Department of Computer Science & Engineering


Lakshmi Narain College of Technology Excellence, Bhopal (M.P.)
Session: December 2023
LIST OF EXPERIMENTS

SERIAL NO. NAME OF EXPERIMENT DATE OF


SUBMISSION
1 Implementation of Block Cipher and Play Fair Cipher

2 Implementation of DES (S-DES) symmetric-key block


cipher

3 Implementation of RSA algorithm as a public key


encryption technique

4 Implementation of Diffie-Hellman Key Exchange


Computational and AES

5 Implementation of Hashing functions: SHA-1 algorithm


and Cryptographic Hash Function

6 Implementation of Digital signature

7 Implementation of Foot printing

8 Implementation of IP security methods

9 Working and advantages of Steganography tools

10 DoS attack and understanding tools

Internal External
EXPERIMENT-1
AIM: Write the code to implement Block Cipher and Play Fair Cipher with an example
output.
PROGRAM:
1. //Block cipher
#include <iostream>
#include <string>

std::string encryptBlockCipher(const std::string& plaintext, int shift) {


std::string ciphertext = plaintext;

for (char& ch : ciphertext) {


if (isalpha(ch)) {
char base = isupper(ch) ? 'A' : 'a';
ch = (ch - base + shift) % 26 + base;
}
}

return ciphertext;
}

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

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


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

std::cout << "Enter shift value: ";


std::cin >> shift;

std::string ciphertext = encryptBlockCipher(plaintext, shift);

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

return 0;
}
OUTPUT:
2. //Play fair cipher
#include <iostream>
#include <vector>
#include <algorithm>

// Function to generate the key table


std::vector<std::vector<char>> generateKeyTable(const std::string& key) {
std::vector<std::vector<char>> table(5, std::vector<char>(5, 0));
std::string keyWithoutDuplicates = key;

// Remove duplicate characters from the key


keyWithoutDuplicates.erase(std::remove_if(keyWithoutDuplicates.begin(),
keyWithoutDuplicates.end(),
[](char c) { return !isalpha(c); }), keyWithoutDuplicates.end());

// Fill in the key table


std::string alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"; // Note: 'J' is omitted
std::string keyAndAlphabet = keyWithoutDuplicates + alphabet;

auto it = keyAndAlphabet.begin();

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


for (int j = 0; j < 5; ++j) {
while (std::count(keyAndAlphabet.begin(), it, *it) > 1) {
++it;
}

table[i][j] = *it++;
}
}

return table;
}

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


void findPosition(const std::vector<std::vector<char>>& table, char ch, int& row,
int& col) {
if (ch == 'J') ch = 'I'; // 'J' and 'I' are usually treated as the same letter in Playfair
cipher

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


for (int j = 0; j < 5; ++j) {
if (table[i][j] == ch) {
row = i;
col = j;
return;
}
}
}
}

// Function to encrypt using Playfair cipher


std::string encryptPlayfair(const std::string& plaintext, const
std::vector<std::vector<char>>& table) {
std::string ciphertext;
size_t len = plaintext.length();
char first, second;
int row1, col1, row2, col2;

for (size_t i = 0; i < len; i += 2) {


first = plaintext[i];
second = (i + 1 < len) ? plaintext[i + 1] : 'X';

findPosition(table, first, row1, col1);


findPosition(table, second, row2, col2);

if (row1 == row2) {
ciphertext += table[row1][(col1 + 1) % 5];
ciphertext += table[row2][(col2 + 1) % 5];
} else if (col1 == col2) {
ciphertext += table[(row1 + 1) % 5][col1];
ciphertext += table[(row2 + 1) % 5][col2];
} else {
ciphertext += table[row1][col2];
ciphertext += table[row2][col1];
}
}

return ciphertext;
}

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

std::cout << "Enter the key for Playfair cipher: ";


std::cin >> key;

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


std::cin >> plaintext;
// Generate the key table
std::vector<std::vector<char>> keyTable = generateKeyTable(key);

// Encrypt the plaintext


std::string ciphertext = encryptPlayfair(plaintext, keyTable);

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

return 0;
}

OUTPUT:

EXPERIMENT-2
AIM: Implement Simplified DES (S-DES) as a symmetric-key block cipher. The S-DES
encryption algorithm takes an 8-bit block of plaintext and a 10-bit key as an input and
produces an 8-bit block of cipher text as its output.
PROGRAM:
def permute(input_block, permutation):

return [input_block[i - 1] for i in permutation]

def generate_keys(key):

p10_key = permute(key, [3, 5, 2, 7, 4, 10, 1, 9, 8, 6])

left_half = p10_key[:5]

right_half = p10_key[5:]

shifted_left_half = left_half[1:] + [left_half[0]]

shifted_right_half = right_half[1:] + [right_half[0]]

key1 = permute(shifted_left_half + shifted_right_half, [6, 3, 7, 4, 8, 5, 10, 9])

double_shifted_left_half = shifted_left_half[1:] + [shifted_left_half[0]]

double_shifted_right_half = shifted_right_half[1:] + [shifted_right_half[0]]

key2 = permute(double_shifted_left_half + double_shifted_right_half, [6, 3, 7, 4, 8, 5, 10, 9])

return key1, key2

def f_function(input_block, key):

expanded_input = permute(input_block, [4, 1, 2, 3, 2, 3, 4, 1])

xor_result = [bit1 ^ bit2 for bit1, bit2 in zip(expanded_input, key)]

left_half = xor_result[:4]

right_half = xor_result[4:]
s_box_output = s_box(left_half, S0) + s_box(right_half, S1)

return permute(s_box_output, [2, 4, 3, 1])

def s_box(input_bits, s_box_table):

row = 2 * input_bits[0] + input_bits[3]

col = 2 * input_bits[1] + input_bits[2]

return list(map(int, list(format(s_box_table[row][col], '02b'))))

def initial_permutation(plaintext):

return permute(plaintext, [2, 6, 3, 1, 4, 8, 5, 7])

def inverse_permutation(ciphertext):

return permute(ciphertext, [4, 1, 3, 5, 7, 2, 8, 6])

def encrypt_sdes(plaintext, key):

initial_permuted_text = initial_permutation(plaintext)

left_half = initial_permuted_text[:4]

right_half = initial_permuted_text[4:]

key1, key2 = generate_keys(key)

new_left_half1 = right_half

f_result1 = f_function(right_half, key1)

new_right_half1 = [bit1 ^ bit2 for bit1, bit2 in zip(left_half, f_result1)]


new_left_half2 = new_right_half1

f_result2 = f_function(new_right_half1, key2)

new_right_half2 = [bit1 ^ bit2 for bit1, bit2 in zip(right_half, f_result2)]

final_ciphertext = inverse_permutation(new_left_half2 + new_right_half2)

return final_ciphertext

# Example usage:

plaintext = [1, 1, 0, 1, 1, 0, 1, 0] # 8-bit plaintext

key = [1, 0, 0, 1, 0, 0, 1, 0, 1, 1] # 10-bit key

S0 = [

[1, 0, 3, 2],

[3, 2, 1, 0],

[0, 2, 1, 3],

[3, 1, 3, 2]

S1 = [

[0, 1, 2, 3],

[2, 0, 1, 3],

[3, 0, 1, 0],

[2, 1, 0, 3]

ciphertext = encrypt_sdes(plaintext, key)

print("Ciphertext:", ciphertext)

OUTPUT:

You might also like