Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,19 +7,10 @@ import os
|
|
| 7 |
import base64
|
| 8 |
import hashlib
|
| 9 |
|
| 10 |
-
def encrypt(mes=None,img=None,document=None):
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
elif mes == None or mes=="" and img != None:
|
| 16 |
-
with open(f'{img}', "rb") as image_file:
|
| 17 |
-
bytes_e = base64.b64encode(image_file.read())
|
| 18 |
-
print (bytes_e)
|
| 19 |
-
#key = Fernet.generate_key()
|
| 20 |
-
key = "potato"
|
| 21 |
-
|
| 22 |
-
key = key.encode()
|
| 23 |
h = hashlib.new('sha256')
|
| 24 |
h.update(key)
|
| 25 |
key = h.hexdigest()
|
|
@@ -27,9 +18,7 @@ def encrypt(mes=None,img=None,document=None):
|
|
| 27 |
print (key)
|
| 28 |
print (h)
|
| 29 |
#key = h
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
password=key
|
| 33 |
#salt = os.urandom(16)
|
| 34 |
salt = key
|
| 35 |
kdf = PBKDF2HMAC(
|
|
@@ -38,8 +27,21 @@ def encrypt(mes=None,img=None,document=None):
|
|
| 38 |
salt=salt,
|
| 39 |
iterations=480000,
|
| 40 |
)
|
| 41 |
-
key = base64.urlsafe_b64encode(kdf.derive(
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
fernet = Fernet(key)
|
| 44 |
|
| 45 |
# then use the Fernet class instance
|
|
@@ -49,24 +51,37 @@ def encrypt(mes=None,img=None,document=None):
|
|
| 49 |
print(f'Length in Bytes: {len(encMessage)}')
|
| 50 |
#print("original string: ", mes)
|
| 51 |
print("encrypted string: ", encMessage)
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
decMessage = fernet.decrypt(encMessage).decode()
|
| 60 |
base = bytes(decMessage, 'utf-8')
|
| 61 |
with open(f"finished_im.png", "wb") as fh:
|
| 62 |
fh.write(base64.decodebytes(bytes(decMessage, 'utf-8')))
|
| 63 |
fh.close
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
with gr.Blocks() as app:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
app.launch()
|
| 72 |
|
|
|
|
| 7 |
import base64
|
| 8 |
import hashlib
|
| 9 |
|
|
|
|
| 10 |
|
| 11 |
+
|
| 12 |
+
def create_key(passw):
|
| 13 |
+
key = passw.encode()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
h = hashlib.new('sha256')
|
| 15 |
h.update(key)
|
| 16 |
key = h.hexdigest()
|
|
|
|
| 18 |
print (key)
|
| 19 |
print (h)
|
| 20 |
#key = h
|
| 21 |
+
#password=key
|
|
|
|
|
|
|
| 22 |
#salt = os.urandom(16)
|
| 23 |
salt = key
|
| 24 |
kdf = PBKDF2HMAC(
|
|
|
|
| 27 |
salt=salt,
|
| 28 |
iterations=480000,
|
| 29 |
)
|
| 30 |
+
key = base64.urlsafe_b64encode(kdf.derive(key))
|
| 31 |
+
return key
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def encrypt(passw=None,mes=None,img=None,document=None):
|
| 35 |
+
|
| 36 |
+
if mes != None and mes != "":
|
| 37 |
+
bytes_e = mes.encode()
|
| 38 |
+
print (bytes_e)
|
| 39 |
+
elif mes == None or mes=="" and img != None:
|
| 40 |
+
with open(f'{img}', "rb") as image_file:
|
| 41 |
+
bytes_e = base64.b64encode(image_file.read())
|
| 42 |
+
print (bytes_e)
|
| 43 |
+
#key = Fernet.generate_key()
|
| 44 |
+
key = create_key(passw)
|
| 45 |
fernet = Fernet(key)
|
| 46 |
|
| 47 |
# then use the Fernet class instance
|
|
|
|
| 51 |
print(f'Length in Bytes: {len(encMessage)}')
|
| 52 |
#print("original string: ", mes)
|
| 53 |
print("encrypted string: ", encMessage)
|
| 54 |
+
return encMessage
|
| 55 |
+
def decrypt(passw=None,enc_in=None):
|
| 56 |
+
key = create_key(passw)
|
| 57 |
|
| 58 |
+
|
| 59 |
+
decMessage = fernet.decrypt(enc_in).decode()
|
| 60 |
+
print("decrypted string: ", decMessage)
|
| 61 |
+
try:
|
|
|
|
| 62 |
base = bytes(decMessage, 'utf-8')
|
| 63 |
with open(f"finished_im.png", "wb") as fh:
|
| 64 |
fh.write(base64.decodebytes(bytes(decMessage, 'utf-8')))
|
| 65 |
fh.close
|
| 66 |
+
dec_im = "finished_im.png"
|
| 67 |
+
except Exception:
|
| 68 |
+
dec_im = None
|
| 69 |
+
return(dec_im,decMessage)
|
| 70 |
with gr.Blocks() as app:
|
| 71 |
+
with gr.Tab("Encrypt"):
|
| 72 |
+
pass_in=gr.Textbox(label="Set Password")
|
| 73 |
+
mes = gr.Textbox(label = "Message")
|
| 74 |
+
im = gr.Image(type="filepath")
|
| 75 |
+
en_btn = gr.Button("Encrypt")
|
| 76 |
+
enc_out = gr.Textbox("Encoded Bytes")
|
| 77 |
+
with gr.Tab("Decrypt"):
|
| 78 |
+
pass_out = gr.Textbox("Enter Password")
|
| 79 |
+
enc_in = gr.Textbox("Encrypted Text")
|
| 80 |
+
d_btn = gr.Button("Decrypt")
|
| 81 |
+
d_txt = gr.Textbox()
|
| 82 |
+
d_im =gr.Image()
|
| 83 |
+
d_txt=gr.Textbox()
|
| 84 |
+
en_btn.click(encrypt,[pass_in,mes,im],enc_out)
|
| 85 |
+
d_btn.click(decrypt,[pass_out,enc_in],[d_im,d_txt])
|
| 86 |
app.launch()
|
| 87 |
|