IS
IS
IS
PRACTICAL 1
AIM : Implement Caesar cipher encryption-decryption.
CODE: def encrypt(text,s):
result = ""
for i in range(len(text)):
char = text[i]
if (char.isupper()):
result += chr((ord(char) + s-65) % 26 + 65)
else:
result += chr((ord(char) + s - 97) % 26 + 97)
return result
text = "ATTACKATONCE"
s=4
print("Text : " + text)
print("Shift : " + str(s))
print("Cipher: " + encrypt(text,s))
OUTPUT:
1
INFORMATION SECURITY (3170720) 191390107018
PRACTICAL 2
AIM: Implement Brute Force attack on Caesar cipher.
CODE:
def caesar_cipher_brute_force(ciphertext):
alphabet = "abcdefghijklmnopqrstuvwxyz"
ciphertext = ciphertext.lower()
print("Ciphertext: \"{}\"".format(ciphertext))
print("\nAttempting to decrypt using brute force:\n")
2
INFORMATION SECURITY (3170720) 191390107018
if __name__ == "__main__":
encrypted_text = input("Enter the ciphertext to decrypt: ")
caesar_cipher_brute_force(encrypted_text)
OUTPUT:
3
INFORMATION SECURITY (3170720) 191390107018
4
INFORMATION SECURITY (3170720) 191390107018
PRACTICAL 3
def generate_key():
alphabet = "abcdefghijklmnopqrstuvwxyz"
shuffled = list(alphabet)
random.shuffle(shuffled)
return dict(zip(alphabet, shuffled))
5
INFORMATION SECURITY (3170720) 191390107018
if __name__ == "__main__":
key = generate_key()
print("Generated Key:", key)
6
INFORMATION SECURITY (3170720) 191390107018
OUTPUT:
Generated Key: {'a': 'y', 'b': 'g', 'c': 't', 'd': 'u', 'e': 'r', 'f': 'c', 'g': 'z', 'h': 'a',
'i': 'b', 'j': 'k', 'k': 'm', 'l': 's', 'm': 'i', 'n': 'd', 'o': 'o', 'p': 'l', 'q': 'n', 'r': 'p', 's':
'f', 't': 'q', 'u': 'w', 'v': 'j', 'w': 'h', 'x': 'v', 'y': 'e', 'z': 'x'}
Enter the plaintext to encrypt: hello how are you
Encrypted Text: arsso aoh ypr eow
Decrypted Text: hello how are you
7
INFORMATION SECURITY (3170720) 191390107018
PRACTICAL 4
AIM: Implement Playfair cipher encryption-decryption.
CODE:
def generate_playfair_key(keyword):
alphabet = "abcdefghiklmnopqrstuvwxyz"
keyword = "".join(dict.fromkeys(keyword.lower().replace("j", "i")))
key_string = keyword + "".join([char for char in alphabet if char not in
keyword])
return [key_string[i:i + 5] for i in range(0, 25, 5)]
def preprocess_text(text):
text = text.lower().replace("j", "i").replace(" ", "")
digraphs = []
i=0
while i < len(text):
char1 = text[i]
if i + 1 < len(text) and text[i + 1] != char1:
char2 = text[i + 1]
i += 2
else:
char2 = "x" if char1 != "x" else "z"
i += 1
8
INFORMATION SECURITY (3170720) 191390107018
digraphs.append(char1 + char2)
return digraphs
if row1 == row2:
result += key_matrix[row1][(col1 + shift) % 5] +
key_matrix[row2][(col2 + shift) % 5]
elif col1 == col2:
result += key_matrix[(row1 + shift) % 5][col1] + key_matrix[(row2
+ shift) % 5][col2]
else:
result += key_matrix[row1][col2] + key_matrix[row2][col1]
9
INFORMATION SECURITY (3170720) 191390107018
return result
if __name__ == "__main__":
keyword = input("Enter the keyword: ")
key_matrix = generate_playfair_key(keyword)
digraphs = preprocess_text(text)
result = playfair_encrypt_decrypt(digraphs, key_matrix,
mode=choice)
10
INFORMATION SECURITY (3170720) 191390107018
OUTPUT:
11
INFORMATION SECURITY (3170720) 191390107018
PRACTICAL 5
AIM: Implement Polyalphabetic cipher encryption-decryption.
CODE:
def generate_vigenere_table():
alphabet = "abcdefghijklmnopqrstuvwxyz"
table = []
for i in range(26):
table.append(alphabet[i:] + alphabet[:i])
return table
12
INFORMATION SECURITY (3170720) 191390107018
return ciphertext
13
INFORMATION SECURITY (3170720) 191390107018
plaintext += c
return plaintext
if __name__ == "__main__":
choice = input("Choose mode (encrypt/decrypt): ").strip().lower()
text = input("Enter the text: ")
key = input("Enter the key: ")
if choice == "encrypt":
result = vigenere_encrypt(text, key)
print("Encrypted Text:", result)
elif choice == "decrypt":
result = vigenere_decrypt(text, key)
print("Decrypted Text:", result)
else:
print("Invalid choice. Please choose either 'encrypt' or 'decrypt'.")
14
INFORMATION SECURITY (3170720) 191390107018
OUTPUT:
15
INFORMATION SECURITY (3170720) 191390107018
PRACTICAL 6
AIM: Implement Hill cipher encryption-decryption.
CODE:
import numpy as np
from sympy import Matrix
def matrix_to_text(matrix):
flat_matrix = matrix.flatten()
return "".join(chr(num + ord('a')) for num in flat_matrix)
16
INFORMATION SECURITY (3170720) 191390107018
plaintext_matrix = np.dot(ciphertext_matrix,
key_matrix_mod_inverse) % 26
return matrix_to_text(plaintext_matrix)
if __name__ == "__main__":
choice = input("Choose mode (encrypt/decrypt): ").strip().lower()
text = input("Enter the text: ")
key_matrix = []
for _ in range(n):
row = list(map(int, input().split()))
key_matrix.append(row)
key_matrix = np.array(key_matrix)
if choice == "encrypt":
result = encrypt_hill(text, key_matrix)
print("Encrypted Text:", result)
elif choice == "decrypt":
result = decrypt_hill(text, key_matrix)
print("Decrypted Text:", result)
else:
print("Invalid choice. Please choose either 'encrypt' or 'decrypt'.")
OUTPUT:
18
INFORMATION SECURITY (3170720) 191390107018
PRACTICAL 7
AIM: Implement Diffie-Hellman Key Exchange Method.
CODE:
import random
def diffie_hellman_key_exchange():
prime = 23
base = 5
print(f"Public Prime (p): {prime}")
print(f"Public Base (g): {base}")
private_a = random.randint(1, prime - 1)
print(f"User A's private key: {private_a}")
private_b = random.randint(1, prime - 1)
print(f"User B's private key: {private_b}")
public_a = (base ** private_a) % prime
public_b = (base ** private_b) % prime
print(f"User A's public value: {public_a}")
print(f"User B's public value: {public_b}")
shared_secret_a = (public_b ** private_a) % prime
shared_secret_b = (public_a ** private_b) % prime
assert shared_secret_a == shared_secret_b, "Shared secrets do not
match!"
19
INFORMATION SECURITY (3170720) 191390107018
return shared_secret_a
if __name__ == "__main__":
shared_secret = diffie_hellman_key_exchange()
print(f"\nThe shared secret key is: {shared_secret}")
OUTPUT:
20
INFORMATION SECURITY (3170720) 191390107018
PRACTICAL 8
AIM: Implement Extended Euclidean algorithm for finding the
multiplicative inverse in GF(2^n).
CODE:
def poly_add(p, q, n):
return [(p[i] + q[i]) % 2 if i < len(p) else q[i] for i in range(n)]
for i in range(len(p)):
for j in range(len(q)):
if p[i] == 1 and q[j] == 1:
# Ensure index is within bounds
if i + j < len(result):
result[i + j] ^= 1
return result[:n]
21
INFORMATION SECURITY (3170720) 191390107018
while any(r1):
q = r0[0]
r0, r1 = r1, poly_mod(poly_add(r0, poly_multiply([q] * len(r1), r1,
n), n), modulus, n)
s0, s1 = s1, poly_add(s0, poly_multiply([q] * len(s1), s1, n), n)
t0, t1 = t1, poly_add(t0, poly_multiply([q] * len(t0), t0, n), n)
return t0
22
INFORMATION SECURITY (3170720) 191390107018
if __name__ == "__main__":
a = [1, 0, 1]
modulus = [1, 0, 1, 1]
inverse = extended_euclidean_algorithm(a, [1] * len(modulus), 3,
modulus)
print(f"The multiplicative inverse of the polynomial a(x) in GF(2^3) is:
{inverse}")
OUTPUT:
23
INFORMATION SECURITY (3170720) 191390107018
PRACTICAL 9
AIM: Implement RSA encryption-decryption algorithm.
CODE:
import random
from math import gcd
def generate_rsa_keys(bits=8):
24
INFORMATION SECURITY (3170720) 191390107018
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
p=q=0
while not is_prime(p):
p = random.randint(2**(bits-1), 2**bits)
while not is_prime(q) or p == q:
q = random.randint(2**(bits-1), 2**bits)
n=p*q
phi_n = (p - 1) * (q - 1)
e = random.randint(2, phi_n)
while gcd(e, phi_n) != 1:
e = random.randint(2, phi_n)
d = mod_inverse(e, phi_n)
25
INFORMATION SECURITY (3170720) 191390107018
if __name__ == "__main__":
public_key, private_key = generate_rsa_keys(bits=8)
message = "Hello"
print(f"Original Message: {message}")
ciphertext = rsa_encrypt(public_key, message)
print(f"Ciphertext: {ciphertext}")
26
INFORMATION SECURITY (3170720) 191390107018
OUTPUT:
27
INFORMATION SECURITY (3170720) 191390107018
PRACTICAL 10
Theory:
• DES (Data Encryption Standard) is a symmetric-key block cipher
that encrypts data in 64-bit blocks using a 56-bit key.
• AES (Advanced Encryption Standard) is a symmetric-key block
cipher that encrypts data in 128-bit blocks using key sizes of 128,
192, or 256 bits.
Both encryption methods follow the principles of symmetric key
encryption, where the same key is used for both encryption and
decryption.
Materials Required:
• Cryptool software
• Computer with Windows or Linux OS
28
INFORMATION SECURITY (3170720) 191390107018
Procedure:
1. Install Cryptool:
Download and install Cryptool from the official website
(https://www.cryptool.org/en/ctool).
2. Open Cryptool:
Launch the Cryptool application.
3. Select the Algorithm:
o For DES:
▪ Navigate to "Cryptographic Algorithms" and select
"DES" from the list.
o For AES:
▪ Similarly, select "AES" from the available algorithms.
4. Input Plaintext:
o Type or paste the message (plaintext) to be encrypted in the
plaintext section of the Cryptool interface.
5. Set the Key:
o Choose or generate a key. For DES, the key size is 56 bits,
while for AES, you can choose between 128, 192, or 256 bits.
o Make sure to remember the key used for decryption.
6. Encryption:
o Click on the "Encrypt" button to perform the encryption
operation. The ciphertext will be generated and displayed in
the corresponding section.
7. Decryption:
29
INFORMATION SECURITY (3170720) 191390107018
30
INFORMATION SECURITY (3170720) 191390107018
31
INFORMATION SECURITY (3170720) 191390107018
32