Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,63 +38,16 @@ def encrypt(passw=None,mes=None,img=None,document=None):
|
|
| 38 |
bytes_e = base64.b64encode(image_file.read())
|
| 39 |
print (bytes_e)
|
| 40 |
#key = Fernet.generate_key()
|
| 41 |
-
|
| 42 |
key = create_key(passw)
|
| 43 |
-
'''
|
| 44 |
-
key = passw.encode()
|
| 45 |
-
h = hashlib.new('sha256')
|
| 46 |
-
h.update(key)
|
| 47 |
-
key = h.hexdigest()
|
| 48 |
-
key = key.encode()
|
| 49 |
-
#salt = os.urandom(16)
|
| 50 |
-
salt = key
|
| 51 |
-
kdf = PBKDF2HMAC(
|
| 52 |
-
algorithm=hashes.SHA256(),
|
| 53 |
-
length=32,
|
| 54 |
-
salt=salt,
|
| 55 |
-
iterations=480000,
|
| 56 |
-
)
|
| 57 |
-
key = base64.urlsafe_b64encode(kdf.derive(key))
|
| 58 |
-
print (f'key == {key}')
|
| 59 |
-
'''
|
| 60 |
fernet = Fernet(key)
|
| 61 |
-
|
| 62 |
-
# then use the Fernet class instance
|
| 63 |
-
# to encrypt the string string must
|
| 64 |
-
# be encoded to byte string before encryption
|
| 65 |
-
#global encMessage
|
| 66 |
encMessage = fernet.encrypt(bytes_e)
|
| 67 |
-
|
| 68 |
-
#print("original string: ", mes)
|
| 69 |
-
print("encrypted string: ", encMessage)
|
| 70 |
-
print (f'encoded string == {encMessage}')
|
| 71 |
-
|
| 72 |
return encMessage
|
|
|
|
| 73 |
def decrypt(passw=None,enc_in=None):
|
| 74 |
enc_in=enc_in.strip('"').strip("b'").strip("'")
|
| 75 |
-
print (f'enc_in stripped == {enc_in}')
|
| 76 |
enc_in = bytes(enc_in,'utf-8')
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
#enc_in=encMessage
|
| 80 |
-
#enc_in=bytes(enc_in,'utf-8')
|
| 81 |
-
#print (f'encoded string == {enc_in}')
|
| 82 |
-
'''
|
| 83 |
-
key = passw.encode()
|
| 84 |
-
h = hashlib.new('sha256')
|
| 85 |
-
h.update(key)
|
| 86 |
-
key = h.hexdigest()
|
| 87 |
-
key = key.encode()
|
| 88 |
-
#salt = os.urandom(16)
|
| 89 |
-
salt = key
|
| 90 |
-
kdf = PBKDF2HMAC(
|
| 91 |
-
algorithm=hashes.SHA256(),
|
| 92 |
-
length=32,
|
| 93 |
-
salt=salt,
|
| 94 |
-
iterations=480000,
|
| 95 |
-
)
|
| 96 |
-
key = base64.urlsafe_b64encode(kdf.derive(key))
|
| 97 |
-
'''
|
| 98 |
key = create_key(passw)
|
| 99 |
|
| 100 |
fernet = Fernet(key)
|
|
@@ -109,6 +62,9 @@ def decrypt(passw=None,enc_in=None):
|
|
| 109 |
except Exception:
|
| 110 |
dec_im = None
|
| 111 |
return(dec_im,decMessage)
|
|
|
|
|
|
|
|
|
|
| 112 |
with gr.Blocks() as app:
|
| 113 |
with gr.Tab("Encrypt"):
|
| 114 |
pass_in=gr.Textbox(label="Set Password")
|
|
|
|
| 38 |
bytes_e = base64.b64encode(image_file.read())
|
| 39 |
print (bytes_e)
|
| 40 |
#key = Fernet.generate_key()
|
|
|
|
| 41 |
key = create_key(passw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
fernet = Fernet(key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
encMessage = fernet.encrypt(bytes_e)
|
| 44 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
return encMessage
|
| 46 |
+
|
| 47 |
def decrypt(passw=None,enc_in=None):
|
| 48 |
enc_in=enc_in.strip('"').strip("b'").strip("'")
|
|
|
|
| 49 |
enc_in = bytes(enc_in,'utf-8')
|
| 50 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
key = create_key(passw)
|
| 52 |
|
| 53 |
fernet = Fernet(key)
|
|
|
|
| 62 |
except Exception:
|
| 63 |
dec_im = None
|
| 64 |
return(dec_im,decMessage)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
with gr.Blocks() as app:
|
| 69 |
with gr.Tab("Encrypt"):
|
| 70 |
pass_in=gr.Textbox(label="Set Password")
|