Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import base58
|
7 |
+
|
8 |
+
import stegan2
|
9 |
+
import qr
|
10 |
+
|
11 |
+
|
12 |
+
def calculate_hash(data, hash_function: str = "sha256") -> str:
|
13 |
+
if type(data) == str:
|
14 |
+
data = bytearray(data, "utf-8")
|
15 |
+
if hash_function == "sha256":
|
16 |
+
h = SHA256.new()
|
17 |
+
h.update(data)
|
18 |
+
return h.hexdigest()
|
19 |
+
if hash_function == "ripemd160":
|
20 |
+
h = RIPEMD160.new()
|
21 |
+
h.update(data)
|
22 |
+
return h.hexdigest()
|
23 |
+
|
24 |
+
|
25 |
+
def generate_keys1():
|
26 |
+
key = RSA.generate(2048)
|
27 |
+
private_key = key.export_key('PEM')
|
28 |
+
file_out = open("private.pem", "wb")
|
29 |
+
file_out.write(private_key)
|
30 |
+
file_out.close()
|
31 |
+
|
32 |
+
public_key = key.publickey().export_key('PEM')
|
33 |
+
file_out = open("receiver.pem", "wb")
|
34 |
+
file_out.write(public_key)
|
35 |
+
file_out.close()
|
36 |
+
|
37 |
+
hash_1 = calculate_hash(public_key, hash_function="sha256")
|
38 |
+
hash_2 = calculate_hash(hash_1, hash_function="ripemd160")
|
39 |
+
address = base58.b58encode(hash_2)
|
40 |
+
address_im=qr.make_qr(txt=address)
|
41 |
+
|
42 |
+
return public_key,private_key,address_im,address
|
43 |
+
|
44 |
+
def encrypt_text1(data,in2,address):
|
45 |
+
data = data.encode("utf-8")
|
46 |
+
#data = "I met aliens in UFO. Here is the map.".encode("utf-8")
|
47 |
+
file_out = open("encrypted_data.bin", "wb")
|
48 |
+
|
49 |
+
recipient_key = RSA.import_key(open("receiver.pem").read())
|
50 |
+
session_key = get_random_bytes(16)
|
51 |
+
|
52 |
+
# Encrypt the session key with the public RSA key
|
53 |
+
cipher_rsa = PKCS1_OAEP.new(recipient_key)
|
54 |
+
enc_session_key = cipher_rsa.encrypt(session_key)
|
55 |
+
|
56 |
+
# Encrypt the data with the AES session key
|
57 |
+
cipher_aes = AES.new(session_key, AES.MODE_EAX)
|
58 |
+
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
|
59 |
+
[ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
|
60 |
+
file_out.close()
|
61 |
+
doc_name = "encrypted_data.bin"
|
62 |
+
with open(doc_name, "rb") as file:
|
63 |
+
file_data =(file.read())
|
64 |
+
print (f'file_data::{file_data}')
|
65 |
+
qr_link="test"
|
66 |
+
enc_qr = stegan2.conv_im(qr_link=qr_link,data=file_data)
|
67 |
+
|
68 |
+
file.close()
|
69 |
+
return str(file_data),enc_qr
|
70 |
+
|
71 |
+
def decrypt_text1(im,in2):
|
72 |
+
enc_in = stegan2.decode(im)
|
73 |
+
private_key = RSA.import_key(open("private.pem").read())
|
74 |
+
enc_session_key = enc_in[:private_key.size_in_bytes()]
|
75 |
+
end1 = private_key.size_in_bytes()+16
|
76 |
+
nonce = enc_in[private_key.size_in_bytes():end1]
|
77 |
+
start1=end1+1
|
78 |
+
end2 = private_key.size_in_bytes()+32
|
79 |
+
start2=end2+1
|
80 |
+
tag = enc_in[end1:end2]
|
81 |
+
ciphertext = enc_in[end2:]
|
82 |
+
print (f'enc_session_key::{enc_session_key}')
|
83 |
+
print (f'nonce::{nonce}')
|
84 |
+
print (f'tag::{tag}')
|
85 |
+
print (f'ciphertext::{ciphertext}')
|
86 |
+
|
87 |
+
# Decrypt the session key with the private RSA key
|
88 |
+
cipher_rsa = PKCS1_OAEP.new(private_key)
|
89 |
+
session_key = cipher_rsa.decrypt(enc_session_key)
|
90 |
+
|
91 |
+
# Decrypt the data with the AES session key
|
92 |
+
cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
|
93 |
+
data = cipher_aes.decrypt_and_verify(ciphertext, tag)
|
94 |
+
return(data.decode("utf-8"))
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
with gr.Blocks() as app:
|
99 |
+
gen_wal_btn=gr.Button()
|
100 |
+
seed = gr.Textbox(label='Seed Phrase')
|
101 |
+
with gr.Row():
|
102 |
+
gr.Column()
|
103 |
+
with gr.Column():
|
104 |
+
with gr.Box():
|
105 |
+
with gr.Row():
|
106 |
+
with gr.Column():
|
107 |
+
out1 = gr.Textbox(label='Private Key')
|
108 |
+
out2 = gr.Textbox(label='Public Key')
|
109 |
+
img3=gr.Pil()
|
110 |
+
out3 = gr.Textbox(label='Address')
|
111 |
+
rsa_to_enc = gr.Textbox(label="txt to encrypt")
|
112 |
+
rsa_enc_btn = gr.Button("RSA Encrypt")
|
113 |
+
rsa_enc_mes = gr.Textbox(label="encoded")
|
114 |
+
qr_enc_mes = gr.Image(type="filepath")
|
115 |
+
rsa_dec_btn = gr.Button("RSA Decrypt")
|
116 |
+
rsa_dec_mes = gr.Textbox(label="decoded")
|
117 |
+
gr.Column()
|
118 |
+
rsa_enc_btn.click(encrypt_text1,[rsa_to_enc,out2,out3],[rsa_enc_mes,qr_enc_mes])
|
119 |
+
rsa_dec_btn.click(decrypt_text1,[qr_enc_mes,out1],rsa_dec_mes)
|
120 |
+
app.launch()
|
121 |
+
|