在我们日常CTF中,经常会遇到许多加密文件,要用base64来解密,但单行解密太费时间,因此我们可以写一个python小脚本来加速它的解密过程,以下就是一个解密或加密的一个python脚本代码示例
import base64
base_a = input ("请输入要解密/加密的文件:")
base_b = inpit ("请输入解密/加密文件的根目录:")
SaveList = [] # 存档列表
# 读取文本内容到列表
with open(base_a, "r", encoding='utf-8') as file:
for line in file:
line = line.strip('\n') # 删除换行符
SaveList.append(line)
file.close()
print("请选择加密(E)还是解密(D):")
type=input()
if(type=='E'):
print("请选择加密次数:\n")
index = input()
count = eval(index)
while(count>0):
i = len(SaveList)
while(i-1>=0):
encode=base64.b64encode(SaveList[i-1].encode('utf-8'))
SaveList[i-1]=str(encode,'utf-8')
i=i-1
count = count - 1
# 写入存档到文件
with open(base_b, "a", encoding='utf-8') as file:
for i in SaveList:
file.write(i + '\n')
file.close()
else:
print("请选择解密次数:")
index = input()
count=eval(index)
while (count > 0):
i = len(SaveList)
while (i-1>=0):
decode = base64.b64decode(SaveList[i-1])
print(decode)
SaveList[i-1] = str(decode, 'utf-8')
i = i - 1
count=count-1
# 写入存档到文件
with open(base_b, "w", encoding='utf-8') as file:
for i in SaveList:
file.write(i + '\n')
file.close()