-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa_cusz.py
111 lines (96 loc) · 2.9 KB
/
rsa_cusz.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
import os
from Crypto import Random
from Crypto.Hash import SHA
from Crypto.PublicKey import RSA
from datetime import datetime
from Crypto.Hash import MD5
from Crypto.Cipher import AES
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
g_hash_key = None
g_cipher_path = 'factory_cipher.bin'
g_orien_bin = '../test_bin/1101_e.bin'
def gen_custz_tool_key():
random_generator = Random.new().read
rsa = RSA.generate(2048,random_generator)
private_pem = rsa.exportKey()
with open('cust_private.pem','wb') as f:
f.write(private_pem)
public_pem = rsa.publickey().exportKey()
with open('cust_public.pem','wb') as f:
f.write(public_pem)
def gen_a_hash_key():#related with curr time and local machine name
timeNow = datetime.now()
# print(timeNow)
userName = os.path.expanduser('~').split('\\')[-1]
# print(userName)
h = MD5.new()
hash_obj = str(timeNow)+userName
h.update(hash_obj.encode('utf-8'))
key = h.hexdigest()
#print(key)
return key
def hash_key_generate():
global g_hash_key
g_hash_key = gen_a_hash_key()
with open('custz_aes.bin','wb') as f:
f.write(g_hash_key)
f.close()
def hash_key_load():
global g_hash_key
g_hash_key = '673d7665c4a7b79b1e361dd2f5fb70ee'
def generate_encrypt_bin_from_orien():
global g_hash_key,g_cipher_path
BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
bin_file = open(g_orien_bin,'rb')
orien_text = bin_file.read()
bin_file.close()
cipher = AES.new(g_hash_key,AES.MODE_ECB)
cipherText = cipher.encrypt(pad(orien_text))
#print(cipherText)
cipher_file = open(g_cipher_path,'wb')
cipher_file.write(cipherText)
cipher_file.close()
def load_encrypt_bin_then_decrypt():
global g_hash_key,g_cipher_path
unpad = lambda s: s[0:-ord(s[-1])]
read_file = open(g_cipher_path,'rb')
read_data = read_file.read()
read_file.close()
decipher = AES.new(g_hash_key,AES.MODE_ECB)
decipherText = unpad(decipher.decrypt(read_data)) #.decode('utf-8')
# print(decipherText)
return decipherText
def get_custz_bin_data():
bin_data = None
with open(g_orien_bin,'rb') as f:
bin_data = f.read()
return bin_data
def get_orien_bin_sign():
sign_data = None
orien_data = get_custz_bin_data()
with open('cust_private.pem') as f:
key = f.read()
rsakey = RSA.importKey(key)
signer = Signature_pkcs1_v1_5.new(rsakey)
digest = SHA.new()
digest.update(orien_data)
sign_data = signer.sign(digest)
return sign_data
if __name__ == '__main__':
# gen_custz_tool_key()
# hash_key_generate()
hash_key_load()
generate_encrypt_bin_from_orien()
sign_data = get_orien_bin_sign()
orien_data = load_encrypt_bin_then_decrypt()
with open('cust_public.pem') as f:
key = f.read()
rsakey = RSA.importKey(key)
verifier = Signature_pkcs1_v1_5.new(rsakey)
digest = SHA.new()
digest.update(orien_data)
is_verify = verifier.verify(digest,sign_data)
print(is_verify)