# -*- coding: utf-8 -*- import base64 flag = 'flag{Base64isF4n}'#flag bin_str = ''.join([bin(ord(c)).replace('0b', '').zfill(8) for c in flag]) base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' withopen('0.txt', 'rb') as f0, open('1.txt', 'wb') as f1: #'0.txt'是明文, '1.txt'用于存放隐写后的 base64 for line in f0.readlines(): rowstr = base64.b64encode(line.replace('\n', '')) equalnum = rowstr.count('=') if equalnum andlen(bin_str): offset = int('0b'+bin_str[:equalnum * 2], 2) char = rowstr[len(rowstr) - equalnum - 1] rowstr = rowstr.replace(char, base64chars[base64chars.index(char) + offset]) bin_str = bin_str[equalnum*2:] f1.write(rowstr + '\n')
解密
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
d='''str ''' e=d.splitlines() binstr="" base64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" for i in e : if i.find("==")>0: temp=bin((base64.find(i[-3])&15))[2:] #取倒数第3个字符,在base64找到对应的索引数(就是编码数),取低4位,再转换为二进制字符 binstr=binstr + "0"*(4-len(temp))+temp #二进制字符补高位0后,连接字符到binstr elif i.find("=")>0: temp=bin((base64.find(i[-2])&3))[2:] #取倒数第2个字符,在base64找到对应的索引数(就是编码数),取低2位,再转换为二进制字符 binstr=binstr + "0"*(2-len(temp))+temp #二进制字符补高位0后,连接字符到binstr str="" for i inrange(0,len(binstr),8): str=str+chr(int(binstr[i:i+8],2)) #从左到右,每取8位转换为ascii字符,连接字符到字符串 print(str)