-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestdss.py
86 lines (78 loc) · 2.48 KB
/
testdss.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from Crypto.PublicKey import RSA
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA1
import base64
class DSS():
# def __init__(ds1, message = 'test'):
# ds1.message = message
def set_message(ds1, message):
ds1.message = message
# print(ds1.message)
# print("")
def gen_keys(ds1, bits, e=None):
'''Generates the public and private keys for the object'''
if(e==None):
key = RSA.generate(bits)
else:
key = RSA.generate(bits, e=e)
ds1.private_key = key.exportKey('DER')
ds1.public_key = key.publickey().exportKey('DER')
return ds1.public_key.hex()
def gen_hash(ds1, h):
# h = SHA1.new()
h.update(ds1.message.encode('utf8'))
return h.hexdigest()
def gen_DS(ds1, h):
'''Generates the Digital Signature for the message by encrypting the hash
of the message with private key'''
h2 = SHA1.new()
h2.update(ds1.message.encode('utf8'))
h = h2.hexdigest()
if(h == h2.hexdigest()):
try:
key = RSA.import_key(ds1.private_key)
signature = pkcs1_15.new(key).sign(h2)
except:
return
ds1.signature = signature
sig = str(base64.b64encode(bytes.fromhex(signature.hex())))
sig64 = sig[1:]
return signature.hex(), sig64
else:
raise ValueError
def verify(ds1, ds, h):
'''Decryptes the signature using Public key and compares the value with
original hash'''
h2 = SHA1.new()
h2.update(ds1.message.encode('utf8'))
if(True):
key = RSA.import_key(ds1.public_key)
try:
pkcs1_15.new(key).verify(h, ds)
s = "The signature is valid."
except (ValueError, TypeError):
s = "The signature is not valid."
print(s)
print("")
return s
else:
raise ValueError
def main():
ds1 = DSS()
h = SHA1.new()
message=input("Enter the message to be encrypted: ")
ds1.set_message(message)
print("Message set successfully")
print("")
ds1.gen_hash(h)
print("Hash is: ", ds1.message)
print("")
ds1.gen_keys(1024)
print("Keys generated")
print("")
ds1.gen_DS(h)
print("Digital signature successfully generated")
print("")
ds1.verify(ds1.signature,h)
# ds1 = DSS()
main()