2021-10-15

2021/10/15 23:44:41

本文主要是介绍2021-10-15,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

sm

from Crypto.Util.number import getPrime,long_to_bytes,bytes_to_long
from Crypto.Cipher import AES
import hashlib
from random import randint
def gen512num():
    order=[]
    while len(order)!=512:
        tmp=randint(1,512)
        if tmp not in order:
            order.append(tmp)
    ps=[]
    for i in range(512):
        p=getPrime(512-order[i]+10)
        pre=bin(p)[2:][0:(512-order[i])]+"1"
        ps.append(int(pre+"0"*(512-len(pre)),2))
    return ps

def run():
    choose=getPrime(512)
    ps=gen512num()
    print "gen over"
    bchoose=bin(choose)[2:]
    r=0
    bchoose = "0"*(512-len(bchoose))+bchoose
    for i in range(512):
        if bchoose[i]=='1':
            r=r^ps[i]
    flag=open("flag","r").read()

    key=long_to_bytes(int(hashlib.md5(long_to_bytes(choose)).hexdigest(),16))
    aes_obj = AES.new(key, AES.MODE_ECB)
    ef=aes_obj.encrypt(flag).encode("base64")

    open("r", "w").write(str(r))
    open("ef","w").write(ef)
    gg=""
    for p in ps:
        gg+=str(p)+"\n"
    open("ps","w").write(gg)

run()

这是一道有关矩阵的题目
题目的关键在于求出bchoose,而bchoose的比特位用来指定ps中特定的数的位进行模2加运算(也就是异或)
所以有 :
ps[i]⊕ps[j]⊕ps[k]…=r
从比特位观察我们可以得到:
r的每一位比特位对应ps中对应的比特位进行异或运算的结果,而究竟是哪些ps值则由bchoose来决定.
因此我们可以写成矩阵形式:
记ps构成的矩阵为 A 待求的bchoose 为 C r为B 则:
C*A=B => C=BA-1
bchoose 求出后答案也就出来了.
参考:https://blog.csdn.net/weixin_44110537/article/details/107941247
先用sage

#sage代码
ps=open('ps','r').readlines()
c=[]
for i in ps:
    c.append(eval(i.strip()))
A=[]
for i in c:
    A.append([int(x) for x in bin(i)[2:].zfill(512)])
r=eval(open('r','r').readline())
B=[int(x) for x in bin(r)[2:].zfill(512)]
A=matrix(GF(2),A)
#print(A.inverse())
B=matrix(GF(2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1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1]
strkey='0b'
for i in key:
    strkey+=str(i)
from Crypto.Util import number
x=number.long_to_bytes(eval(strkey))
import hashlib
x=hashlib.md5(x).hexdigest()
x=number.long_to_bytes(eval('0x'+x))
from Crypto.Cipher import AES
aes=AES.new(x,AES.MODE_ECB)
flag_enc='5eFo3ANg2fu9LRrFktWCJmVvx6RgBFzd0R8GXQ8JD78='
import base64
flag_enc=base64.b64decode(flag_enc)
flag=aes.decrypt(flag_enc)
print(flag)

#flag{shemir_alotof_in_wctf_fun!}


这篇关于2021-10-15的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程