文件加密解密)通过对文件中的每比特加5来给文件加密。编写程序提示用户输入一个输入文件名(如138.txt)和一个输出文件名(139.txt)并将加密后的输入文件存储到输出文件中。然后,输入待解密的文件,解密后输出。python
时间: 2024-02-28 11:52:56 浏览: 126
好的,下面是 Python 的代码实现:
加密文件:
```python
def encrypt_file(input_file_name, output_file_name):
with open(input_file_name, 'rb') as input_file:
with open(output_file_name, 'wb') as output_file:
while True:
byte = input_file.read(1)
if not byte:
break
encrypted_byte = (byte[0] + 5) % 256
output_file.write(bytes([encrypted_byte]))
print("加密成功!")
input_file_name = input("请输入要加密的文件名:")
output_file_name = input("请输入加密后保存的文件名:")
encrypt_file(input_file_name, output_file_name)
```
解密文件:
```python
def decrypt_file(input_file_name, output_file_name):
with open(input_file_name, 'rb') as input_file:
with open(output_file_name, 'wb') as output_file:
while True:
byte = input_file.read(1)
if not byte:
break
decrypted_byte = (byte[0] - 5) % 256
output_file.write(bytes([decrypted_byte]))
print("解密成功!")
input_file_name = input("请输入要解密的文件名:")
output_file_name = input("请输入解密后保存的文件名:")
decrypt_file(input_file_name, output_file_name)
```
以上代码中,我们使用了 Python 的文件操作和位运算,通过加5和减5的操作完成了文件的加密和解密。
阅读全文
相关推荐















