'''
该程序来自和女朋友吵架后(不想谈了 准前女友)
说一下原理:我们都知道一个字节的取值范围是0-255 也就是256个基本单位构成了文本、图片、视频等
总而言之:256把锁对应256把钥匙
'''
import re
# 设置钥匙和锁 字典格式
lockAndKey = {
自己设置钥匙和锁}
# 加密
def encryption(path):
encryptionBeforBin = b'' # 操作之前的二进制
encryptionAfterBin = b'' # 操作之后的二进制
global lockAndKey # 指向外部字典
# 读取文件
with open(path, 'rb') as file:
encryptionBeforBin = file.read() # 获得文件的二进制文件
# 循环遍历二进制
for i in encryptionBeforBin:
encryptionAfterBin = encryptionAfterBin + bytes([lockAndKey[i]]) # 加密
print('已经生成加密信息')
# 重写文件
with open(path, 'wb') as file:
file.write(encryptionAfterBin)
print("\033[37;41m Done!\033[0m")
# 解密
def decode(path):
encryptionBeforBin = b'' # 操作之前的二进制
encryptionAfterBin = b'' # 操作之后的二进制
global lockAndKey # 指向外部字典
# 颠倒锁和钥匙
lockAndKey = {
value: key for key, value in lockAndKey.items()}
# 读取文件
with open(path, 'rb') as file:
encryptionBeforBin = file.read() # 获得文件的二进制文件
# 循环遍历二进制
for i in encryptionBeforBin:
encryptionAfterBin = encryptionAfterBin + bytes([lockAndKey[i]]) # 解密
print('已经生成解密信息')
# 重写文件
with open(path, 'wb') as file:
file.write(encryptionAfterBin)
print("\033[37;41m Done!\033[0m")
if __name__ == '__main__':
content = '''
******************************
* 该加密适用于小文件 *
* *
* 加密请输入0 *
* 解密请输入1 *
************************************
'''
print(content)
choose = input('输入你的选择:')
if not re.fullmatch('[0-1]', choose):
print("\033[37;41m 不要皮 \033[0m")
exit(1)
path = input('输入文件路径:')
if choose.__eq__('0'):
encryption(path)
else:
decode(path)
python 文件加密
猜你喜欢
转载自blog.csdn.net/Mr_Qian_Ives/article/details/108105157
今日推荐
周排行