-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.py
More file actions
146 lines (116 loc) · 4.96 KB
/
encrypt.py
File metadata and controls
146 lines (116 loc) · 4.96 KB
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
import sys
import os
from PyQt5.QtWidgets import QApplication, QFileDialog, QVBoxLayout, QPushButton, QLabel, QLineEdit, QWidget, QMessageBox
from PyQt5.QtCore import Qt
from cryptography.fernet import Fernet
import struct
class EncryptTool(QWidget):
HEADER = b"ENCRYPTED_FILE" # 标记文件已加密
def __init__(self):
super().__init__()
self.setWindowTitle("文件加密工具")
self.setGeometry(100, 100, 400, 200)
self.init_ui()
self.key = Fernet.generate_key()
self.cipher = Fernet(self.key)
def init_ui(self):
layout = QVBoxLayout()
# 显示选择的路径
self.path_label = QLabel("选择文件或文件夹路径:")
layout.addWidget(self.path_label)
self.path_input = QLineEdit()
self.path_input.setPlaceholderText("选择文件或文件夹路径...")
layout.addWidget(self.path_input)
# 浏览按钮
browse_button = QPushButton("浏览")
browse_button.clicked.connect(self.browse_path)
layout.addWidget(browse_button)
# 加密按钮
encrypt_button = QPushButton("加密")
encrypt_button.clicked.connect(self.encrypt_file_or_folder)
layout.addWidget(encrypt_button)
# 解密按钮
decrypt_button = QPushButton("解密")
decrypt_button.clicked.connect(self.decrypt_file_or_folder)
layout.addWidget(decrypt_button)
self.setLayout(layout)
def browse_path(self):
path, _ = QFileDialog.getOpenFileName(self, "选择文件") # 默认选择文件
if not path:
path = QFileDialog.getExistingDirectory(self, "选择文件夹") # 如果没选文件则尝试选择文件夹
if path:
self.path_input.setText(path)
def encrypt_file_or_folder(self):
path = self.path_input.text()
if not path:
QMessageBox.warning(self, "错误", "请先选择文件或文件夹路径!")
return
if os.path.isfile(path):
self.encrypt_file(path)
elif os.path.isdir(path):
self.encrypt_folder(path)
else:
QMessageBox.warning(self, "错误", "路径无效!")
def encrypt_file(self, filepath):
try:
with open(filepath, "rb") as file:
data = file.read()
# 检查文件是否已加密
if data.startswith(self.HEADER):
QMessageBox.warning(self, "错误", "文件已加密!")
return
encrypted_data = self.HEADER + self.cipher.encrypt(data)
with open(filepath, "wb") as file:
file.write(encrypted_data)
QMessageBox.information(self, "成功", f"文件已加密: {filepath}")
except Exception as e:
QMessageBox.critical(self, "错误", f"加密失败: {e}")
def encrypt_folder(self, folderpath):
try:
for root, dirs, files in os.walk(folderpath):
for file in files:
filepath = os.path.join(root, file)
self.encrypt_file(filepath)
QMessageBox.information(self, "成功", f"文件夹已加密: {folderpath}")
except Exception as e:
QMessageBox.critical(self, "错误", f"加密失败: {e}")
def decrypt_file_or_folder(self):
path = self.path_input.text()
if not path:
QMessageBox.warning(self, "错误", "请先选择文件或文件夹路径!")
return
if os.path.isfile(path):
self.decrypt_file(path)
elif os.path.isdir(path):
self.decrypt_folder(path)
else:
QMessageBox.warning(self, "错误", "路径无效!")
def decrypt_file(self, filepath):
try:
with open(filepath, "rb") as file:
data = file.read()
# 检查文件是否已加密
if not data.startswith(self.HEADER):
QMessageBox.warning(self, "错误", "文件不是加密文件!")
return
encrypted_data = data[len(self.HEADER) :]
decrypted_data = self.cipher.decrypt(encrypted_data)
with open(filepath, "wb") as file:
file.write(decrypted_data)
QMessageBox.information(self, "成功", f"文件已解密: {filepath}")
except Exception as e:
QMessageBox.critical(self, "错误", f"解密失败: {e}")
def decrypt_folder(self, folderpath):
try:
for root, dirs, files in os.walk(folderpath):
for file in files:
filepath = os.path.join(root, file)
self.decrypt_file(filepath)
QMessageBox.information(self, "成功", f"文件夹已解密: {folderpath}")
except Exception as e:
QMessageBox.critical(self, "错误", f"解密失败: {e}")
if __name__ == "__main__":
app = QApplication(sys.argv)
tool = EncryptTool()
tool.show()
sys.exit(app.exec_())