Professional Documents
Culture Documents
INSSS
INSSS
Practical 2
Code :-
key_square = ""
for char in key:
if char not in key_square:
key_square += char
digraphs = []
for i in range(0, len(plain_text), 2):
if i == len(plain_text) - 1:
digraphs.append(plain_text[i] + "Z")
elif plain_text[i] == plain_text[i+1]:
digraphs.append(plain_text[i] + "X")
plain_text = plain_text[:i+1] + "X" + plain_text[i+1:]
else:
digraphs.append(plain_text[i:i+2])
cipher_text = ""
for digraph in digraphs:
row1, col1 = divmod(key_square.index(digraph[0]), 5)
if row1 == row2:
cipher_text += key_square[row1*5 + (col1+1)%5]
cipher_text += key_square[row2*5 + (col2+1)%5]
elif col1 == col2:
cipher_text += key_square[((row1+1)%5)*5 + col1]
cipher_text += key_square[((row2+1)%5)*5 + col2]
else:
cipher_text += key_square[row1*5 + col2]
cipher_text += key_square[row2*5 + col1]
return cipher_text
Output :-
Practical 3.1
Code :-
Output :-
Practical 3.2
Code :-
import numpy as np
key = key.upper()
key = [ord(char) - 65 for char in key]
key = np.array(key).reshape(int(np.sqrt(len(key))), -1)
if plain_text.shape[0] % key.shape[0] != 0:
padding = key.shape[0] - plain_text.shape[0] % key.shape[0]
plain_text = np.vstack([plain_text, np.zeros((padding, 1))])
cipher_text = ""
for i in range(0, plain_text.shape[0], key.shape[0]):
block = plain_text[i:i+key.shape[0]]
block = np.dot(key, block) % 26
block = "".join([chr(char + 65) for char in block.flatten()])
cipher_text += block
return cipher_text
Output :-
Practical 4
Code :-
Output :-