Spaces:
Runtime error
Runtime error
Create crypt.py
Browse files
crypt.py
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from Crypto.PublicKey import RSA
|
3 |
+
from Crypto.Random import get_random_bytes
|
4 |
+
from Crypto.Cipher import AES, PKCS1_OAEP
|
5 |
+
from Crypto.Hash import RIPEMD160, SHA256
|
6 |
+
from Crypto.Signature import pkcs1_15
|
7 |
+
import binascii
|
8 |
+
|
9 |
+
import base58
|
10 |
+
import stegan
|
11 |
+
import stegan2
|
12 |
+
import qr
|
13 |
+
import overlay
|
14 |
+
|
15 |
+
|
16 |
+
def calculate_hash(data, hash_function: str = "sha256") -> str:
|
17 |
+
if type(data) == str:
|
18 |
+
data = bytearray(data, "utf-8")
|
19 |
+
if hash_function == "sha256":
|
20 |
+
h = SHA256.new()
|
21 |
+
h.update(data)
|
22 |
+
return h.hexdigest()
|
23 |
+
if hash_function == "ripemd160":
|
24 |
+
h = RIPEMD160.new()
|
25 |
+
h.update(data)
|
26 |
+
return h.hexdigest()
|
27 |
+
|
28 |
+
|
29 |
+
def generate_keys():
|
30 |
+
secret_code="SECRET PASSWORD"
|
31 |
+
key = RSA.generate(2048)
|
32 |
+
#private_key = key.export_key('PEM')
|
33 |
+
private_key = key.export_key(passphrase=secret_code, pkcs=8,
|
34 |
+
protection="scryptAndAES128-CBC")
|
35 |
+
print(f'private_key:: {private_key}')
|
36 |
+
|
37 |
+
file_out_priv = open("private.pem", "wb")
|
38 |
+
file_out_priv.write(private_key)
|
39 |
+
file_out_priv.close()
|
40 |
+
|
41 |
+
public_key = key.publickey().export_key('PEM')
|
42 |
+
file_out_pub = open("receiver.pem", "wb")
|
43 |
+
file_out_pub.write(public_key)
|
44 |
+
file_out_pub.close()
|
45 |
+
|
46 |
+
|
47 |
+
hash_1 = calculate_hash(public_key, hash_function="sha256")
|
48 |
+
hash_2 = calculate_hash(hash_1, hash_function="ripemd160")
|
49 |
+
address = base58.b58encode(hash_2)
|
50 |
+
address_im=qr.make_qr(txt=address)
|
51 |
+
add_label = str(address)
|
52 |
+
add_label = add_label.strip("b").strip("'")
|
53 |
+
address_im = overlay.textover(address_im, "Wallet",add_label)
|
54 |
+
address_im.save("address_im.png")
|
55 |
+
#qr_link="test"
|
56 |
+
address_im = "address_im.png"
|
57 |
+
private_key_im = overlay.textover("private_key.png", "Key",add_label)
|
58 |
+
private_key_im.save("private_key_im.png")
|
59 |
+
priv_key = stegan.conv_im("private_key_im.png",data=private_key)
|
60 |
+
|
61 |
+
pub_key = stegan.conv_im("address_im.png",data=public_key)
|
62 |
+
|
63 |
+
return public_key,private_key,address_im,address,priv_key,pub_key
|
64 |
+
|
65 |
+
def sign(data,in2):
|
66 |
+
priv_key = stegan2.decode(in2)
|
67 |
+
print(f'priv_key:: {priv_key}')
|
68 |
+
private_key = RSA.import_key(priv_key)
|
69 |
+
|
70 |
+
hash_object = SHA256.new(data)
|
71 |
+
signature = pkcs1_15.new(private_key).sign(hash_object)
|
72 |
+
signature = binascii.hexlify(signature).decode("utf-8")
|
73 |
+
data_json = {
|
74 |
+
"data": data,
|
75 |
+
"signature": signature
|
76 |
+
}
|
77 |
+
return data_json
|
78 |
+
|
79 |
+
def validate_signature(public_key: bytes, signature: bytes, transaction_data: bytes):
|
80 |
+
public_key_object = RSA.import_key(public_key)
|
81 |
+
transaction_hash = SHA256.new(transaction_data)
|
82 |
+
pkcs1_15.new(public_key_object).verify(transaction_hash, signature)
|
83 |
+
|
84 |
+
def encrypt_text(data,pub_im,priv_im,address):
|
85 |
+
pub_key = stegan2.decode(pub_im)
|
86 |
+
#priv_key = stegan2.decode(in2)
|
87 |
+
#print(f'priv_key:: {priv_key}')
|
88 |
+
#private_key = RSA.import_key(priv_key)
|
89 |
+
data = data.encode("utf-8")
|
90 |
+
#data = sign(data,priv_im)
|
91 |
+
|
92 |
+
|
93 |
+
recipient_key = RSA.import_key(pub_key)
|
94 |
+
|
95 |
+
session_key = get_random_bytes(16)
|
96 |
+
|
97 |
+
# Encrypt the session key with the public RSA key
|
98 |
+
cipher_rsa = PKCS1_OAEP.new(recipient_key)
|
99 |
+
enc_session_key = cipher_rsa.encrypt(session_key)
|
100 |
+
|
101 |
+
# Encrypt the data with the AES session key
|
102 |
+
cipher_aes = AES.new(session_key, AES.MODE_EAX)
|
103 |
+
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
|
104 |
+
|
105 |
+
file_out = open("encrypted_data.bin", "wb")
|
106 |
+
|
107 |
+
[ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
|
108 |
+
file_out.close()
|
109 |
+
doc_name = "encrypted_data.bin"
|
110 |
+
with open(doc_name, "rb") as file:
|
111 |
+
file_data =(file.read())
|
112 |
+
print (f'file_data::{file_data}')
|
113 |
+
qr_link="test"
|
114 |
+
#trans_im1.save("trans_im.png")
|
115 |
+
hash_1 = calculate_hash(pub_key, hash_function="sha256")
|
116 |
+
hash_2 = calculate_hash(hash_1, hash_function="ripemd160")
|
117 |
+
address = base58.b58encode(hash_2)
|
118 |
+
add_label = str(address)
|
119 |
+
add_label = add_label.strip("b").strip("'")
|
120 |
+
|
121 |
+
trans_im1=qr.make_qr(txt=address, color_b="#ECFD08")
|
122 |
+
|
123 |
+
#address_im = "address_im.png"
|
124 |
+
private_key_im = overlay.textover(trans_im1, "Transaction",add_label)
|
125 |
+
private_key_im.save("private_key_im.png")
|
126 |
+
|
127 |
+
|
128 |
+
|
129 |
+
|
130 |
+
#trans_im1.save("trans_im.png")
|
131 |
+
enc_qr = stegan.conv_im("private_key_im.png",data=file_data)
|
132 |
+
|
133 |
+
file.close()
|
134 |
+
return str(file_data),enc_qr
|
135 |
+
|
136 |
+
def decrypt_text(im,in2):
|
137 |
+
enc_in = stegan2.decode(im)
|
138 |
+
secret_code="SECRET PASSWORD"
|
139 |
+
#private_key = RSA.import_key(open("private.pem").read())
|
140 |
+
priv_key = stegan2.decode(in2)
|
141 |
+
print(f'priv_key:: {priv_key}')
|
142 |
+
private_key = RSA.import_key(priv_key,passphrase=secret_code)
|
143 |
+
print(f'private_key:: {private_key}')
|
144 |
+
enc_session_key = enc_in[:private_key.size_in_bytes()]
|
145 |
+
end1 = private_key.size_in_bytes()+16
|
146 |
+
nonce = enc_in[private_key.size_in_bytes():end1]
|
147 |
+
start1=end1+1
|
148 |
+
end2 = private_key.size_in_bytes()+32
|
149 |
+
start2=end2+1
|
150 |
+
tag = enc_in[end1:end2]
|
151 |
+
ciphertext = enc_in[end2:]
|
152 |
+
print (f'enc_session_key::{enc_session_key}')
|
153 |
+
print (f'nonce::{nonce}')
|
154 |
+
print (f'tag::{tag}')
|
155 |
+
print (f'ciphertext::{ciphertext}')
|
156 |
+
|
157 |
+
# Decrypt the session key with the private RSA key
|
158 |
+
cipher_rsa = PKCS1_OAEP.new(private_key)
|
159 |
+
session_key = cipher_rsa.decrypt(enc_session_key)
|
160 |
+
|
161 |
+
# Decrypt the data with the AES session key
|
162 |
+
cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
|
163 |
+
data = cipher_aes.decrypt_and_verify(ciphertext, tag)
|
164 |
+
|
165 |
+
#public_key = private_key.publickey().export_key('PEM')
|
166 |
+
#data_val = data[0]
|
167 |
+
#print (data_val)
|
168 |
+
|
169 |
+
#signature = data[1]
|
170 |
+
#print (signature)
|
171 |
+
#transaction_hash = SHA256.new(data)
|
172 |
+
|
173 |
+
#transaction
|
174 |
+
|
175 |
+
#print (pkcs1_15.new(public_key).verify(transaction_hash, signature))
|
176 |
+
return(data.decode("utf-8"))
|
177 |
+
|
178 |
+
def test_fn(im1,im2):
|
179 |
+
return im1,im2,im1
|
180 |
+
def test_fn2(im1):
|
181 |
+
return im1
|