Repository navigation
Expand file tree
/
Copy pathICS-07
More file actions
96 lines (73 loc) · 1.45 KB
/
Copy pathICS-07
File metadata and controls
96 lines (73 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Function to find gcd
# of two numbers
def euclid(m, n):
if n == 0:
return m
else:
r = m % n
return euclid(n, r)
# Program to find
# Multiplicative inverse
def exteuclid(a, b):
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)
p = int(input("Enter Values Of P :"))
q = int(input("Enter Values of Q :"))
n = p * q
Pn = (p-1)*(q-1)
# Generate encryption key
# in range 1<e<Pn
key = []
for i in range(2, Pn):
gcd = euclid(Pn, i)
if gcd == 1:
key.append(i)
# Select an encryption key
# from the above list
e = int(313)
# Obtain inverse of
# encryption key in Z_Pn
r, d = exteuclid(Pn, e)
if r == 1:
d = int(d)
print("decryption key is: ", d)
else:
print("Multiplicative inverse for\
the given encryption key does not \
exist. Choose a different encryption key ")
# Enter the message to be sent
M = input("Enter Message To Be Sent :")
# Signature is created by Alice
S = (M**d) % n
# Alice sends M and S both to Bob
# Bob generates message M1 using the
# signature S, Alice's public key e
# and product n.
M1 = (S**e) % n
# If M = M1 only then Bob accepts
# the message sent by Alice.
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 ")