INS Lab Manual Kush
INS Lab Manual Kush
BACHELOR OF TECHNOLOGY
VII SEMESTER
COMPUTER SCIENCE & ENGINEERING DEPARTMENT
LABORATORY MANUAL
CERTIFICATE
Implement
5. Polyalphabetic cipher
encryption-decryption.
Implement Simple
6. Transposition
encryption-decryption.
Implement Diffie-
7. Hellman Key exchange
Method
Practical – 1
Theory:
• Caesar cipher is a substitution cipher where each letter in the plaintext is
shifted a certain number of places down or up the alphabet.
• It's a simple and historical method of encryption, but it's not very secure
due to its vulnerability to brute force attacks.
• The key idea is shifting letters by a fixed amount to encode and decode
messages.
• For Caesar cipher encryption, each letter in the plaintext is shifted a fixed
number of places down or up the alphabet.
• Let's say we have a shift of 3. So, 'A' becomes 'D', 'B' becomes 'E', and so
on.
• To decrypt, you simply shift back by the same number of places.
• It's a straightforward method but not very secure.
• The key is crucial for both encryption and decryption.
Code :-
else:
# If the character is not a letter, do not change it
encrypted.append(char)
return ''.join(encrypted)
Output:-
2|Page
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
Code:-
Output:-
3|Page
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
Practical – 2
Theory:
• A monoalphabetic cipher is a substitution cipher where each letter in the
plaintext is replaced by a corresponding letter from a fixed substitution
alphabet.
• This method ensures that each letter consistently maps to the same
substitute letter throughout the message.
• Although simple to implement, monoalphabetic ciphers are highly
vulnerable to frequency analysis, as the patterns in letter frequency
remain unchanged from the plaintext to the ciphertext.
• Historical examples, like the Caesar cipher, demonstrate the fundamental
concept of monoalphabetic encryption and its straightforward yet easily
breakable nature.
Code:
def generate_monoalphabetic_cipher():
mp = {}
j = 25
for i in range(26):
mp[chr(ord('a') + i)] = chr(ord('a') + j)
j -= 1
return mp
return ''.join(encrypted)
Output:
5|Page
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
Practical – 3
Theory:
• The Playfair cipher was the first practical digraph substitution cipher.
• In playfair cipher unlike traditional cipher we encrypt a pair of
alphabets(digraphs) instead of a single alphabet.
• Pair cannot be made with same letter.
• Break the letter in single and add a bogus letter to the previous letter.
• If the letter is standing alone in the process of pairing, then add an
extrabogus letter with the alone letter.
• If both the letters are in the same column: Take the letter below
eachone (going back to the top if at the bottom).
Code:
def to_lower_case(text):
return text.lower()
def remove_spaces(text):
return text.replace(" ", "")
def generate_key_table(key):
key = to_lower_case(remove_spaces(key)).replace('j', 'i')
dicty = [0] * 26
key_table = [[""] * 5 for _ in range(5)]
i, j = 0, 0
for k in key:
if k != 'j' and not dicty[ord(k) - ord('a')]:
dicty[ord(k) - ord('a')] = 1
key_table[i][j] = k
j += 1
6|Page
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
if j == 5:
i += 1
j=0
for k in range(26):
if k != 9 and not dicty[k]:
key_table[i][j] = chr(k + ord('a'))
j += 1
if j == 5:
i += 1
j=0
return key_table
def format_message(message):
message = to_lower_case(remove_spaces(message)).replace('j', 'i')
formatted_message = ""
i=0
if len(formatted_message) % 2 != 0:
formatted_message += 'x'
return formatted_message
7|Page
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
if row1 == row2:
encrypted_message += key_table[row1][(col1 + 1) % 5] +
key_table[row2][(col2 + 1) % 5]
elif col1 == col2:
encrypted_message += key_table[(row1 + 1) % 5][col1] + key_table[(row2
+ 1) % 5][col2]
else:
encrypted_message += key_table[row1][col2] + key_table[row2][col1]
return encrypted_message
key = "Keyword"
message = "Hello World"
encrypted_message = playfair_encrypt(message, key)
8|Page
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
Output:
9|Page
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
Decryption Code:-
def to_lower_case(text):
return text.lower()
def remove_spaces(text):
return text.replace(" ", "")
def generate_key_table(key):
key = to_lower_case(remove_spaces(key)).replace('j', 'i')
dicty = [0] * 26
key_table = [[""] * 5 for _ in range(5)]
i, j = 0, 0
for k in key:
if k != 'j' and not dicty[ord(k) - ord('a')]:
dicty[ord(k) - ord('a')] = 1
key_table[i][j] = k
j += 1
if j == 5:
i += 1
j=0
for k in range(26):
if k != 9 and not dicty[k]:
key_table[i][j] = chr(k + ord('a'))
j += 1
if j == 5:
i += 1
j=0
return key_table
def format_message(message):
message = to_lower_case(remove_spaces(message)).replace('j', 'i')
formatted_message = ""
i=0
10 | P a g
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
if len(formatted_message) % 2 != 0:
formatted_message += 'x'
return formatted_message
if row1 == row2:
encrypted_message += key_table[row1][(col1 + 1) % 5] +
key_table[row2][(col2 + 1) % 5]
elif col1 == col2:
encrypted_message += key_table[(row1 + 1) % 5][col1] + key_table[(row2
+ 1) % 5][col2]
else:
encrypted_message += key_table[row1][col2] + key_table[row2][col1]
11 | P a g
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
return encrypted_message
if row1 == row2:
decrypted_message += key_table[row1][(col1 - 1) % 5] +
key_table[row2][(col2 - 1) % 5]
elif col1 == col2:
decrypted_message += key_table[(row1 - 1) % 5][col1] + key_table[(row2 -
1) % 5][col2]
else:
decrypted_message += key_table[row1][col2] + key_table[row2][col1]
return decrypted_message
key = "Keyword"
message = "Hello World"
encrypted_message = playfair_encrypt(message, key)
decrypted_message = playfair_decrypt(encrypted_message, key)
Output:-
12 | P a g
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
13 | P a g
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
Practical – 4
Aim :- A) Hill Cipher Encryption
Theory:
• Hill cipher is a polygraphic substitution cipher based on linear algebra.
• Each letter is represented by a number modulo 26.
• To encrypt a message, each block of n letters (considered as an n-
component vector) is multiplied by an invertible n × n matrix,
againstmodulus 26.
• To decrypt the message, each block is multiplied by the inverse of
thematrix used for encryption.
• The matrix used for encryption is the cipher key, and it should be
chosenrandomly from the set of invertible n × n matrices (modulo 26).
Code:
def to_lower_case(text):
return text.lower()
def remove_spaces(text):
return text.replace(" ", "")
def create_string_from_matrix(matrix):
string = ''.join(chr(num % 26 + ord('a')) for row in matrix for num in row)
return string
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
result[i][j] %= 26
return result
key = "GYBNQKURP"
key_size = 3
message = "ACT"
message = to_lower_case(remove_spaces(message))
Output:
15 | P a g
210303105509
FACULTY OF ENGINEERING & TECHNOLOGY
Subject : Information & Network Security Laboratory
Subject Code: 203105311
B.Tech – 4th Year 7th Semester
def to_lower_case(text):
return text.lower()
def remove_spaces(text):
return text.replace(" ", "")
def create_string_from_matrix(matrix):
string = ''.join(chr(num % 26 + ord('a')) for row in matrix for num in row)
return string
def matrix_inverse(key_matrix):
adjugate_matrix = [
[(key_matrix[1][1] * key_matrix[2][2] - key_matrix[1][2] * key_matrix[2][1]),
-(key_matrix[0][1] * key_matrix[2][2] - key_matrix[0][2] * key_matrix[2][1]),
(key_matrix[0][1] * key_matrix[1][2] - key_matrix[0][2] * key_matrix[1][1])],
return key_inverse
create_matrix_of_integers_from_string(encrypted_message, len(key_matrix)))
decrypted_message = create_string_from_matrix(decrypted_matrix)
return decrypted_message
key = "GYBNQKURP"
key_size = 3
key_matrix = create_matrix_of_integers_from_string(key, key_size)
message = "ACT"
message = to_lower_case(remove_spaces(message))
encrypted_message = create_string_from_matrix(matrix_multiply(key_matrix,
create_matrix_of_integers_from_string(message, key_size)))
OutPut:-
18 | P a g
210303105509
FACULTY OF COMPUTER SCIENCE ANDENGINEERING
INFORMATION AND NETWORK SECURITY LABORATORY
(203105311)
B.TECH CSE 7THSEMESTER 4THYEAR
PRACTICAL-5
AIM: Polyalphabetic Encryption and Decryption
CODE
def generate_key(string, key):
key = list(key)
if len(string) == len(key):
return(key)
else:
key.append(key[i % len(key)])
return("" . join(key))
cipher_text = []
for i in range(len(string)):
x = (ord(string[i]) + ord(key[i])) % 26
x += ord('A')
cipher_text.append(chr(x))
return("" . join(cipher_text))
orig_text = []
for i in range(len(cipher_text)):
x += ord('A')
orig_text.append(chr(x))
return("" . join(orig_text))
210303105509
FACULTY OF COMPUTER SCIENCE ANDENGINEERING
INFORMATION AND NETWORK SECURITY LABORATORY
(203105311)
B.TECH CSE 7THSEMESTER 4THYEAR
if name == " main ":
output
210303105509
FACULTY OF COMPUTER SCIENCE ANDENGINEERING
INFORMATION AND NETWORK SECURITY LABORATORY
(203105311)
B.TECH CSE 7THSEMESTER 4THYEAR
PRACTICAL-6
AIM: Simple Transposition Encryption and Decryption.
CODE :
def generate_key(string, key):
key = list(key)
if len(string) == len(key):
return(key)
else:
for i in range(len(string) - len(key)):
key.append(key[i % len(key)])
return("" . join(key))
def encrypt(string, key):
cipher_text = []
for i in range(len(string)):
x = (ord(string[i]) + ord(key[i])) % 26
x += ord('A')
cipher_text.append(chr(x))
return("" . join(cipher_text))
210303105509
FACULTY OF COMPUTER SCIENCE ANDENGINEERING
INFORMATION AND NETWORK SECURITY LABORATORY
(203105311)
B.TECH CSE 7THSEMESTER 4THYEAR
orig_text.append(chr(x))
return("" . join(orig_text))
if name == " main ":
string = input("Enter the plaintext: ").upper()
keyword = input("Enter the keyword: ").upper()
OUTPUT
210303105509
FACULTY OF COMPUTER SCIENCE ANDENGINEERING
INFORMATION AND NETWORK SECURITY LABORATORY
(203105311)
B.TECH CSE 7THSEMESTER 4THYEAR
PRACTICAL-7
AIM:-Diffie- Heilman key exchange method\
CODE;-
import random
def mod_exp(base, exp, mod):
result = 1
while exp > 0:
if exp % 2 == 1: # If exp is odd
result = (result * base) % mod
exp = exp // 2
base = (base * base) % mod
return result
def diffie_hellman(p, g):
a = random.randint(1, p-1)
A = mod_exp(g, a, p)
b = random.randint(1, p-1)
B = mod_exp(g, b, p)
alice_secret = mod_exp(B, a, p)
bob_secret = mod_exp(A, b, p)
return alice_secret, bob_secret
if name == " main "
p = int(input("Enter a large prime number (p): "))
g = int(input("Enter a base (g): "))
210303105509
FACULTY OF COMPUTER SCIENCE ANDENGINEERING
INFORMATION AND NETWORK SECURITY LABORATORY
(203105311)
B.TECH CSE 7THSEMESTER 4THYEAR
210303105509
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
Practical - 8
AIM: Implement Diffie Hellman key exchange method.
Theory:
The Diffie-Hellman key exchange is a cryptographic protocol that allows two parties to securely
share a secret key over an insecure channel. It works by each party selecting a private key, then
generating and exchanging public keys based on a shared prime number and base. Both parties can
then independently compute the same shared secret using their own private key and the other
party's public key. The security of the protocol relies on the difficulty of solving the discrete
logarithm problem, making it secure even if the exchanged public keys are intercepted.
Key Concepts:
1. Public Parameters:
o Prime number p: A large prime number.
o Base g: A primitive root modulo p, typically a small integer.
2. Private Keys:
o Alice’s private key a: A secret number randomly chosen by Alice.
o Bob’s private key b: A secret number randomly chosen by Bob.
These private keys are kept secret and are not shared with anyone.
3. Public Keys:
o Alice’s public key A: Calculated as A=ga mod p.
o Bob’s public key B: Calculated as B=gb mod p.
1. Global Parameters:
▪ Both Alice and Bob agree on a common prime number p and a base g, where g is a
primitive root modulo p.
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
▪ Alice computes her public key YA using the formula YA= gXA mod p.
▪ Bob computes his public key YB using the formula YB=. gX Bmod p.
▪ Alice and Bob share their public keys YA and YB with each other.
Both Alice and Bob will arrive at the same shared secret key K, which can be used for secure
communication. The exchange process relies on the difficulty of calculating the discrete logarithm,
ensuring that the shared secret remains secure even if an eavesdropper knows the public keys.
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
Code:
shared_secret_alice = modular_pow(B, a, p)
shared_secret_bob = modular_pow(A, b, p)
if shared_secret_alice == shared_secret_bob:
print("\nSuccess! The shared secrets match.")
else:
print("\nError! The shared secrets do not match.")
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
Output:
Result:
The implementation of Diffie Hellman key exchange algorithm encryption and decryption has
been successfully completed.
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
Practical - 9
AIM: Implement RSA encryption-decryption algorithm.
Theory:
The RSA algorithm (Rivest-Shamir-Adleman) is one of the first public-key cryptosystems and is
widely used for secure data transmission. It is an asymmetric encryption algorithm, meaning it
uses two different keys: a public key for encryption and a private key for decryption.
1. Key Generation:
• Choose two large prime numbers, p and q.
• Compute n = p×q. This is used as part of both the public and private keys.
• Compute Euler's totient function ϕ(n)=(p−1)×(q−1).
• Choose an integer e such that 1<e<ϕ(n) and e is coprime with ϕ(n). This e becomes
the public key exponent.
• Compute d, the modular multiplicative inverse of e, such that d×e≡1(mod ϕ (n)).
This d becomes the private key exponent.
• Public Key: (n,e)
• Private Key: (n,d)
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
Code:
import random
n=p*q
phi = (p - 1) * (q - 1)
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
# Main program
def main():
# Key generation
public_key, private_key = generate_keys()
print(f"Public Key: {public_key}")
print(f"Private Key: {private_key}")
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
Output:
Result:
The implementation of RSA algorithm encryption and decryption has been successfully
completed.
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
Practical - 10
AIM: Demonstrate working of Digital Signature using Cryptool.
Theory:
Digital Signature:
A digital signature in cryptography is a mechanism that ensures the authenticity, integrity, and non-
repudiation of digital messages or documents. It's similar to a handwritten signature or a stamped
seal but offers far more security. Digital signatures use asymmetric cryptography, which involves
a pair of keys: a private key and a public key.
1. Private Key: Used by the signer to create the signature. Only the owner of the private key
can sign the document.
2. Public Key: Used by others to verify the signature. Anyone with the public key can check
whether the document was signed by the corresponding private key.
Process:
1. Hashing: First, the document or message is hashed using a cryptographic hash function
(e.g., SHA-256). This creates a fixed-size output (hash) representing the document.
2. Signing: The hash of the document is then encrypted with the signer's private key. This
encrypted hash is the digital signature.
3. Verification:
o The receiver decrypts the signature using the sender's public key, obtaining the
original hash.
o The receiver also hashes the received document using the same hash function.
o If the decrypted hash matches the hash of the received document, the signature is
valid, confirming that the document hasn't been altered and was signed by the
legitimate private key owner.
CrypTool:
CrypTool is an open-source project that provides a suite of tools and educational resources for
learning and experimenting with cryptography. It is widely used by students, educators, and
cryptography enthusiasts to understand cryptographic concepts and algorithms through a hands-
on approach.
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
4. CrypTool Variants: There are several versions of CrypTool, each designed for different
needs:
o CrypTool 1 (CT1): A desktop application focused on classical and modern
cryptography.
o CrypTool 2 (CT2): Provides a modern, flexible environment for experimenting
with cryptographic algorithms. It includes drag-and-drop functionality for visual
programming and workflows.
o JCrypTool (JCT): A Java-based version for cross-platform usage, with a focus on
modularity.
o CrypTool Online (CTO): A web-based version that allows users to experiment
with cryptography through their web browser without needing to install software.
5. Analysis Tools: CrypTool offers tools for analyzing cryptographic strengths, such as:
o Frequency analysis: For breaking classical ciphers (Caesar, Vigenère, etc.).
o Side-channel attack simulations
o Statistical tests on the randomness of cipher outputs.
Following are the steps involved in generating a digital signature using cryptool:
1. Launch CrypTool: Open CrypTool, a software that offers various cryptographic functions.
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
2. Load/Enter the Message: Input the message or file you want to sign. You can either type
the text manually or import a file.
3. Select Hash Function: Choose a hash function (e.g., SHA-256) to generate a fixed-size
hash (message digest) from the original message.
4. Choose Asymmetric Key Algorithm: Select a public-key cryptography algorithm like RSA
or DSAfor signing. You will need a private key for signing and a public key for verification.
5. Generate Key Pair: If you don’t already have a key pair, generate a new one (private and
public key). The private key will be used to create the digital signature.
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
210303105509 |
Faculty of Engineering & Technology
Subject name & code : Information &
Network Security (203105311)
B.Tech CSE Year: 4th Sem : 7th
6. Sign the Message: Using the private key, the message digest (from step 3) is encrypted,
producing the digital signature.
7. Save or Export: Save or export the digital signature along with the original message if
needed.
8. Verify the Signature (optional): Use the public key to verify the digital signature to ensure
the message hasn’t been tampered with.
These steps allow the sender to sign the message, ensuring authenticity and integrity.
210303105509 |