2020蓝帽杯线上预选赛_熟悉的解密

题目下载 : 1

base64 解码得到 python 代码


#-*- coding: utf-8 -*-
import sys
from ctypes import *
def encipher(v, k):
    y = c_uint32(v[0])
    z = c_uint32(v[1])
    sum = c_uint32(0)
    delta = 0x9e3779b9
    n = 32
    w = [0,0]
    while(n>0):
        sum.value += delta
        y.value += ( z.value << 4 ) + k[0] ^ z.value + sum.value ^ ( z.value >> 5 ) + k[1]
        z.value += ( y.value << 4 ) + k[2] ^ y.value + sum.value ^ ( y.value >> 5 ) + k[3]
        n -= 1
    w[0] = y.value
    w[1] = z.value
    return w
def encodestr(text, key):
    cipherList = []
    text += (8 - len(text) % 8) * chr(0)
    for i in range(len(text)/8):
        v1 = 0
        v2 = 0
        for j in range(4):
            v1+= ord(text[i*8+j]) << (4-j-1)*8
            v2+= ord(text[i*8+j+4]) << (4-j-1)*8
        cipherList.append(encipher([v1,v2],key))
    return cipherList

if __name__ == "__main__":
    key = [11,22,33,44]
    flag = ?
    cipher = encodestr(flag,key)
    #cipher = [[4018289233L, 2950320151L], [1771827478L, 493980876L], [1863284879L, 1137797599L], [2759701525L, 3957885055L], [2600866805L, 78850724L]]

看了别人的 wp 知道这个是 tea 加密算法

#include <bits/stdc++.h>
using namespace std;
int List[5][2]= {{1718378855,2067085111},
    {859137328,1663907428},{808594737,828727597},
    {942683954,758133808},{1694498816,0}};
int main() {
    for (int i=0;i<5;i++) {
        int now=List[i][0];
        for (int j=0;j<4;j++) {
            printf("%c",now>>(8*(3-j)));
            now-=(now>>(8*(3-j)))<<(8*(3-j));
        }
        now=List[i][1];
        for (int j=0;j<4;j++) {
            printf("%c",now>>(8*(3-j)));
            now-=(now>>(8*(3-j)))<<(8*(3-j));
        }
    }

    return 0;
}

解出一部分 flag{57735e0c-6d02-11ea-8072-040e

剩下一部分是 base64 隐写

脚本如下

import base64
def get_base64_diff_value(s1,s2):
    table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
    res = 0
    for i in range(len(s1)):
        if s1[i] != s2[i]:
            return abs(table.index(s1[i]) - table.index(s2[i]))
    return res

def solve():
    lines = open('1.txt','r').readlines()
    bin_str = ''

    for line in lines:
        steg_line = line.replace('\n','')
        # print(steg_line)
        norm_line = base64.b64encode(base64.b64decode(steg_line)).decode()
        # print(norm_line)
        diff = get_base64_diff_value(steg_line,norm_line)
        # print(diff)
        pad_num = steg_line.count('=')
        if diff:
            bin_str += bin(diff)[2:].zfill(pad_num*2)
        else:
            bin_str += '0' * pad_num * 2
    print(bin_str)
    res_str = ''
    for j in range(int(len(bin_str)/8)):
        # print(8*j,(j+1)*8)
        res_str+=chr(int(bin_str[8*j:(j+1)*8],2))
    print(res_str[-52:])
    print(base64.b64decode(res_str[-52:]))

solve()

得出另外一部分 flag : 3c032fa7}

flag{57735e0c-6d02-11ea-8072-040e3c032fa7}