-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSA.py
65 lines (50 loc) · 1.15 KB
/
DSA.py
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
import random
from gmpy2 import invert, powmod
MAX = 1000000007
def hash(message):
res = 1
for c in message:
res = res * ord(c) % MAX
return res
def sign(message, p, q, g, private_key):
s = 0
r = 0
while s == 0 or r == 0:
k = random.randrange(2, q)
r = int(((g**k) % p) % q)
s = int((invert(k, q)*(hash(message)+private_key*r)) % q)
print(k)
print(r)
print(s)
return (r, s)
def verify(signature, tampered_message, p, q, g, public_key) -> bool:
r, s = signature
w = invert(s, q)
print(w)
u1 = (hash(tampered_message)*w) % q
print(u1)
u2 = (r*w) % q
print(u2)
v = int(((g**u1)*(public_key**u2) % p) % q)
print(v)
return r == v
def main():
p = 23
q = 11
g = 1
while g == 1:
h = random.randint(2, p-2)
g = int(pow(h, (p-1)/q) % p)
SentMsg = input()
RecievedMsg = input()
x = random.randint(1, q-1)
y = int(pow(g, x)) % p
print(p)
print(q)
print(g)
print(x)
print(y)
signature = sign(SentMsg, p, q, g, x)
result = verify(signature, RecievedMsg, p, q, g, y)
print(result)
main()