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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from cryptography.fernet import Fernet
|
3 |
+
from cryptography.hazmat.primitives import hashes
|
4 |
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
5 |
+
import os
|
6 |
+
import base64
|
7 |
+
import hashlib
|
8 |
+
from pathlib import Path
|
9 |
+
import qr
|
10 |
+
import stegan
|
11 |
+
import json
|
12 |
+
|
13 |
+
def trans_block(sender,recipient,amount):
|
14 |
+
transaction = [{
|
15 |
+
'sender': sender,
|
16 |
+
'recipient': recipient,
|
17 |
+
'amount': amount
|
18 |
+
}]
|
19 |
+
json_object = json.dumps(transaction, indent=4)
|
20 |
+
print (json_object)
|
21 |
+
return json_object
|
22 |
+
|
23 |
+
def create_key(passw):
|
24 |
+
key = passw.encode()
|
25 |
+
h = hashlib.new('sha256')
|
26 |
+
h.update(key)
|
27 |
+
key = h.hexdigest()
|
28 |
+
key = key.encode()
|
29 |
+
#salt = os.urandom(16)
|
30 |
+
salt = key
|
31 |
+
kdf = PBKDF2HMAC(
|
32 |
+
algorithm=hashes.SHA256(),
|
33 |
+
length=32,
|
34 |
+
salt=salt,
|
35 |
+
iterations=480000,
|
36 |
+
)
|
37 |
+
key = base64.urlsafe_b64encode(kdf.derive(key))
|
38 |
+
return key
|
39 |
+
|
40 |
+
|
41 |
+
def encrypt(passw,mes=None,img=None,doc=None,trans_s=None,trans_r=None,trans_a=None):
|
42 |
+
#key = Fernet.generate_key()
|
43 |
+
key = create_key(passw)
|
44 |
+
fernet = Fernet(key)
|
45 |
+
enc_mes=""
|
46 |
+
enc_file=None
|
47 |
+
if mes != None and mes != "":
|
48 |
+
bytes_m = mes.encode()
|
49 |
+
enc_mes = fernet.encrypt(bytes_m)
|
50 |
+
enc_mes = f'{enc_mes}+aaa+'
|
51 |
+
if img != None:
|
52 |
+
with open(f'{img}', "rb") as image_file:
|
53 |
+
bytes_i = base64.b64encode(image_file.read())
|
54 |
+
if mes != None and mes != "":
|
55 |
+
im_bytes = fernet.encrypt(bytes_i)
|
56 |
+
enc_mes = f'{enc_mes}{im_bytes}+bbb+'
|
57 |
+
else:
|
58 |
+
im_bytes = fernet.encrypt(bytes_i)
|
59 |
+
enc_mes = f'{im_bytes}+bbb+'
|
60 |
+
if trans_s != None and trans_r != None and trans_a != None and trans_s != "" and trans_r != "" and trans_a != "":
|
61 |
+
trans = trans_block(trans_s,trans_r,trans_a)
|
62 |
+
bytes_j = trans.encode()
|
63 |
+
if mes != None or mes != "" or img != None or img !="":
|
64 |
+
json_bytes = fernet.encrypt(bytes_j)
|
65 |
+
enc_mes = f'{enc_mes}{json_bytes}+ccc+'
|
66 |
+
else:
|
67 |
+
json_bytes = fernet.encrypt(bytes_j)
|
68 |
+
enc_mes = f'{json_bytes}+ccc+'
|
69 |
+
|
70 |
+
#enc_qr = qr.make_qr(txt=(enc_mes.strip('"')))
|
71 |
+
m = hashlib.sha256()
|
72 |
+
bytes_enc_mes = enc_mes.encode()
|
73 |
+
m.update(bytes_enc_mes)
|
74 |
+
out = m.hexdigest()
|
75 |
+
out = out.encode()
|
76 |
+
print(out)
|
77 |
+
qr_link = out
|
78 |
+
enc_qr = stegan.conv_im(qr_link=qr_link,data=enc_mes)
|
79 |
+
|
80 |
+
if doc != None:
|
81 |
+
#print(dir(doc))
|
82 |
+
#print(doc.name)
|
83 |
+
doc_name = doc.name
|
84 |
+
doc_name = doc_name.split("/",4)[4]
|
85 |
+
with open(doc.name, "rb") as file:
|
86 |
+
# read all file data
|
87 |
+
file_data = file.read()
|
88 |
+
enc_doc = fernet.encrypt(file_data)
|
89 |
+
og_name = doc.name
|
90 |
+
og_end = og_name.split(".",1)[1]
|
91 |
+
og_front=og_name.split(".",1)[0]
|
92 |
+
|
93 |
+
|
94 |
+
enc_file=f'{doc.name}.ocrpt'
|
95 |
+
with open(enc_file, "wb") as file:
|
96 |
+
file.write(enc_doc)
|
97 |
+
#current_url.url_return("www.google.com")
|
98 |
+
return enc_mes,enc_file,enc_qr,enc_qr
|
99 |
+
|
100 |
+
def decrypt(passw,enc_in=None):
|
101 |
+
key = create_key(passw)
|
102 |
+
fernet = Fernet(key)
|
103 |
+
dec_im = None
|
104 |
+
mes_dec= None
|
105 |
+
enc_in=enc_in.strip('"')
|
106 |
+
print (f'enc_in :::: {enc_in}')
|
107 |
+
|
108 |
+
if "+aaa+" in enc_in:
|
109 |
+
mes1=enc_in.split("+aaa+",1)[0]
|
110 |
+
mes1=mes1.strip("b'").strip("'")
|
111 |
+
mes_bytes = bytes(mes1,'utf-8')
|
112 |
+
mes_dec = fernet.decrypt(mes_bytes).decode()
|
113 |
+
if "+bbb+" in enc_in:
|
114 |
+
mes12=enc_in.split("+aaa+",1)[1]
|
115 |
+
mes2=mes12.split("+bbb+",1)[0]
|
116 |
+
mes2=mes2.strip("b'").strip("'")
|
117 |
+
im_bytes = bytes(mes2,'utf-8')
|
118 |
+
print(f'im_bytes::{im_bytes}')
|
119 |
+
|
120 |
+
mes2 = fernet.decrypt(mes2).decode()
|
121 |
+
#base = bytes(decMessage, 'utf-8')
|
122 |
+
with open(f"finished_im.png", "wb") as fh:
|
123 |
+
#fh.write(base64.decodebytes(im_bytes))
|
124 |
+
fh.write(base64.decodebytes(bytes(mes2, 'utf-8')))
|
125 |
+
fh.close
|
126 |
+
dec_im = "finished_im.png"
|
127 |
+
if "+ccc+" in enc_in:
|
128 |
+
mes13=enc_in.split("+bbb+",1)[1]
|
129 |
+
mes3=mes13.split("+ccc+",1)[0]
|
130 |
+
mes3=mes3.strip("b'").strip("'")
|
131 |
+
json_bytes = bytes(mes3,'utf-8')
|
132 |
+
print(f'im_bytes::{json_bytes}')
|
133 |
+
dec_json = fernet.decrypt(json_bytes).decode()
|
134 |
+
if not "+bbb+" in enc_in:
|
135 |
+
if "+ccc+" in enc_in:
|
136 |
+
mes14=enc_in.split("+aaa+",1)[1]
|
137 |
+
|
138 |
+
mes4=mes14.split("+ccc+",1)[0]
|
139 |
+
mes4=mes4.strip("b'").strip("'")
|
140 |
+
json_bytes = bytes(mes4,'utf-8')
|
141 |
+
print(f'im_bytes::{json_bytes}')
|
142 |
+
dec_json = fernet.decrypt(json_bytes).decode()
|
143 |
+
|
144 |
+
if not "+aaa+" in enc_in:
|
145 |
+
if "+bbb+" in enc_in:
|
146 |
+
mes2 = enc_in.split("+bbb+",1)[0]
|
147 |
+
mes2=mes2.strip("b'").strip("'")
|
148 |
+
im_bytes = bytes(mes2,'utf-8')
|
149 |
+
print(f'im_bytes2::{im_bytes}')
|
150 |
+
mes2 = fernet.decrypt(mes2).decode()
|
151 |
+
|
152 |
+
#base = bytes(decMessage, 'utf-8')
|
153 |
+
with open(f"finished_im.png", "wb") as fh:
|
154 |
+
#fh.write(base64.decodebytes(im_bytes))
|
155 |
+
fh.write(base64.decodebytes(bytes(mes2, 'utf-8')))
|
156 |
+
fh.close
|
157 |
+
dec_im = "finished_im.png"
|
158 |
+
if "+ccc+" in enc_in:
|
159 |
+
mes13=enc_in.split("+bbb+",1)[1]
|
160 |
+
mes3=mes13.split("+ccc+",1)[0]
|
161 |
+
mes3=mes3.strip("b'").strip("'")
|
162 |
+
json_bytes = bytes(mes3,'utf-8')
|
163 |
+
print(f'im_bytes::{json_bytes}')
|
164 |
+
dec_json = fernet.decrypt(json_bytes).decode()
|
165 |
+
if not "+bbb+" in enc_in:
|
166 |
+
if "+ccc+" in enc_in:
|
167 |
+
mes4=enc_in.split("+ccc+",1)[0]
|
168 |
+
mes14=mes4.strip("b'").strip("'")
|
169 |
+
json_bytes = bytes(mes14,'utf-8')
|
170 |
+
print(f'im_bytes::{json_bytes}')
|
171 |
+
dec_json = fernet.decrypt(json_bytes).decode()
|
172 |
+
|
173 |
+
|
174 |
+
return(dec_im,mes_dec,dec_json)
|
175 |
+
|
176 |
+
def decode_doc(passw,doc=None):
|
177 |
+
key = create_key(passw)
|
178 |
+
fernet = Fernet(key)
|
179 |
+
doc_name = doc.name
|
180 |
+
doc_name = doc_name.split("/",4)[4]
|
181 |
+
#bytes_d = doc.encode()
|
182 |
+
|
183 |
+
#doc = Path(doc)
|
184 |
+
with open(doc.name, "rb") as file:
|
185 |
+
# read all file data
|
186 |
+
file_data = file.read()
|
187 |
+
dec_doc = fernet.decrypt(file_data)
|
188 |
+
og_name = doc.name
|
189 |
+
og_end = og_name.split(".",1)[1]
|
190 |
+
og_front=og_name.split(".",1)[0]
|
191 |
+
|
192 |
+
dec_file = doc.name.strip(".ocrpt")
|
193 |
+
#enc_file=f'{doc.name}.ocrpt'
|
194 |
+
with open(dec_file, "wb") as file:
|
195 |
+
file.write(dec_doc)
|
196 |
+
return dec_file
|
197 |
+
|
198 |
+
def decode_qr(im,passw):
|
199 |
+
with open(f'{im}', "rb") as image_file:
|
200 |
+
bytes_i = base64.b64encode(image_file.read())
|
201 |
+
decode_qr = stegan.decode(im)
|
202 |
+
dec_im,mes_dec,dec_json = decrypt(passw, enc_in=decode_qr)
|
203 |
+
|
204 |
+
return(dec_im,mes_dec,dec_json)
|
205 |
+
|
206 |
+
|
207 |
+
with gr.Blocks() as app:
|
208 |
+
with gr.Tab("Encrypt"):
|
209 |
+
pass_in=gr.Textbox(label="Set Password")
|
210 |
+
with gr.Tab("String"):
|
211 |
+
with gr.Row():
|
212 |
+
with gr.Column():
|
213 |
+
send=gr.Textbox(label="Sender")
|
214 |
+
rec=gr.Textbox(label="Recipient")
|
215 |
+
am=gr.Textbox(label="Amount")
|
216 |
+
with gr.Column():
|
217 |
+
mes = gr.Textbox(label = "Message")
|
218 |
+
with gr.Row():
|
219 |
+
im = gr.Image(type="filepath")
|
220 |
+
with gr.Tab("File"):
|
221 |
+
doc=gr.File()
|
222 |
+
enc_doc_out=gr.File()
|
223 |
+
en_btn = gr.Button("Encrypt")
|
224 |
+
enc_out = gr.Textbox(label="Encrypted Bytes")
|
225 |
+
enc_qr_out = gr.Image(label = "Encrypted QR")
|
226 |
+
|
227 |
+
with gr.Tab("Decrypt"):
|
228 |
+
pass_out = gr.Textbox(label="Enter Password")
|
229 |
+
with gr.Tab("String"):
|
230 |
+
enc_in = gr.Textbox(label="Encrypted Bytes")
|
231 |
+
d_btn = gr.Button("Decrypt")
|
232 |
+
d_txt = gr.Textbox(label="Decrypted Message")
|
233 |
+
d_im =gr.Image(label="Decrytped Image")
|
234 |
+
with gr.Tab("File"):
|
235 |
+
dec_doc_in = gr.File()
|
236 |
+
dec_doc_btn = gr.Button("Decrypt")
|
237 |
+
dec_doc_out=gr.File()
|
238 |
+
with gr.Tab("QR"):
|
239 |
+
dec_qr_im = gr.Image(type="filepath")
|
240 |
+
with gr.Row():
|
241 |
+
dec_qr_txt = gr.Textbox(label="Decrypted Message")
|
242 |
+
d_json = gr.Textbox(label="Decrypted JSON")
|
243 |
+
|
244 |
+
dec_qr_im_out =gr.Image(label="Decrytped Image")
|
245 |
+
dec_qr_btn = gr.Button("Decrypt")
|
246 |
+
|
247 |
+
dec_qr_btn.click(decode_qr,[dec_qr_im,pass_out],[dec_qr_im_out,dec_qr_txt,d_json])
|
248 |
+
dec_doc_btn.click(decode_doc,[pass_out,dec_doc_in],dec_doc_out)
|
249 |
+
en_btn.click(encrypt,[pass_in,mes,im,doc,send,rec,am],[enc_out,enc_doc_out,enc_qr_out,dec_qr_im])
|
250 |
+
d_btn.click(decrypt,[pass_out,enc_in],[d_im,d_txt,d_json])
|
251 |
+
app.launch()
|