在做CTF时遇到一个全是Base64编码的txt文件,一个一个解码太慢了,就自己写了个脚本,按行读取并解码后输出到文件中,并且可以多次解码。
下面是代码:
#-*- coding : utf-8 -*-
import base64
SaveList = [] # 存档列表
# 读取文本内容到列表
with open("1.txt", "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("result.txt", "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("result.txt", "w", encoding='utf-8') as file:
for i in SaveList:
file.write(i + '\n')
file.close()