如何用python加密文件
时间: 2023-05-21 22:04:01 浏览: 76
可以使用Python中的cryptography库来加密文件。以下是一个简单的示例代码:
```python
from cryptography.fernet import Fernet
# 生成密钥
key = Fernet.generate_key()
# 保存密钥到文件
with open('key.key', 'wb') as key_file:
key_file.write(key)
# 读取密钥
with open('key.key', 'rb') as key_file:
key = key_file.read()
# 创建Fernet对象
fernet = Fernet(key)
# 加密文件
with open('plaintext.txt', 'rb') as plaintext_file:
plaintext = plaintext_file.read()
encrypted = fernet.encrypt(plaintext)
with open('encrypted.txt', 'wb') as encrypted_file:
encrypted_file.write(encrypted)
```
在上面的代码中,我们首先生成一个密钥,然后将其保存到文件中。接着,我们读取密钥并使用它创建一个Fernet对象。最后,我们使用Fernet对象加密文件并将加密后的内容保存到另一个文件中。
需要注意的是,加密后的文件不能直接打开查看,需要使用相同的密钥进行解密。
相关问题
用python加密文件
可以使用 python 的 `pycrypto` 库来加密文件。
先安装pycrypto库
```
pip install pycrypto
```
示例代码如下:
```python
from Crypto.Cipher import AES
# 需要加密的文件
file_path = 'example.txt'
# 密钥
key = b'Sixteen byte key'
# 初始化加密器
cipher = AES.new(key, AES.MODE_EAX)
# 读取文件
with open(file_path, 'rb') as f:
plaintext = f.read()
# 加密文件
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
# 保存加密后的文件
with open(file_path, 'wb') as f:
[f.write(x) for x in (cipher.nonce, tag, ciphertext)]
```
这样就可以将文件加密了。
如果需要解密
```python
from Crypto.Cipher import AES
# 需要解密的文件
file_path = 'example.txt'
# 密钥
key = b'Sixteen byte key'
# 读取加密后的文件
with open(file_path, 'rb') as f:
nonce, tag, ciphertext = [f.read(x) for x in (16, 16, -1)]
# 初始化解密器
cipher = AES.new(key, AES.MODE_EAX, nonce)
# 解密文件
plaintext = cipher.decrypt_and_verify(ciphertext, tag)
# 保存解密后的文件
with open(file_path, 'wb') as f:
f.write(plaintext)
```
请注意,这只是一个示例,实际使用中需要根据实际情况调整代码。
使用Python实现加密传输文件
要使用Python实现加密传输文件,可以使用`socket`库进行网络通信,使用`cryptography`库进行文件加密。以下是一个简单的示例:
1. 首先,安装`cryptography`库:
```bash
pip install cryptography
```
2. 创建一个名为`server.py`的文件,用于接收客户端发送的加密文件:
```python
import socket
from cryptography.fernet import Fernet
# 生成密钥并保存到文件
key = Fernet.generate_key()
with open("key.key", "wb") as key_file:
key_file.write(key)
# 加载密钥
with open("key.key", "rb") as key_file:
key = key_file.read()
cipher_suite = Fernet(key)
# 创建服务器套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 12345))
server_socket.listen(1)
print("等待客户端连接...")
# 接受客户端连接
client_socket, client_address = server_socket.accept()
print(f"客户端 {client_address} 已连接")
# 接收加密文件
with open("received_file.txt", "wb") as received_file:
encrypted_data = client_socket.recv(1024)
decrypted_data = cipher_suite.decrypt(encrypted_data)
received_file.write(decrypted_data)
print("文件接收成功")
# 关闭套接字
client_socket.close()
server_socket.close()
```
3. 创建一个名为`client.py`的文件,用于发送加密文件给服务器:
```python
import socket
from cryptography.fernet import Fernet
# 生成密钥并保存到文件
key = Fernet.generate_key()
with open("key.key", "wb") as key_file:
key_file.write(key)
# 加载密钥
with open("key.key", "rb") as key_file:
key = key_file.read()
cipher_suite = Fernet(key)
# 创建客户端套接字
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 12345))
# 读取要发送的文件
with open("send_file.txt", "rb") as send_file:
encrypted_data = cipher_suite.encrypt(send_file.read())
# 发送加密文件给服务器
client_socket.sendall(encrypted_data)
print("文件发送成功")
# 关闭套接字
client_socket.close()
```
运行`server.py`和`client.py`,客户端将加密文件发送给服务器,服务器解密后接收文件。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)