Professional Documents
Culture Documents
Practical Record
Submitted by
AMAR MEHTA
NILESH GARG(RA2011050010027)
(RA2011050010035)
BONAFIDE CERTIFICATE
Certified that this is the bonafide record of the work done in the 18CSE435J -
Advanced Cryptography Laboratory during the seventh semester in the
academic year 2023 - 2024.
NILESH
Name: Amar mehta GARG
RA2011050010035
Register Number:RA2011050010027
Year / Semester / Section: IV / VII / U2
Program: Blockchain Technology
Examiner 1 Examiner 2
List of Exercises
Sl. Page
No.
Ex. No. Date Title No.
Remarks
Implementation of Diffie-Hellman
7 Ex. 7
Algorithm
Aim:
a) Caesar’s Cipher
b) ROT-13
Algorithm / Procedure:
● Begin a function named encrypt_text that takes two parameters: plaintext (the text to be
encrypted) and n (the shift value).
● Initialize two empty strings, ans and ans1, to store the encrypted text.
● For each character ch in the plaintext:
a. If ch is a space, append a space character to both ans and ans1.
b. If ch is an uppercase letter:
- Append a character to ans obtained by shifting ch cyclically by n positions within uppercase
letters.
- Append a character to ans1 obtained by shifting ch cyclically by ROT13 positions within
uppercase letters.
c. If ch is a lowercase letter:
- Append a character to ans obtained by shifting ch cyclically by n positions within lowercase
letters.
- Append a character to ans1 obtained by shifting ch cyclically by ROT13 positions within
lowercase letters.
● Print "Cipher Text is(Caesar Cipher): '' followed by the result of calling encrypt_text with
plaintext and n as arguments.
● Print "Cipher Text is(ROT-13): " followed by the result of calling encrypt_text with plaintext
and ROT13 as arguments.
Program / Code:
def encrypt_text(plaintext,n):
ans = ""
ans1 = ""
for i in range(len(plaintext)):
ch = plaintext[i]
if ch==" ":
ans+=" "
ans1+=" "
elif (ch.isupper()):
else:
return ans
n = 3
ROT13 = 13
Output:
Results:
The implementation of substitution ciphers (Caesar Cipher and ROT-13) is executed and verified
successfully.
18CSE435J - Advanced Cryptography
Aim:
Algorithm / Procedure:
generateKey Function:
● Initialize the generateKey function that takes string and key as inputs.
● Convert key into a list of characters.
● If string and key have equal lengths, return key as is.
● If lengths are different, extend the key by appending characters from itself in a circular
manner to match string length.
● Return the modified key as a string.
encryption Function:
● Initialize the encryption function that takes string and key as inputs.
● Generate encrypted text by iterating over each character in string.
● Calculate new character values using modular arithmetic based on key characters.
● Convert calculated values back to alphabetic characters and join them into the encrypted
text.
● Return the encrypted text as a string.
decryption Function:
● Initialize the decryption function that takes encrypt_text and key as inputs.
● Generate original text by iterating over each character in encrypt_text.
● Calculate new character values using modular arithmetic and the original key characters.
● Convert calculated values back to alphabetic characters and join them into the original text.
● Return the original text as a string.
Main Program:
key = list(key)
if len(string) == len(key):
return(key)
else:
key.append(key[i % len(key)])
return("" . join(key))
encrypt_text = []
for i in range(len(string)):
x = (ord(string[i]) +ord(key[i])) % 26
x += ord('A')
encrypt_text.append(chr(x))
return("" . join(encrypt_text))
orig_text = []
for i in range(len(encrypt_text)):
x += ord('A')
orig_text.append(chr(x))
return("" . join(orig_text))
if __name__ == "__main__":
encrypt_text = encryption(string,key)
print("Encrypted message:", encrypt_text)
Output:
Results:
Exercise No. 3
Implementation of Transposition Cipher Techniques
Date of Exercise 09.08.2023
Aim:
Algorithm / Procedure:
encryptMsg Function:
● Initialize encryptMsg function with msg (char array) and key (int).
● Set msgLen to strlen(msg).
● Initialize variables i, j, k = -1, row = 0, col = 0.
● Create a railMatrix 2D array with newline chars.
● Iterate through msg:
● Place the char in railMatrix, move col.
● Toggle k for first/last row, update row.
● Print "Encrypted Message:".
● Print characters from railMatrix without newline chars.
decryptMsg Function:
● Initialize decryptMsg function with enMsg (char array) and key (int).
● Set msgLen to strlen(enMsg).
● Initialize variables i, j, k = -1, row = 0, col = 0, m = 0.
● Create railMatrix 2D array with newline chars.
● Iterate through enMsg:
● Mark cell in railMatrix, move col.
● Toggle k for first/last row, update row.
● Fill marked cells with chars from enMsg sequentially.
● Initialize row, col, k for decryption.
● Print "Decrypted Message:".
● Iterate through railMatrix, print chars and adjust direction.
Main Program:
#include<string.h>
char railMatrix[key][msgLen];
railMatrix[i][j] = '\n';
railMatrix[row][col++] = msg[i];
k= k * (-1);
row = row + k;
if(railMatrix[i][j] != '\n')
cout<<railMatrix[i][j];
char railMatrix[key][msgLen];
railMatrix[i][j] = '\n';
railMatrix[row][col++] = '*';
if(row == 0 || row == key-1)
k= k * (-1);
row = row + k;
if(railMatrix[i][j] == '*')
railMatrix[i][j] = enMsg[m++];
row = col = 0;
k = -1;
cout<<railMatrix[row][col++];
k= k * (-1);
row = row + k;
int main(){
int key = 3;
encryptMsg(msg, key);
decryptMsg(enMsg, key);
return 0;
}
Output:
Results:
The implementation of the rail fence cipher is executed and verified successfully.
18CSE435J - Advanced Cryptography
Exercise No. 4
Implementation of DES Algorithm
Date of Exercise 18.08.2023
Aim:
Algorithm / Procedure:
● Import the necessary modules from the Crypto library: DES for the encryption algorithm and
get_random_bytes for key generation.
Key Generation:
● Create a DES cipher object using the generated key and the ECB (Electronic Codebook) mode.
ECB mode encrypts each block of data independently, which is less secure than other modes
like CBC.
Encryption:
● Encrypt the padded message using the created DES cipher object.
Output:
key = get_random_bytes(8)
print("Key:", key)
print("Encrypted:", ciphertext)
Decryption:-
key = b'\x9c\xdc\x9c\xa2M\xb1Q3'
ciphertext = cipher.encrypt(padded_message)
print("Key:", key)
print("Encrypted:", ciphertext)
decrypted_padded = cipher.decrypt(ciphertext)
decrypted_message = decrypted_padded.rstrip(b'\x00')
print("Decrypted:", decrypted_message.decode('utf-8'))
Output:
Results:
Exercise No. 5
Implementation of Blowfish Algorithm
Date of Exercise 12.09.2023
Aim:
Algorithm / Procedure:
Import Libraries:
User Input:
Encryption Setup:
● Create a Blowfish cipher object using the encryption key and IV in Cipher Block Chaining
(CBC) mode.
Decryption Setup:
● Create a new Blowfish cipher object for decryption using the same encryption key and IV in
CBC mode.
Remove Padding:
● Use the unpad_text function to remove the padding from the decrypted text.
● Display the decrypted text.
Program / Code:
Step 1:-
Step 2:-
from Crypto.Cipher import Blowfish
def pad_text(text):
pad_length = 8 - (len(text) % 8)
def unpad_text(text):
pad_length = text[-1]
return text[:-pad_length]
if len(key) != 8:
exit(1)
plaintext = pad_text(plaintext)
iv = Random.new().read(8)
ciphertext = cipher.encrypt(plaintext)
decrypted_text = unpad_text(cipher.decrypt(ciphertext))
Results:
Exercise No. 6
Implementation of Rijndael Algorithm
Date of Exercise 12.09.2023
Aim:
Note:- Rijndael is a symmetric key block cipher algorithm designed by Vincent Rijmen and Joan
Daemen. It was selected as the encryption standard by the U.S. National Institute of Standards and
Technology (NIST) in 2001 and is known as AES (Advanced Encryption Standard) when used in that
standardized form.
Algorithm / Procedure:
Program / Code:
def pad_text(text):
def unpad_text(text):
pad_length = text[-1]
return text[:-pad_length]
key = get_random_bytes(16)
ciphertext = cipher.encrypt(plaintext)
decrypted_text = unpad_text(decipher.decrypt(ciphertext))
Output:
Results:
Exercise No. 7
Implementation of Diffie-Hellman Algorithm
Date of Exercise 12.09.2023
Aim:
(The Diffie-Hellman key exchange is a cryptographic method that allows two parties to establish a
shared secret key over an insecure communication channel, without directly exchanging their secret
keys. This shared secret key can then be used for secure communication or encryption.)
Algorithm / Procedure:
Alice:
● Generate a private key a_private, a random integer within the range [1, p-1].
Bob:
● Generate a private key b_private, a random integer within the range [1, p-1].
Alice:
Bob:
● Alice and Bob exchange their public keys over a secure channel.
Alice:
Bob:
● Both Alice and Bob now have the same shared secret, which can be used as a secret key for
secure communication.
Program / Code:
a_public = (g ** a_private) % p
b_public = (g ** b_private) % p
# The shared secret should be the same for both Alice and Bob
Results:
Exercise No. 8
Implementation of RSA Algorithms
Date of Exercise 20.09.2023
Aim:
Algorithm / Procedure:
Key Generation:
● Compute n = p * q, which will be the modulus for both the public and private keys.
● Calculate φ(n) = (p - 1) * (q - 1), where φ is Euler's totient function.
● Choose a public exponent e, where 1 < e < φ(n), and gcd(e, φ(n)) = 1.
● Calculate the private exponent d, which is the modular multiplicative inverse of e modulo
φ(n), i.e., d * e ≡ 1 (mod φ(n)).
Keys:
Encryption:
Decryption:
Program / Code:
import random
import math
def is_prime(num):
if num < 2:
return False
if num % i == 0:
return False
return True
while b:
a, b = b, a % b
return a
m0, x0, x1 = m, 0, 1
while a > 1:
q = a // m
m, a = a % m, m
x0, x1 = x1 - q * x0, x0
def generate_prime(bits):
while True:
num = random.getrandbits(bits)
if is_prime(num):
return num
def generate_keypair(bits):
p = generate_prime(bits)
q = generate_prime(bits)
n=p*q
phi = (p - 1) * (q - 1)
e = random.randint(2, phi - 1)
e = random.randint(2, phi - 1)
d = mod_inverse(e, phi)
n, e = public_key
return cipher
n, d = private_key
return ''.join(plaintext)
bits = int(input("Enter the number of bits for the key size: "))
Results:
Exercise No. 9
Implementation of Digital Signature Standards
Date of Exercise 16.10.2023
Aim:
Digital Signature Standards (DSS) are cryptographic standards that define the algorithms and
protocols used for digital signatures. In the context of DSS, the Digital Signature Algorithm (DSA) is a
widely used algorithm for generating digital signatures.
Algorithm / Procedure:
Key Generation:
Signing:
Hash the input message using the SHA-256 hashing algorithm to create a message digest.
Sign the message digest using the private key to generate a digital signature.
Verification:
Hash the original input message using the SHA-256 hashing algorithm to recreate the message
digest.
Verify the digital signature using the public key and the recreated message digest.
Output:
Program / Code:
public_key = key.publickey().export_key()
private_key = key.export_key()
# Step 2: Signing
signature = signer.sign(hash_object)
# Step 3: Verification
try:
print("Signature is valid.")
except ValueError:
print("Signature is invalid.")
print("\nPublic Key:")
print(public_key.decode())
print("\nPrivate Key:")
print(private_key.decode())
Output:
Results:
Exercise No. 10
Set Up a Honeypot and Honeypot on Network
Date of Exercise 16.10.2023
Aim:
Algorithm / Procedure:
Prerequisites:
Steps:
Install Honeyd:
Install Honeyd on your dedicated machine or VM. You can find the software at Honeyd's official
website.
Create a configuration file (e.g., honeypot.conf) to define the services you want to emulate. Here's a
simple example:
create default
Ensure the machine is on a separate network or VLAN, isolated from your production network.
Run Honeyd with the Configuration File:
Monitor Logs:
Honeyd logs activities to the console. Monitor the console for any connection attempts to the
emulated services.
Install Wireshark on the honeypot machine to capture network traffic for detailed analysis.
Analysis:
Analyze the logs and captured traffic to understand the nature of connection attempts. Look for port
scans, connection requests, or any other suspicious activities.
Edit the configuration file to add or modify services based on your experiment goals. For example,
emulate an SSH server, a database server, etc.
When you're done with the experiment, gracefully shut down the Honeyd process.
Output:
Results:
The implementation of a honeypot and honeypot on the network is executed and verified
successfully.