File size: 18,887 Bytes
870f264
 
 
 
661168b
 
f231878
 
b9b2117
0ad92bc
d178250
0ad92bc
870f264
 
 
 
e3536a1
870f264
 
7ae00b6
870f264
730c7dd
3fee23e
bb04f7a
a15f47c
0ad92bc
bb04f7a
 
 
 
 
 
 
 
 
 
 
 
0ad92bc
 
efdf194
9364c8f
b9b2117
 
 
0d5618b
b9b2117
 
 
 
 
 
1a82369
 
f231878
3995fb9
c0c54c8
f231878
 
 
 
c0c54c8
f231878
 
 
d178250
 
 
 
 
 
 
 
f231878
d7b7de1
d7b2896
f231878
e9637e4
f231878
 
 
 
 
 
 
 
 
 
 
e9637e4
 
092e8a6
 
 
 
065a299
092e8a6
bab441f
3995fb9
092e8a6
b2675cf
a15f47c
b2675cf
092e8a6
 
 
 
 
 
 
 
 
 
 
a15f47c
092e8a6
d7b7de1
976b974
1754ce2
6331b29
f231878
1386a30
d0d02a0
 
 
 
6aaeb91
 
 
 
7ae00b6
48c0cb2
c323edd
1bf5518
1386a30
 
d538e06
 
 
1386a30
96283e7
1386a30
 
de09aa4
 
 
96283e7
1386a30
80caa9b
de09aa4
 
 
96283e7
1386a30
 
de09aa4
 
 
a43f694
 
 
 
ad4b159
e9637e4
 
 
1386a30
4582320
53aac24
abed56d
 
 
 
42dc7db
ad4b159
 
b4463d6
 
e0a3dfc
a1e1a4a
f9f3e41
45cd04a
43f1145
a1e1a4a
8a0e6f6
5eb1003
 
 
 
d045d23
 
 
b521aaa
d045d23
b521aaa
6b92740
81c44d6
78868b0
8e09069
 
 
 
8a0e6f6
ad4b159
 
 
8e09069
81c44d6
 
e9637e4
c2978fc
 
 
bb6b131
f231878
 
 
 
 
 
 
 
 
 
 
0ad92bc
0d5618b
d7f6906
58a853a
e3536a1
d78fe3e
 
9364c8f
870f264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e91bf6c
102b50d
e9637e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e91bf6c
 
e9637e4
401104b
e9637e4
401104b
 
e9637e4
 
aeb845d
870f264
 
 
 
 
 
 
 
b837526
870f264
 
 
 
 
 
 
 
5c0d997
870f264
 
 
 
 
 
 
 
 
25a32e8
 
870f264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401104b
25a32e8
401104b
 
 
25a32e8
401104b
 
 
 
dec05f1
 
525631e
 
401104b
 
 
 
 
870f264
401104b
 
 
 
ebc426b
401104b
e9637e4
 
738892e
e9637e4
401104b
e9637e4
e302404
5a2d3c8
e302404
 
 
5a2d3c8
e302404
 
401104b
e9637e4
 
 
 
 
401104b
 
 
 
e9637e4
 
 
 
 
 
 
 
401104b
 
e9637e4
401104b
 
870f264
407f835
870f264
401104b
 
870f264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeb845d
870f264
 
 
 
f2821dc
 
 
2f7ac38
 
 
 
 
 
 
 
 
 
 
 
 
870f264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f7ac38
 
 
f2821dc
 
9364c8f
 
1e43ee3
58a853a
 
 
 
 
 
 
0d5618b
 
 
3d94c23
0d5618b
 
870f264
8d3ccd1
e9637e4
0d5618b
d178250
3b30522
870f264
f2821dc
0d5618b
870f264
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import gradio as gr
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP
import binascii
from base64 import b64decode
import base58

import os
import base64
import hashlib
from pathlib import Path
import qrcode as qrx
import qr
import stegan
import stegan2
import json
import re
import os
from Crypto.Hash import RIPEMD160, SHA256
#import pickle 

def calculate_hash(data, hash_function: str = "sha256") -> str:
    if type(data) == str:
        data = bytearray(data, "utf-8")
    if hash_function == "sha256":
        h = SHA256.new()
        h.update(data)
        return h.hexdigest()
    if hash_function == "ripemd160":
        h = RIPEMD160.new()
        h.update(data)
        return h.hexdigest()



eth_wal = gr.Interface.load("spaces/Omnibus/decentral-test-mod")

def rsa_encrypt_eth():

    priv = secrets.token_hex(32)
    #print (secrets)
    private_key = "0x" + priv
    print ("SAVE BUT DO NOT SHARE THIS:", private_key)
    acct = Account.from_key(private_key)
    print("Address:", acct.address)




def generate_keys1():
    key = RSA.generate(2048)
    private_key = key.export_key('PEM')
    file_out = open("private.pem", "wb")
    file_out.write(private_key)
    file_out.close()
    
    public_key = key.publickey().export_key('PEM')
    file_out = open("receiver.pem", "wb")
    file_out.write(public_key)
    file_out.close()


    hash_1 = calculate_hash(public_key, hash_function="sha256")
    hash_2 = calculate_hash(hash_1, hash_function="ripemd160")
    address = base58.b58encode(hash_2)
    address_im=qr.make_qr(txt=address)
    
    return public_key,private_key,address_im,address

def encrypt_text1(data,in2,address):
    data = data.encode("utf-8")
    #data = "I met aliens in UFO. Here is the map.".encode("utf-8")
    file_out = open("encrypted_data.bin", "wb")
    
    recipient_key = RSA.import_key(open("receiver.pem").read())
    session_key = get_random_bytes(16)
    
    # Encrypt the session key with the public RSA key
    cipher_rsa = PKCS1_OAEP.new(recipient_key)
    enc_session_key = cipher_rsa.encrypt(session_key)
    
    # Encrypt the data with the AES session key
    cipher_aes = AES.new(session_key, AES.MODE_EAX)
    ciphertext, tag = cipher_aes.encrypt_and_digest(data)
    [ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
    file_out.close()
################################
    #doc_name = doc.name
    #doc_name = doc_name.split("/",4)[4]
    doc_name = "encrypted_data.bin"
    with open(doc_name, "rb") as file:
        # read all file data
        file_data =(file.read())
        print (f'file_data::{file_data}')
    qr_link="test"
    enc_qr = stegan2.conv_im(qr_link=qr_link,data=file_data)
    
    file.close()
    #enc_doc = fernet.encrypt(file_data)
    #og_name = doc.name
    #og_end = og_name.split(".",1)[1]
    #og_front=og_name.split(".",1)[0]
    
    
    #enc_file=f'{doc.name}.ocrpt'
    #with open(enc_file, "wb") as file:
    #    file.write(enc_doc)
########################################    
    
    #output1 = pickle.dumps(output)
    #enc_qr = stegan2.conv_im(qr_link=address,data=out_file)
    
    #out = output
    #output.close()
    return str(file_data),enc_qr

def decrypt_text1(im,in2):
    mes1=""
    mes2=""
    mes3=""
    mes4=""
    mes_bytes1 =b''
    mes_bytes2 =b''
    mes_bytes3 =b''
    mes_bytes4 =b''
    enc_in = stegan2.decode(im)
    '''
    #enc_in=enc_in.replace('"',"'")
    print (f'enc_in::{enc_in}')
    if "+aaa+" in enc_in:
        mes1=enc_in.split("+aaa+",1)[0]
        mes_bytes1=mes1
        print (f'mes_bytes1::{mes_bytes1}')
        #mes_bytes1 = bytes(mes1)
        #mes_dec = fernet.decrypt(mes_bytes).decode()
    if "+bbb+" in enc_in:
        mes2=enc_in.split("+aaa+",1)[1]
        mes2=mes2.split("+bbb+",1)[0]
        #mes2=mes2.strip('b"').strip('"')
        mes_bytes2=mes2
       # mes_bytes2 = bytes(mes2)
    if "+ccc+" in enc_in:
        mes3=enc_in.split("+bbb+",1)[1]
        mes3=mes3.split("+ccc+",1)[0]
        #mes3=mes3.strip('b"').strip('"')
        mes_bytes3=mes3
        #mes_bytes3 = bytes(mes3)
    if "+ddd+" in enc_in:
        mes4=enc_in.split("+ccc+",1)[1]
        mes4=mes4.split("+ddd+",1)[0]
        #mes4=mes4.strip('b"').strip('"')
        mes_bytes4=mes4
        #mes_bytes4 = bytes(mes4)        
    print(f'mes_bytes1::{mes_bytes1}')
    print(f'mes_bytes2::{mes_bytes2}')
    print(f'mes_bytes3::{mes_bytes3}')
    print(f'mes_bytes4::{mes_bytes4}')
    
    #file_out = open("encrypted_data.bin", "w")
    #[ file_out.write(x) for x in (mes_bytes1, mes_bytes2, mes_bytes3, mes_bytes4) ]
    #file_out.close()

    #enc_in = stegan2.decode(im)
    #enc_in.replace('"','')
    #enc_in = base64.b64decode(enc_in)  
    #with open("encrypted_data1.bin", "wb") as file:
    #    file.write(enc_in)   
    #file.close()


    
    enc_in=enc_in.strip('"')
    #enc_in=enc_in.strip("b").strip("'")
    #enc_in = enc_in()
    print (f'enc_in 1:: {enc_in}')
    #enc_in = bytes(enc_in,"utf-8")  
    #enc_in = f"b'{enc_in}'"
    #enc_in = base64.b64encode(enc_in)  
    print (f'enc_in 2:: {enc_in}')
    '''

    private_key = RSA.import_key(open("private.pem").read())

    
    enc_session_key = enc_in[:private_key.size_in_bytes()]
    end1 = private_key.size_in_bytes()+16
    nonce = enc_in[private_key.size_in_bytes():end1]
    start1=end1+1
    end2 = private_key.size_in_bytes()+32
    start2=end2+1
    tag = enc_in[end1:end2]
    ciphertext = enc_in[end2:]
    #enc_session_key, nonce, tag, cipher_text =[ x for x in (enc_in[:private_key.size_in_bytes()],enc_in[private_key.size_in_bytes():private_key.size_in_bytes()+16],enc_in[private_key.size_in_bytes+16():private_key.size_in_bytes()+32],enc_in[-1]) ]
    print (f'enc_session_key::{enc_session_key}')
    print (f'nonce::{nonce}')
    print (f'tag::{tag}')
    print (f'ciphertext::{ciphertext}')    


    file_in = open("encrypted_data1.bin", "rb")
    #file_in=file_in.strip('"')
    
    #enc_session_key, nonce, tag, ciphertext = \
    #   [ file_in.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1) ]
    file_in.close()
    print (f'enc_session_key::{enc_session_key}')
    print (f'nonce::{nonce}')
    print (f'tag::{tag}')
    print (f'ciphertext::{ciphertext}')
    
    # Decrypt the session key with the private RSA key
    cipher_rsa = PKCS1_OAEP.new(private_key)
    session_key = cipher_rsa.decrypt(enc_session_key)
    
    # Decrypt the data with the AES session key
    cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
    data = cipher_aes.decrypt_and_verify(ciphertext, tag)
    return(data.decode("utf-8"))

    

    
def eth_create(seed = None):
    priv,pub,addr,seed = eth_wal(seed)
    qr_wal=qrx.make(addr)
    qr_wal.save("qr_wal_tmp.png")
    return priv,pub,addr,"qr_wal_tmp.png",seed

def trans_block(sender,recipient,amount):
    transaction = [{
        'sender': sender,
        'recipient': recipient,
        'amount': amount
    }]
    json_object = json.dumps(transaction, indent=4)
    print (json_object)
    return json_object

def create_key(passw):
    key = passw.encode()
    h = hashlib.new('sha256')
    h.update(key)
    key = h.hexdigest()
    key = key.encode()
    #salt = os.urandom(16)
    salt = key
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=480000,
    )
    key = base64.urlsafe_b64encode(kdf.derive(key))
    return key


def encrypt(passw,trans_s=None,trans_r=None,trans_a=None):
    #key = Fernet.generate_key()
    key = create_key(passw)
    fernet = Fernet(key)
    enc_mes=""
    enc_file=None
 
    if trans_s != None and trans_r != None and trans_a != None and trans_s != "" and trans_r != "" and trans_a != "":
        trans = trans_block(trans_s,trans_r,trans_a)
        bytes_j = trans.encode()

        recipient_key = RSA.import_key(open("receiver.pem").read())
        session_key = get_random_bytes(16)
        
        # Encrypt the session key with the public RSA key
        cipher_rsa = PKCS1_OAEP.new(recipient_key)
        enc_session_key = cipher_rsa.encrypt(session_key)
        
        # Encrypt the data with the AES session key
        cipher_aes = AES.new(session_key, AES.MODE_EAX)
        ciphertext, tag = cipher_aes.encrypt_and_digest(data)
        #[ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
        #file_out.close()
        out_list=[enc_session_key,cipher_aes.nonce,tag,ciphertext]
        json_form = {
            'enc_session_key':f"{enc_session_key}",
            'cipher_aes':f"{cipher_aes.nonce}",
            'tag':f"{tag}",
            'ciphertext':f"{ciphertext}"
        }
        #out_json.append(json_form)
        
        recipient_key = RSA.import_key(open("receiver.pem").read())

        #cipher_rsa = PKCS1_OAEP.new(recipient_key)
        #json_bytes = cipher_rsa.encrypt(bytes_j)
        #json_key = cipher_rsa.encrypt(key)

        
        #json_bytes = fernet.encrypt(bytes_j)
        enc_mes = f'{json_bytes}+ccc+{out_list}+ddd+'
        print(f'enc_mes out:: {enc_mes}')
    #enc_qr = qr.make_qr(txt=(enc_mes.strip('"')))
    m = hashlib.sha256()
    bytes_enc_mes = enc_mes.encode()
    m.update(bytes_enc_mes)
    out = m.hexdigest()
    out = out.encode()
    print(out)
    qr_link = out
    enc_qr = stegan2.conv_im(qr_link=qr_link,data=enc_mes)
  
    return enc_mes,enc_qr,enc_qr
    
def decrypt(passw,enc_in=None):
    key = create_key(passw)
    fernet = Fernet(key)
    dec_im = None
    mes_dec= None
    dec_json=None
    enc_in=enc_in.strip('"')
    print (f'enc_in :::: {enc_in}')
    

    
    if "+aaa+" in enc_in:
        mes1=enc_in.split("+aaa+",1)[0]
        mes1=mes1.strip("b'").strip("'")
        mes_bytes = bytes(mes1,'utf-8')

        
        mes_dec = fernet.decrypt(mes_bytes).decode()
        if "+bbb+" in enc_in:
            mes12=enc_in.split("+aaa+",1)[1]
            mes2=mes12.split("+bbb+",1)[0]
            mes2=mes2.strip("b'").strip("'")
            im_bytes = bytes(mes2,'utf-8')
            print(f'im_bytes::{im_bytes}')
            
            mes2 = fernet.decrypt(mes2).decode()
            #base = bytes(decMessage, 'utf-8')
            with open(f"finished_im.png", "wb") as fh:
                #fh.write(base64.decodebytes(im_bytes))
                fh.write(base64.decodebytes(bytes(mes2, 'utf-8')))
            fh.close     
            dec_im = "finished_im.png"
            if "+ccc+" in enc_in:
                mes13=enc_in.split("+bbb+",1)[1]
                mes3=mes13.split("+ccc+",1)[0]
                mes3=mes3.strip("b'").strip("'")
                json_bytes = bytes(mes3,'utf-8')
                print(f'im_bytes::{json_bytes}')
                dec_json = fernet.decrypt(json_bytes).decode()
        if not "+bbb+" in enc_in:
            if "+ccc+" in enc_in:
                mes14=enc_in.split("+aaa+",1)[1]
                
                mes4=mes14.split("+ccc+",1)[0]
                mes4=mes4.strip("b'").strip("'")
                json_bytes = bytes(mes4,'utf-8')
                print(f'im_bytes::{json_bytes}')
                dec_json = fernet.decrypt(json_bytes).decode()
   
    if not "+aaa+" in enc_in:
        if "+bbb+" in enc_in:
            mes2 = enc_in.split("+bbb+",1)[0]
            mes2=mes2.strip("b'").strip("'")
            im_bytes = bytes(mes2,'utf-8')
            print(f'im_bytes2::{im_bytes}')
            mes2 = fernet.decrypt(mes2).decode()

            #base = bytes(decMessage, 'utf-8')
            with open(f"finished_im.png", "wb") as fh:
                #fh.write(base64.decodebytes(im_bytes))
                fh.write(base64.decodebytes(bytes(mes2, 'utf-8')))
            fh.close     
            dec_im = "finished_im.png"
            if "+ccc+" in enc_in:
                mes13=enc_in.split("+bbb+",1)[1]
                mes3=mes13.split("+ccc+",1)[0]
                mes3=mes3.strip("b'").strip("'")
                json_bytes = bytes(mes3,'utf-8')
                print(f'im_bytes::{json_bytes}')
                dec_json = fernet.decrypt(json_bytes).decode()
        if not "+bbb+" in enc_in:
            if "+ccc+" in enc_in:
                mes4=enc_in.split("+ccc+",1)[0]
                mes14=mes4.strip("b'").strip("'")
                json_bytes = bytes(mes14,'utf-8')
                print(f'im_bytes::{json_bytes}')
                #private_key = RSA.import_key(open("private.pem").read())

                #cipher_rsa = PKCS1_OAEP.new(private_key)
                #dec_json = cipher_rsa.decrypt(json_bytes).decode()
                #print("done")
                
                dec_json = fernet.decrypt(json_bytes).decode()       
                    
    
    return(dec_im, mes_dec, dec_json)
def decrypt_json(im,enc_in=None):
    enc_in = stegan2.decode(im)
    #key = create_key(passw)
    #fernet = Fernet(key)
    dec_im = None
    mes_dec= None
    dec_json=None
    enc_in=enc_in.strip('"')
    print (f'enc_in :::: {enc_in}')

    if "+ccc+" in enc_in:
        if "+ddd+" in enc_in:
            mes15=enc_in.split("+ccc+",1)[1]
            mes5=mes15.split("+ddd+",1)[0]
            mes5=mes5.strip('"')
            mes5=mes5.strip("b'").strip("'")
            mes5=mes5.strip("[").strip("]")

            #session_key = 
            
            json_bytes = bytes(mes5,'utf-8')
            json_dump = JSON.dumps(json_bytes)
            file_out = open("encrypted_data.bin", "wb")
            [ file_out.write(json_bytes) ]
            file_out.close()   
            file_in = open("encrypted_data.bin", "rb")
            json_bytes = \
               [ file_in.read() ]
            file_in.close()            

            
        private_key = RSA.import_key(open("private.pem").read())
            #cipher_rsa = PKCS1_OAEP.new(private_key)
            #key = cipher_rsa.decrypt(json_bytes).decode()        
            #print (f'New Key:: {key}')
        #fernet = Fernet(key)
        mes4=enc_in.split("+ccc+",1)[0]
        mes14=mes4.strip("b'").strip("'")
        json_bytes = bytes(mes14,'utf-8')
        print(f'json_bytes22::{json_bytes}')
        # Decrypt the session key with the private RSA key
        cipher_rsa = PKCS1_OAEP.new(private_key)
        session_key = cipher_rsa.decrypt(enc_session_key)
        
        # Decrypt the data with the AES session key
        cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
        data = cipher_aes.decrypt_and_verify(ciphertext, tag)
        return(data.decode("utf-8"))
        #print("done")
        
        #dec_json = fernet.decrypt(json_bytes).decode()       

            
    
    return(dec_json)


    
def decode_doc(passw,doc=None):
    key = create_key(passw)
    fernet = Fernet(key)
    doc_name = doc.name
    doc_name = doc_name.split("/",4)[4]
    #bytes_d = doc.encode()
    
    #doc = Path(doc)
    with open(doc.name, "rb") as file:
        # read all file data
        file_data = file.read()
    dec_doc = fernet.decrypt(file_data)
    og_name = doc.name
    og_end = og_name.split(".",1)[1]
    og_front=og_name.split(".",1)[0]
    
    dec_file = doc.name.strip(".ocrpt")
    #enc_file=f'{doc.name}.ocrpt'
    with open(dec_file, "wb") as file:
        file.write(dec_doc)    
    return dec_file

def decode_qr(im,passw):
    with open(f'{im}', "rb") as image_file:
        bytes_i = base64.b64encode(image_file.read())     
    decode_qr = stegan.decode(im)
    print(f'decode_qr:: {decode_qr}')
    dec_im,mes_dec,dec_json = decrypt(passw, enc_in=decode_qr)
    
    return(dec_im,mes_dec,dec_json)
    
def predict(text, url_params):
    print(url_params)
    return ["Hello " + text + "!!", url_params]


get_window_url_params = """
    function(text_input, url_params) {
        console.log(text_input, url_params);
        const params = new URLSearchParams(window.location.search);
        url_params = Object.fromEntries(params);
        return [text_input, url_params];
        }
    """    



with gr.Blocks() as app:
    with gr.Row():
        gr.Column()
        with gr.Column():
            with gr.Tab("Encrypt"):
                pass_in=gr.Textbox(label="Set Password")
                with gr.Row():
                    with gr.Column():
                        send=gr.Textbox(label="Sender")
                        rec=gr.Textbox(label="Recipient")
                        am=gr.Textbox(label="Amount")    
                        
        
                en_btn = gr.Button("Encrypt")
                enc_out = gr.Textbox(label="Encrypted Bytes")
                enc_qr_out = gr.Image(label = "Encrypted QR")
        
            with gr.Tab("Decrypt"):
                pass_out = gr.Textbox(label="Enter Password")
        
                dec_qr_im = gr.Image(type="filepath")
                d_json = gr.JSON(label="Decrypted JSON")
                dec_qr_txt = gr.Textbox(label="Decrypted Message")
                dec_qr_im_out =gr.Image(label="Decrytped Image")
                dec_qr_btn = gr.Button("Decrypt")
            with gr.Tab("URL"):
                url_params = gr.JSON({}, visible=True, label="URL Params")
                url_btn = gr.Button("Get URL")
                text_input = gr.Text(label="Input",visible=False)
                text_output = gr.Text(label="Output",visible=False)
            with gr.Tab("Gen Wal"):
                gen_wal_btn=gr.Button()
                seed = gr.Textbox(label='Seed Phrase')
                with gr.Box():
                    with gr.Row():
                        with gr.Column():
                            out1 = gr.Textbox(label='Private Key')
                            out2 = gr.Textbox(label='Public Key')
                            img3=gr.Pil()
                            out3 = gr.Textbox(label='Address')
                rsa_to_enc = gr.Textbox(label="txt to encrypt")
                rsa_enc_btn = gr.Button("RSA Encrypt")
                rsa_enc_mes = gr.Textbox(label="encoded")
                qr_enc_mes = gr.Image(type="filepath")
                rsa_dec_btn = gr.Button("RSA Decrypt")
                rsa_dec_mes = gr.Textbox(label="decoded")
        gr.Column()
    rsa_enc_btn.click(encrypt_text1,[rsa_to_enc,out2,out3],[rsa_enc_mes,qr_enc_mes])
    rsa_dec_btn.click(decrypt_text1,[qr_enc_mes,out1],rsa_dec_mes)
    
    gen_wal_btn.click(generate_keys1,None,[out2,out1, img3,out3])
    dec_qr_btn.click(decrypt_json,[dec_qr_im,pass_out],[dec_qr_im_out,dec_qr_txt,d_json])
    en_btn.click(encrypt,[pass_in,send,rec,am],[enc_out,enc_qr_out,dec_qr_im])
    url_btn.click(fn=predict, inputs=[text_input,url_params],outputs=[text_output,url_params], _js=get_window_url_params)                
    
app.launch()