1. 程式人生 > 實用技巧 >攻防世界-密碼學-Decode_The_File

攻防世界-密碼學-Decode_The_File

1. 題目資訊

附件是一個文字檔案,裡面有658行base64編碼。

2. 分析

先將附件中每行base64編碼進行解碼,得到一個Python指令碼。但其實資訊並非隱藏在解碼後的資料中,而是隱藏在編碼中。首先介紹base64編碼的原理,當需要編碼的資料剩下1或2位元組時,編碼與解碼過程如下:

可以看到,即使\(a_{i} \neq 0\),解碼過程也能正確進行;從而可以使用\(a_{i}\)傳遞一些資訊。因此,只要我們將編碼中的\(a_{i}\)提取出來,就可以解出flag。

3. 解題

實現的Python指令碼如下:

from base64 import b64decode
from string import uppercase,lowercase,digits
from Crypto.Util.number import long_to_bytes

def solve():
    with open('encode','r') as f:
        codes=f.read()
    Lc=codes.split('\n')[:-1]
    base=uppercase+lowercase+digits+'+/'
    re2=[]
    for code in Lc:
        if '==' in code:
            re2.append(bin(base.find(code[-3]))[2:].rjust(6,'0')[2:])
        elif '=' in code:
            re2.append(bin(base.find(code[-2]))[2:].rjust(6,'0')[4:])
    ret=''.join(re2)
    return long_to_bytes(long(ret[:ret.rfind('1')+1],2))

if __name__=='__main__':
    print solve()

程式執行結果如下:

$ python solve.py
ROIS{base_GA_caN_b3_d1ffeR3nT}