Professional Documents
Culture Documents
IS EXP1-8
IS EXP1-8
IS EXP1-8
SAP ID : 60004210127
SUB: INFORMATION
SECURITY
C21 COMPS
CODE:
key=input("Enter key")
key=key.replace(" ", "")
key=key.upper()
def matrix(x,y,initial):
return [[initial for i in range(x)] for j in range(y)]
result=list()
for c in key: #storing key
if c not in result:
if c=='J':
result.append('I')
else:
result.append(c)
flag=0
for i in range(65,91): #storing other character
if chr(i) not in result:
if i==73 and chr(74) not in result:
result.append("I")
flag=1
elif flag==0 and i==73 or i==74:
pass
else:
result.append(chr(i))
k=0
my_matrix=matrix(5,5,0) #initialize matrix
for i in range(0,5): #making matrix
for j in range(0,5):
my_matrix[i][j]=result[k]
k+=1
while(1):
choice=int(input("\n 1.Encryption \n 2.Decryption: \n 3.EXIT"))
if choice==1:
encrypt()
elif choice==2:
decrypt()
elif choice==3:
exit()
else:
print("Choose correct choice")
OUTPUT:
CODE:
def create_vigenere_matrix():
matrix = [[chr((i + j) % 26 + ord('A')) for j in range(26)] for i
in range(26)]
return matrix
matrix_print=create_vigenere_matrix()
print("Matrix",matrix_print)
OUTPUT:
CODE:
plaintext = input("Enter plaintext: \n")
key = input("Enter key:\n")
if len(plaintext)==len (key):
result = [chr((((ord (a) - ord ("a")) ^ (ord(b) - ord("a"))) % 26)
+ ord ("a") )
for a, b in zip(plaintext.lower(), key.lower())]
print("".join(result))
OUTPUT:
CODE:
key = key.replace(i, str(rank), 1) rank += 1 key = [int(i) for i in key] return key
j = 0
for i in key_rank:
in range(rows):
a = ["*"] *(len(key_rank))
count = 0 for r in key_rank: a[count] = cip_mat[r][i] count +=1
OUTPUT:
EXP5- RSA
CODE:
import math
def gcd(a, h):
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)
while (e < phi):
# e must be co-prime to phi and
# smaller than phi.
if(gcd(e, phi) == 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)
print("Encrypted data = ", c)
# Decryption m = (c ^ d) % n
m = pow(c, d)
m = math.fmod(m, n)
print("Original Message Sent = ", m)
OUTPUT:
CODE:
from random import randint
P=
23 Q
=9
print('The Value of P is :%d'%(P))
print('The Value of Q is :%d'%(Q))
OUTPUT:
EXP7- MD5
CODE:
import hashlib
def md5_hash(text):
OUTPUT:
EXP8 – RSA DIGITAL SIGNATURE
CODE:
def euclid(m, n):
if n == 0: return m else:
r=m%n
return euclid(n, r) def exteuclid(a, b):
r1 = a
r2 = b
s1 = int(1) s2 = int(0) t1 = int(0) t2 = int(1)
while r2 > 0:
if t1 < 0: t1 = t1 % a
gcd = euclid(Pn, i)
else:
print("Multiplicative inverse for\ the given encryption key does not \ exist. Choose a different
encryption key ")
if M == M1:
print("As M = M1, Accept the\ message sent by Alice") else:
print("As M not equal to M1,\ Do not accept the message\ sent by Alice ")
OUTPUT: