-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdss.py
More file actions
47 lines (38 loc) · 1.09 KB
/
dss.py
File metadata and controls
47 lines (38 loc) · 1.09 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
import random
def mod_inverse(a, m):
for i in range(1, m):
if (a * i) % m == 1:
return i
return None
def dss():
# Get inputs from user
p = int(input("Enter prime number p: "))
q = int(input("Enter prime number q: "))
message = input("Enter original message: ")
x = int(input("Enter private key x: "))
k = int(input("Enter random integer k: "))
h = int(input("Enter hash value h: "))
# Key Generation
g = 2 # Simple generator, can be changed
y = pow(g, x, p)
print("\nSender side:")
print(f"Public key y: {y}")
# Signature Generation
r = (pow(g, k, p) % q)
k_inv = mod_inverse(k, q)
s = (k_inv * (h + x * r)) % q
print(f"r: {r}")
print(f"s: {s}")
# Receiver side
print("\nReceiver side:")
w = mod_inverse(s, q)
u1 = (h * w) % q
u2 = (r * w) % q
v = ((pow(g, u1, p) * pow(y, u2, p)) % p) % q
print(f"z: {h}") # z is the same as h in this case
print(f"u1: {u1}")
print(f"u2: {u2}")
print(f"v: {v}")
print(f"Signature verified: {v == r}")
# Run the function
dss()