File size: 4,145 Bytes
e255167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94a5bbf
e255167
 
 
 
 
d4cb47b
e255167
 
 
d4cb47b
 
 
e255167
 
 
 
 
 
 
 
 
 
d4cb47b
 
e255167
 
 
 
 
 
 
 
 
 
1a59183
 
 
 
 
 
e255167
 
1a59183
e255167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae87355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53a9a86
 
 
ae87355
 
53a9a86
94a5bbf
e255167
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
import gradio as gr
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import os
import base64
import hashlib
from pathlib import Path
import qr
import stegan
import json

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()

        json_bytes = fernet.encrypt(bytes_j)
        enc_mes = f'{json_bytes}+ccc+'

    #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 = stegan.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
    enc_in=enc_in.strip('"')
    print (f'enc_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}')
        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)
    dec_im,mes_dec,dec_json = decrypt(passw, enc_in=decode_qr)
    
    return(dec_im,mes_dec,dec_json)
    
    
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_btn = gr.Button("Decrypt")
        gr.Column()
    dec_qr_btn.click(decode_qr,[dec_qr_im,pass_out],[d_json])
    en_btn.click(encrypt,[pass_in,send,rec,am],[enc_out,enc_qr_out,dec_qr_im])
app.launch()