用脚本为文本文件内容加密
时间: 2024-03-20 14:40:22 浏览: 16
您可以使用Python语言编写脚本来为文本文件内容进行加密。一种简单的方法是使用Python内置的base64模块。下面是一个示例脚本:
```python
import base64
# 读取文件内容
with open('filename.txt', 'r') as f:
plaintext = f.read().encode('utf-8')
# 加密文件内容
ciphertext = base64.b64encode(plaintext)
# 将加密后的内容写入文件
with open('filename_encrypted.txt', 'wb') as f:
f.write(ciphertext)
```
在上述示例中,首先使用`open()`函数读取待加密的文本文件内容,然后将内容编码为UTF-8格式并保存在`plaintext`变量中。接下来,使用`base64.b64encode()`函数对`plaintext`进行加密,得到加密后的内容`ciphertext`。最后,使用`open()`函数创建一个新的文件,将加密后的内容写入其中。
注意:该方法只提供基础的加密功能,不具备强大的加密保护能力。如果您需要更高级的加密保护,请使用专业的加密算法。
相关问题
写一个脚本,使用指定秘钥为文本文件内容加密和解密
您可以使用Python语言编写脚本来使用指定秘钥为文本文件内容进行加密和解密。下面是一个示例脚本:
```python
import cryptography.fernet
import argparse
import os
# 定义命令行参数
parser = argparse.ArgumentParser(description='Encrypt/Decrypt a file with a given key.')
parser.add_argument('filename', help='Name of the file to encrypt/decrypt.')
parser.add_argument('key', help='Encryption/Decryption key.')
# 解析命令行参数
args = parser.parse_args()
# 获取文件名和密钥
filename = args.filename
key = args.key.encode()
# 随机生成加密密钥
cipher_suite = cryptography.fernet.Fernet(key)
# 判断文件是否存在
if not os.path.isfile(filename):
print(f"Error: {filename} does not exist.")
exit()
# 加密文件内容
with open(filename, 'rb') as f:
plaintext = f.read()
ciphertext = cipher_suite.encrypt(plaintext)
# 将加密后的内容写入文件
with open(filename + '.enc', 'wb') as f:
f.write(ciphertext)
print(f"{filename} encrypted successfully as {filename}.enc.")
# 解密文件内容
with open(filename + '.enc', 'rb') as f:
ciphertext = f.read()
plaintext = cipher_suite.decrypt(ciphertext)
# 将解密后的内容写入文件
with open(filename + '.decrypted', 'wb') as f:
f.write(plaintext)
print(f"{filename}.enc decrypted successfully as {filename}.decrypted.")
```
在上述示例中,使用`argparse`模块定义了两个命令行参数:`filename`和`key`。`filename`参数表示要加密/解密的文件名,`key`参数表示加密/解密使用的密钥。然后使用`cryptography.fernet.Fernet()`函数生成一个加密密钥,将密钥保存在变量`cipher_suite`中。接下来,判断文件是否存在,如果文件不存在,输出错误信息并退出程序。然后,使用`cipher_suite.encrypt()`函数对文件内容进行加密得到密文,使用`open()`函数创建一个新的文件,将加密后的内容写入其中。最后,使用`cipher_suite.decrypt()`函数解密文件内容得到明文,并将明文写入新的文件中。
注意:该示例脚本使用`cryptography`库中的Fernet加密算法进行加密和解密,具有较高的安全性和保密性。但是请注意保护好密钥,不要将密钥泄露给他人。
写两个脚本,用来使用指定秘钥为文本文件内容加密和解密,使用AES
好的,下面是使用AES加密和解密文本文件内容的Python脚本。请在使用前先安装 pyaes 库。
加密脚本:
```python
import pyaes
# 加密函数
def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
if not out_filename:
out_filename = in_filename + '.enc'
iv = pyaes.AESModeOfOperationCTR.generate_nonce()
encryptor = pyaes.AESModeOfOperationCTR(key, iv)
filesize = os.path.getsize(in_filename)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(iv)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
outfile.write(encryptor.encrypt(chunk))
# 示例
key = b'This_is_a_secret_key'
in_filename = 'test.txt'
encrypt_file(key, in_filename)
```
解密脚本:
```python
import pyaes
# 解密函数
def decrypt_file(key, in_filename, out_filename=None, chunksize=24*1024):
if not out_filename:
out_filename = os.path.splitext(in_filename)[0]
with open(in_filename, 'rb') as infile:
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
iv = infile.read(16)
decryptor = pyaes.AESModeOfOperationCTR(key, iv)
with open(out_filename, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
outfile.write(decryptor.decrypt(chunk))
outfile.truncate(origsize)
# 示例
key = b'This_is_a_secret_key'
in_filename = 'test.txt.enc'
decrypt_file(key, in_filename)
```
这两个脚本分别用于加密和解密指定的文本文件,使用AES算法,并使用指定的秘钥进行加密和解密。注意,这两个脚本必须在同一目录下使用。