INS New
INS New
# Substitution Cipher
import string
all_letters= string.ascii_letters
"""
"""
dict1 = {}
key = 4
for i in range(len(all_letters)):
dict1[all_letters[i]] = all_letters[(i+key)%len(all_letters)]
if char in all_letters:
temp = dict1[char]
cipher_txt.append(temp)
else:
temp =char
cipher_txt.append(temp)
cipher_txt= "".join(cipher_txt)
"""
"""
dict2 = {}
for i in range(len(all_letters)):
dict2[all_letters[i]] = all_letters[(i-key)%(len(all_letters))]
# loop to recover plain text
decrypt_txt = []
if char in all_letters:
temp = dict2[char]
decrypt_txt.append(temp)
else:
temp = char
decrypt_txt.append(temp)
decrypt_txt = "".join(decrypt_txt)
Ptactical 1B
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:
null_count = msg.count('_')
if null_count > 0:
# Driver Code
cipher = encryptMessage(msg)
format(cipher))
# Practical no 2
import math
temp = 0
while(1):
temp = a % h
if (temp == 0):
return h
a=h
h = temp
p=3
q=7
n = p*q
e=2
phi = (p-1)*(q-1)
break
else:
e = e+1
# d*e = 1 + k * totient
k=2
d = (1 + (k*phi))/e
# Message to be encrypted
msg = 12.0
print("Message data = ", msg)
# Encryption c = (msg ^ e) % n
c = pow(msg, e)
c = math.fmod(c, n)
# Decryption m = (c ^ d) % n
m = pow(c, d)
m = math.fmod(m, n)
# practical no 3
import hmac
import hashlib
digest_maker.update(b'another msg')
# print the Hexdigest of the bytes passed to update
print (digest_maker.digest())
digest_clone = digest_maker.copy()
# of two numbers
if n == 0:
return m
else:
r=m%n
return euclid(n, r)
# Practical No 4
# Program to find
r1 = a
r2 = b
s1 = int(1)
s2 = int(0)
t1 = int(0)
t2 = int(1)
while r2 > 0:
q = r1//r2
r = r1-q * r2
r1 = r2
r2 = r
s = s1-q * s2
s1 = s2
s2 = s
t = t1-q * t2
t1 = t2
t2 = t
if t1 < 0:
t1 = t1 % a
return (r1, t1)
# numbers p and q
p = 823
q = 953
n=p*q
Pn = (p-1)*(q-1)
# in range 1<e<Pn
key = []
gcd = euclid(Pn, i)
if gcd == 1:
key.append(i)
e = int(313)
# Obtain inverse of
# encryption key in Z_Pn
r, d = exteuclid(Pn, e)
if r == 1:
d = int(d)
else:
M = 19070
S = (M**d) % n
# and product n.
M1 = (S**e) % n
else: