base64隐写解密代码

'''
base64隐写解密
'''

base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

flag=''
with open('en.txt','r') as f:
    for line in f.readlines():
        line=line[:-1]
        num=line.count('=')
        if num == 0 :
            continue
        lastchar = line[-(num+1)]
        
        #print(line,num,lastchar)
        myindex = base64chars.index(lastchar)
        #print(myindex)
        bin_str = bin(myindex)[2:].zfill(6)
        #print(bin_str)
        flag+=bin_str[6-2*num:]
        #print(bin_str[6-2*num:])
print(''.join([chr(int(flag[i:i + 8], 2)) for i in range(0, len(flag), 8)]))
        
        
        
        
            
        
'''
base64隐写加密

'''
import base64
flag = 'flag{1_am_fl0g}'
bin_str = ''.join([bin(ord(c)).replace('0b','').zfill(8) for c in flag])
#print(bin_str)
base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

with open('Plaintext.txt','r') as f0 , open('en.txt','w') as f1:
    for line in f0.readline():
        line = line.replace('\n','')
        #base64加密
        b64_str = str(base64.b64encode(line.encode('utf-8')))[2:-1]
        #print(b64_str)
        #num是0的个数
        num = b64_str.count('=')
        if num == 0:
            continue
        #隐写的地方,也就是需要改变的字符串
        lastchar = b64_str[-(num+1)]
        #其原来的索引
        myindex = base64chars.index(lastchar)
        bin_tmp = bin(myindex)[2:].zfill(6)
        #更新填充索引
        en_str = bin_tmp[:6-2*num]+bin_str[:num*2]
        #更新flag
        bin_str=bin_str[num*2:]
        #更新字符
        ch = base64chars[int(en_str,2)]
        #更新base64
        b64_str=b64_str[:-(num+1)]+ch+b64_str[-(num):]
        #写入
        f1.write(b64_str+'\n')

你可能感兴趣的:(CTF,base64隐写)