Spaces:
Sleeping
Sleeping
File size: 5,214 Bytes
539c7c3 8181693 e7b5e28 6d5aa24 025ae20 7dce7b2 4679c40 d76b993 c3b92ce 8181693 14cf141 d7d3635 a2ee0d5 4679c40 cbe0ea8 4679c40 a2ee0d5 539c7c3 015f310 cbe0ea8 d454933 6b71a29 7258d35 a26cc88 2a42089 9248bc6 06b239b 40d5fc0 2a42089 6b71a29 2a42089 a26cc88 2a42089 6b71a29 4679c40 a2ee0d5 fbbfd2f 72c201a 1d690ec d128263 717263c cbe0ea8 8a33270 1d690ec 0193816 7d70e27 1d690ec aebea04 5667f71 1048887 cbe0ea8 cb074ce cbe0ea8 5251401 5381e78 aebea04 886be9a 5251401 cb074ce 5251401 72c201a cbe0ea8 717263c d454933 717263c 539c7c3 4679c40 6b71a29 aeb8ec1 4679c40 7f09474 a2eea8c 4679c40 7f09474 717263c a2eea8c 4679c40 539c7c3 | 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 |
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
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=None,mes=None,img=None,doc=None):
#key = Fernet.generate_key()
key = create_key(passw)
fernet = Fernet(key)
enc_mes=None
enc_file=None
if mes != None and mes != "":
bytes_m = mes.encode()
enc_mes = fernet.encrypt(bytes_m)
enc_mes = f'{enc_mes}+aaa+'
if img != None:
with open(f'{img}', "rb") as image_file:
bytes_i = base64.b64encode(image_file.read())
if mes != None and mes != "":
im_bytes = fernet.encrypt(bytes_i)
enc_mes = f'{enc_mes}{im_bytes}+bbb+'
else:
enc_mes = f'{im_bytes}+bbb+'
if doc != None:
print(dir(doc))
print(doc.name)
#print(doc.file)
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()
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)
return enc_mes,enc_file
def decrypt(passw=None,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 "+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 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"
return(dec_im,mes_dec)
def decode_doc(passw=None,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
with gr.Blocks() as app:
with gr.Tab("Encrypt"):
pass_in=gr.Textbox(label="Set Password")
mes = gr.Textbox(label = "Message")
with gr.Row():
im = gr.Image(type="filepath")
doc=gr.File()
en_btn = gr.Button("Encrypt")
enc_out = gr.Textbox(label="Encrypted Bytes")
enc_doc_out=gr.File()
with gr.Tab("Decrypt"):
pass_out = gr.Textbox(label="Enter Password")
with gr.Tab("String"):
enc_in = gr.Textbox(label="Encrypted Bytes")
d_btn = gr.Button("Decrypt")
d_txt = gr.Textbox(label="Decrypted")
d_im =gr.Image(label="Decrytped Image")
with gr.Tab("File"):
dec_doc_in = gr.File()
dec_doc_btn = gr.Button("Decrypt")
dec_doc_out=gr.File()
dec_doc_btn.click(decode_doc,[pass_out,dec_doc_in],dec_doc_out)
en_btn.click(encrypt,[pass_in,mes,im,doc],[enc_out,enc_doc_out])
d_btn.click(decrypt,[pass_out,enc_in],[d_im,d_txt])
app.launch()
|