Ins Practical
Ins Practical
Caesar cipher is a type of Monoalphabetic cipher. It uses the similar substitution method to receive
the cipher text characters for each plain text character.
def encrypt_text(plaintext,n):
ans = ""
for i in range(len(plaintext)):
ch = plaintext[i]
if ch==" ":
ans+=" "
elif (ch.isupper()):
else:
return ans
plaintext = "DANCE"
n=1
OUTPUT
Shift pattern is : 1
# and Decryption
# length(text) = columns
# to distinguish filled
for j in range(key)]
dir_down = False
row, col = 0, 0
for i in range(len(text)):
rail[row][col] = text[i]
col += 1
# direction flag
if dir_down:
row += 1
else:
row -= 1
result = []
for i in range(key):
for j in range(len(text)):
if rail[i][j] != '\n':
result.append(rail[i][j])
return("" . join(result))
for j in range(key)]
dir_down = None
row, col = 0, 0
for i in range(len(cipher)):
if row == 0:
dir_down = True
if row == key - 1:
dir_down = False
rail[row][col] = '*'
col += 1
if dir_down:
row += 1
else:
row -= 1
index = 0
for i in range(key):
for j in range(len(cipher)):
rail[i][j] = cipher[index]
index += 1
result = []
row, col = 0, 0
for i in range(len(cipher)):
if row == 0:
dir_down = True
if row == key-1:
dir_down = False
if (rail[row][col] != '*'):
result.append(rail[row][col])
col += 1
# direction flag
if dir_down:
row += 1
else:
row -= 1
return("".join(result))
# Driver code
if __name__ == "__main__":
# same cipher-text
print(decryptRailFence("atctak", 2))
Output
atc tak
hlool elwrd
attack
helloworld
PRACTICAL 3
Columnar Cipher
# Python3 implementation of
# Columnar Transposition
import math
key = "HACK"
# Encryption
def encryptMessage(msg):
cipher = ""
k_indx = 0
msg_len = float(len(msg))
msg_lst = list(msg)
key_lst = sorted(list(key))
col = len(key)
msg_lst.extend('_' * fill_null)
for _ in range(col):
curr_idx = key.index(key_lst[k_indx])
cipher += ''.join([row[curr_idx]
k_indx += 1
return cipher
# Decryption
def decryptMessage(cipher):
msg = ""
k_indx = 0
msg_indx = 0
msg_len = float(len(cipher))
msg_lst = list(cipher)
col = len(key)
key_lst = sorted(list(key))
dec_cipher = []
for _ in range(row):
for _ in range(col):
curr_idx = key.index(key_lst[k_indx])
for j in range(row):
dec_cipher[j][curr_idx] = msg_lst[msg_indx]
msg_indx += 1
k_indx += 1
try:
except TypeError:
raise TypeError("This program cannot",
null_count = msg.count('_')
if null_count > 0:
return msg
# Driver Code
msg = "hihelloworld"
cipher = encryptMessage(msg)
format(cipher))
format(decryptMessage(cipher)))
Output
PRACTICAL - 4
PLAYFAIR CIPHER
def toLowerCase(text):
return text.lower()
def removeSpaces(text):
newText = ""
for i in text:
if i == " ":
continue
else:
newText = newText + i
return newText
# as a list element
def Diagraph(text):
Diagraph = []
group = 0
Diagraph.append(text[group:i])
group = i
Diagraph.append(text[group:])
return Diagraph
def FillerLetter(text):
k = len(text)
if k % 2 == 0:
if text[i] == text[i+1]:
new_word = FillerLetter(new_word)
break
else:
new_word = text
Else
if text[i] == text[i+1]:
new_word = FillerLetter(new_word)
break
else:
new_word = text
return new_word
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
key_letters = []
for i in word:
if i not in key_letters:
key_letters.append(i)
compElements = []
for i in key_letters:
if i not in compElements:
compElements.append(i)
for i in list1:
if i not in compElements:
compElements.append(i)
matrix = []
matrix.append(compElements[:5])
compElements = compElements[5:]
return matrix
for i in range(5):
for j in range(5):
if(mat[i][j] == element):
return i, j
if e1c == 4:
char1 = matr[e1r][0]
else:
char1 = matr[e1r][e1c+1]
char2 = ''
if e2c == 4:
char2 = matr[e2r][0]
else:
char2 = matr[e2r][e2c+1]
char1 = ''
if e1r == 4:
char1 = matr[0][e1c]
else:
char1 = matr[e1r+1][e1c]
char2 = ''
if e2r == 4:
char2 = matr[0][e2c]
else:
char2 = matr[e2r+1][e2c]
char1 = ''
char1 = matr[e1r][e2c]
char2 = ''
char2 = matr[e2r][e1c]
CipherText = []
c1 = 0
c2 = 0
if ele1_x == ele2_x:
else:
c1, c2 = encrypt_RectangleRule(
cipher = c1 + c2
CipherText.append(cipher)
return CipherText
text_Plain = 'instrument'
text_Plain = removeSpaces(toLowerCase(text_Plain))
PlainTextList = Diagraph(FillerLetter(text_Plain))
if len(PlainTextList[-1]) != 2:
PlainTextList[-1] = PlainTextList[-1]+'z'
key = "Monarchy"
key = toLowerCase(key)
CipherText = ""
for i in CipherList:
CipherText += i
print("CipherText:", CipherText)
OUTPUT
CipherText: gatlmzclrq
PRACTICAL 5
VERNAM CIPHER
# Python program Implementing One Time Pad Algorithm
# Method 1
# Initializing cipherText
cipherText = ""
cipher = []
for i in range(len(key)):
# subtract 26 from it
for i in range(len(key)):
cipher[i] = cipher[i] - 26
x = cipher[i] + ord('A')
cipherText += chr(x)
return cipherText
# Method 2
plainText = ""
# of corresponding no.'s of
plain = []
for i in range(len(key)):
for i in range(len(key)):
for i in range(len(key)):
x = plain[i] + ord('A')
plainText += chr(x)
# Returning plainText
return plainText
plainText = "Hello"
# Declaring key
key = "MONEY"
OUTPUT
Message - HELLO
PRACTICAL - 6
RSA ALGORITHM
import math
# step 1
p=3
q=7
# step 2
n = p*q
print("n =", n)
# step 3
phi = (p-1)*(q-1)
# step 4
e=2
while(e<phi):
if (math.gcd(e, phi) == 1):
break
else:
e += 1
print("e =", e)
# step 5
k=2
d = ((k*phi)+1)/e
print("d =", d)
print(f'Public key: {e, n}')
print(f'Private key: {d, n}')
# plain text
msg = 11
print(f'Original message:{msg}')
# encryption
C = pow(msg, e)
C = math.fmod(C, n)
print(f'Encrypted message: {C}')
# decryption
M = pow(C, d)
M = math.fmod(M, n)
Output
n = 21
e=5
d = 5.0
Public key: (5, 21)
Private key: (5.0, 21)
Original message:11
Encrypted message: 2.0
Decrypted message: 11.0
PRACTICAL - 7
HMAC MODULE
OUTPUT
Hexdigest: df2ae7cdb5c849001e33ee29eb1c51ba2cafbaa7
Hexdigest after update: 3923273eb3aa9328478eb5aabf2d96e185256b4b
Digest size: 20 bytes
Block size: 64 bytes
Canonical name: hmac-sha1
Digest: b"9#'>\xb3\xaa\x93(G\x8e\xb5\xaa\xbf-\x96\xe1\x85%kK"
Hexdigest of clone: 3923273eb3aa9328478eb5aabf2d96e185256b4b