CTF中基本的Xor解密操作

先解密Base64,然後進行Xor的檢測及解密

#!/usr/bin/python3
# -*- coding: utf-8 -*- 
# --author:valecalida--
# 異或運算僅允許數字之間的運算,不允許其他類型之間的運算

from base64 import b64decode as b64d
message = input("請輸入您想要進行操作的字符串 >>>")
if message[0:2] == "b\'":
    message = message[2:-1]
#print(message)
flags = input("請輸入解碼的樣式(例:flag、ctfhub) >>>")


def b64_detect(msg):
    try:
        cipher_text = b64d(msg)
    except BaseException as e:
        print("您輸入的值好像不能使用Base64解密,請再嘗試別的方法")
    else:
        res = []
        for i in range(len(flags)):
            res.append(cipher_text[i] ^ ord(flags[i]))
    finally:
        return res, cipher_text


def decode_xor():
    result = ''
    res, cipher_text =b64_detect(message)
    if res[0] - res[1] == 0:
        print("這是一個值不變的Xor運算")
        for i in range(len(cipher_text)):
            result += chr(res[0] ^ cipher_text[i])
        return result
    elif res[0] - res[1] == 1:
        print("這是一個值遞減的Xor運算")
        for i in range(len(cipher_text)):
            result += chr((res[0] - i) ^ cipher_text[i])
        return result
    elif res[0] - res[1] == -1:
        print("這是一個值遞增的Xor運算")
        for i in range(len(cipher_text)):
            result += chr((res[0] + i) ^ cipher_text[i])
        return result
    else:
        print("這好像不是Xor運算,再試試別的吧")
        return result


print("\t程序返回的結果是 >>", decode_xor())


運行結果如下:

這是一個接觸到的OTP類型的題目,但是我感覺好像沒考到這個知識點(黑人問號臉???),爲了避免侵權,這裏打上馬賽克(侵權請聯繫我)

由於題目已經給出了hint,所以這裏直接用就行了

#!/usr/bin/python2
# -*- coding: utf-8 -*-
import binascii
c1 = '24161a1d1************************20c03170e'
c2 = '380e****************************120100071c'
c3 = '2511************************000302581c1d15'
c4 = '1************************1a**b01460c07175d'
c5 = '24161a1***********************06120c03170e'
c6 = '380e**************************0e120100071c'
c7 = '270********************************606011a'
c8 = '27091*****************************f60a0108'
c9 = '24090************************0030f0c1b1e18' # 這是密碼
c10 = '24161**********************13030a0c071713'
c11 = '2409*****************************161b1a18'
c12 = '24091************************1d1211010b0e'
c13 = '330e06************************60510181304'
ciphers = [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11,c12,c13]
cipher_text = "Th*************************ht"


def sxor(s1,s2):
    return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(s1, s2))


for cipher in ciphers:
    k = sxor(cipher.decode('hex'),cipher_text)
    print(binascii.a2b_hex(k.encode('hex')))

注意這裏用的是python2

運行結果

這裏直接得到了密碼,所以我也不知道它到底有沒有考到這個知識點,但是個人感覺沒有,給大家看着玩吧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章