python对文本文件加密
时间: 2023-10-13 18:09:30 浏览: 49
Python有很多加密模块可以用于文本文件加密,其中比较常用的有Cryptography和PyCrypto。下面是一个使用Cryptography模块对文本文件进行加密的示例代码:
```python
from cryptography.fernet import Fernet
# 生成密钥
key = Fernet.generate_key()
# 读取明文文件
with open('plaintext.txt', 'rb') as f:
plaintext = f.read()
# 加密明文文件
f = Fernet(key)
encrypted_data = f.encrypt(plaintext)
# 将密文保存到文件
with open('encrypted.txt', 'wb') as f:
f.write(encrypted_data)
# 将密钥保存到文件
with open('key.txt', 'wb') as f:
f.write(key)
```
这段代码会生成一个随机的密钥,然后读取明文文件并使用密钥加密,最后将密文和密钥保存到文件。要解密文件,只需要读取密文和密钥文件,然后使用密钥解密即可。需要注意的是,密钥文件需要妥善保管,避免泄露。
相关问题
python rsa对txt进行加密解密
下面是Python的RSA加密和解密示例:
```python
# 导入库
import rsa
# 生成密钥对
(pubkey, privkey) = rsa.newkeys(1024)
# 显示公钥和私钥
print(pubkey)
print(privkey)
# 加密文本
message = b"Hello world"
ciphertext = rsa.encrypt(message, pubkey)
# 显示密文
print(ciphertext)
# 解密文本
plaintext = rsa.decrypt(ciphertext, privkey)
# 显示明文
print(plaintext)
```
以上代码会生成一个1024位的RSA密钥对并显示出来。然后对文本进行加密并显示出密文,再对密文进行解密并显示出明文。
注意:如果你想从.txt文件中读取文本并加密,可以使用以下代码:
```python
# 读取明文
with open("plaintext.txt", "rb") as f:
message = f.read()
# 加密文本
ciphertext = rsa.encrypt(message, pubkey)
# 保存密文
with open("ciphertext.txt", "wb") as f:
f.write(ciphertext)
```
解密也是类似的:
```python
# 读取密文
with open("ciphertext.txt", "rb") as f:
ciphertext = f.read()
# 解密文本
plaintext = rsa.decrypt(ciphertext, privkey)
# 保存明文
with open("plaintext.txt", "wb") as f:
f.write(plaintext)
```
希望这样可以帮助到你!
python将文本文件发送到邮箱
你可以使用Python的SMTP库来发送邮件,具体步骤如下:
1. 导入SMTP库
```python
import smtplib
```
2. 设置SMTP服务器和端口号
```python
smtp_server = 'smtp.邮箱服务商.com' # 邮箱服务商的SMTP服务器地址
smtp_port = 587 # SMTP服务器的端口号
```
3. 登录邮箱账号
```python
sender = '发送者邮箱地址'
password = '发送者邮箱密码'
smtp_obj = smtplib.SMTP(smtp_server, smtp_port)
smtp_obj.starttls() # 开启TLS加密
smtp_obj.login(sender, password)
```
4. 设置邮件内容
```python
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# 创建MIMEMultipart对象
msg = MIMEMultipart()
# 构建邮件正文
body = MIMEText('邮件正文')
msg.attach(body)
# 构建邮件附件
with open('文件路径', 'rb') as f:
attachment = MIMEApplication(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename='文件名')
msg.attach(attachment)
# 设置邮件主题、发件人、收件人
msg['Subject'] = '邮件主题'
msg['From'] = sender
msg['To'] = '接收者邮箱地址'
```
5. 发送邮件
```python
smtp_obj.sendmail(sender, '接收者邮箱地址', msg.as_string())
smtp_obj.quit()
```
完整代码示例:
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
smtp_server = 'smtp.邮箱服务商.com' # 邮箱服务商的SMTP服务器地址
smtp_port = 587 # SMTP服务器的端口号
sender = '发送者邮箱地址'
password = '发送者邮箱密码'
smtp_obj = smtplib.SMTP(smtp_server, smtp_port)
smtp_obj.starttls() # 开启TLS加密
smtp_obj.login(sender, password)
msg = MIMEMultipart()
body = MIMEText('邮件正文')
msg.attach(body)
with open('文件路径', 'rb') as f:
attachment = MIMEApplication(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename='文件名')
msg.attach(attachment)
msg['Subject'] = '邮件主题'
msg['From'] = sender
msg['To'] = '接收者邮箱地址'
smtp_obj.sendmail(sender, '接收者邮箱地址', msg.as_string())
smtp_obj.quit()
```
注意替换代码中的`smtp_server`、`smtp_port`、`sender`、`password`、`文件路径`、`文件名`、`邮件主题`、`邮件正文`、`接收者邮箱地址`等参数为你自己的信息。