Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from cryptography.fernet import Fernet
|
| 4 |
+
def encrypt():
|
| 5 |
+
# we will be encrypting the below string.
|
| 6 |
+
message = "hello geeks"
|
| 7 |
+
|
| 8 |
+
# generate a key for encryption and decryption
|
| 9 |
+
# You can use fernet to generate
|
| 10 |
+
# the key or use random key generator
|
| 11 |
+
# here I'm using fernet to generate key
|
| 12 |
+
|
| 13 |
+
key = Fernet.generate_key()
|
| 14 |
+
|
| 15 |
+
# Instance the Fernet class with the key
|
| 16 |
+
|
| 17 |
+
fernet = Fernet(key)
|
| 18 |
+
|
| 19 |
+
# then use the Fernet class instance
|
| 20 |
+
# to encrypt the string string must
|
| 21 |
+
# be encoded to byte string before encryption
|
| 22 |
+
encMessage = fernet.encrypt(message.encode())
|
| 23 |
+
|
| 24 |
+
print("original string: ", message)
|
| 25 |
+
print("encrypted string: ", encMessage)
|
| 26 |
+
|
| 27 |
+
# decrypt the encrypted string with the
|
| 28 |
+
# Fernet instance of the key,
|
| 29 |
+
# that was used for encrypting the string
|
| 30 |
+
# encoded byte string is returned by decrypt method,
|
| 31 |
+
# so decode it to string with decode methods
|
| 32 |
+
decMessage = fernet.decrypt(encMessage).decode()
|
| 33 |
+
|
| 34 |
+
print("decrypted string: ", decMessage)
|
| 35 |
+
|
| 36 |
+
with gr.Blocks() as app:
|
| 37 |
+
btn = gr.Button()
|
| 38 |
+
btn.click(encrypt,None,None)
|
| 39 |
+
app.launch()
|
| 40 |
+
|