Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,21 @@
|
|
1 |
import gradio as gr
|
2 |
from cryptography.fernet import Fernet
|
|
|
3 |
|
4 |
# AES 加密函数(Fernet 实现)
|
5 |
def encrypt_file_gr(file):
|
6 |
-
# `file` 是一个 NamedTemporaryFile 对象,带有属性 `name`
|
7 |
file_path = file.name
|
8 |
-
# 生成密钥并初始化 cipher
|
9 |
key = Fernet.generate_key()
|
10 |
cipher = Fernet(key)
|
11 |
-
# 读取原文件并加密
|
12 |
with open(file_path, 'rb') as f:
|
13 |
data = f.read()
|
14 |
encrypted_data = cipher.encrypt(data)
|
15 |
-
# 写入加密文件
|
16 |
enc_path = file_path + '.enc'
|
17 |
with open(enc_path, 'wb') as f:
|
18 |
f.write(encrypted_data)
|
19 |
-
# 返回密钥和加密文件路径
|
20 |
return key.decode(), enc_path
|
21 |
|
22 |
-
# AES
|
23 |
-
|
24 |
def decrypt_file_gr(file, key):
|
25 |
file_path = file.name
|
26 |
cipher = Fernet(key.encode())
|
@@ -28,12 +23,15 @@ def decrypt_file_gr(file, key):
|
|
28 |
with open(file_path, 'rb') as f:
|
29 |
encrypted_data = f.read()
|
30 |
decrypted_data = cipher.decrypt(encrypted_data)
|
31 |
-
|
|
|
|
|
|
|
|
|
32 |
with open(dec_path, 'wb') as f:
|
33 |
f.write(decrypted_data)
|
34 |
return dec_path
|
35 |
-
except Exception
|
36 |
-
# 如果解密失败,返回空,让 Gradio 显示错误
|
37 |
return None
|
38 |
|
39 |
# Gradio 界面设计
|
@@ -43,7 +41,8 @@ with gr.Blocks() as demo:
|
|
43 |
with gr.Tab("加密"):
|
44 |
encrypt_in = gr.File(label="上传文件")
|
45 |
encrypt_btn = gr.Button("加密文件 🔒")
|
46 |
-
|
|
|
47 |
encrypt_out = gr.File(label="下载加密文件 (.enc)")
|
48 |
encrypt_btn.click(
|
49 |
fn=encrypt_file_gr,
|
@@ -55,13 +54,12 @@ with gr.Blocks() as demo:
|
|
55 |
decrypt_in = gr.File(label="上传加密文件 (.enc)")
|
56 |
decrypt_key_in = gr.Textbox(label="输入密钥")
|
57 |
decrypt_btn = gr.Button("解密文件 🔓")
|
58 |
-
decrypt_out = gr.File(label="
|
59 |
decrypt_btn.click(
|
60 |
fn=decrypt_file_gr,
|
61 |
inputs=[decrypt_in, decrypt_key_in],
|
62 |
outputs=decrypt_out
|
63 |
)
|
64 |
|
65 |
-
# 本地服务器启动函数,Hugging Face Spaces 会自动调用 demo.launch()
|
66 |
if __name__ == "__main__":
|
67 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from cryptography.fernet import Fernet
|
3 |
+
import os
|
4 |
|
5 |
# AES 加密函数(Fernet 实现)
|
6 |
def encrypt_file_gr(file):
|
|
|
7 |
file_path = file.name
|
|
|
8 |
key = Fernet.generate_key()
|
9 |
cipher = Fernet(key)
|
|
|
10 |
with open(file_path, 'rb') as f:
|
11 |
data = f.read()
|
12 |
encrypted_data = cipher.encrypt(data)
|
|
|
13 |
enc_path = file_path + '.enc'
|
14 |
with open(enc_path, 'wb') as f:
|
15 |
f.write(encrypted_data)
|
|
|
16 |
return key.decode(), enc_path
|
17 |
|
18 |
+
# AES 解密函数,恢复原始文件名和格式
|
|
|
19 |
def decrypt_file_gr(file, key):
|
20 |
file_path = file.name
|
21 |
cipher = Fernet(key.encode())
|
|
|
23 |
with open(file_path, 'rb') as f:
|
24 |
encrypted_data = f.read()
|
25 |
decrypted_data = cipher.decrypt(encrypted_data)
|
26 |
+
# 去掉 .enc 后缀,恢复原始文件名
|
27 |
+
if file_path.lower().endswith('.enc'):
|
28 |
+
dec_path = file_path[:-4]
|
29 |
+
else:
|
30 |
+
dec_path = file_path + '.dec'
|
31 |
with open(dec_path, 'wb') as f:
|
32 |
f.write(decrypted_data)
|
33 |
return dec_path
|
34 |
+
except Exception:
|
|
|
35 |
return None
|
36 |
|
37 |
# Gradio 界面设计
|
|
|
41 |
with gr.Tab("加密"):
|
42 |
encrypt_in = gr.File(label="上传文件")
|
43 |
encrypt_btn = gr.Button("加密文件 🔒")
|
44 |
+
# 文本框添加 copyable 属性,实现一键复制功能
|
45 |
+
encrypt_key = gr.Textbox(label="生成的密钥", interactive=False, copyable=True)
|
46 |
encrypt_out = gr.File(label="下载加密文件 (.enc)")
|
47 |
encrypt_btn.click(
|
48 |
fn=encrypt_file_gr,
|
|
|
54 |
decrypt_in = gr.File(label="上传加密文件 (.enc)")
|
55 |
decrypt_key_in = gr.Textbox(label="输入密钥")
|
56 |
decrypt_btn = gr.Button("解密文件 🔓")
|
57 |
+
decrypt_out = gr.File(label="下载解密文件(原始格式)")
|
58 |
decrypt_btn.click(
|
59 |
fn=decrypt_file_gr,
|
60 |
inputs=[decrypt_in, decrypt_key_in],
|
61 |
outputs=decrypt_out
|
62 |
)
|
63 |
|
|
|
64 |
if __name__ == "__main__":
|
65 |
+
demo.launch()
|