import hashlib import random from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad import os from my_own_flag import flag
defMT_19937(num,en_c): seed1 = os.urandom(16) random.seed(seed1) number = [] for i inrange(num): number.append(random.getrandbits(32)) cal = 0 for i inrange(num,num+en_c): cal += random.getrandbits(32) return number,cal
defencrypt(cal,flag): key = hashlib.sha256(str(cal).encode()).digest() A = AES.new(key, AES.MODE_ECB) c = A.encrypt(pad(flag,16)) return c
defmain(): LEN = len(flag) m1,m2 = flag[:LEN//2],flag[LEN//2:]
####################################################################################### part1 rc1 = RandCrack() for i inrange(624): rc1.submit(t1[i]) cal = 0 for i inrange(624): cal += rc1.predict_getrandbits(32) key = hashlib.sha256(str(cal).encode()).digest() A = AES.new(key, AES.MODE_ECB) flag1 = A.decrypt(c1) print(flag1) #SHCTF{TH1s_1s_YoU5
####################################################################################### part1 rc2 = RandCrack() num1 = 553 for i inrange(num1): rc2.submit(t2[i]) for i inrange(624-num1): rc2.submit(getrandbits(32)) cal = 0 for i inrange(624//4): cal += rc2.predict_getrandbits(32) key = hashlib.sha256(str(cal).encode()).digest() A = AES.new(key, AES.MODE_ECB) flag2 = A.decrypt(c2) print(flag2)
#SHCTF{TH1s_1s_YoU5_5TART_WAY_0F_CTF}
[Week4] baby_rsa
题目描述:
1
只有中间未知?
题目:
1 2 3 4 5 6 7
#https://github.com/jvdsn/crypto-attacks n = 149172698687247343307484774427463947040435385939538317995577802933708356659744781308849658149199463270402946054959026247011496643609722381036883462993606208405454448793748282856217226973570288117498818638210423816294135228225752144034736417495450129714250843040389723696691326017062575682989124677170212774709 e = 117932126002671581139669626170313849654365346787524775666511151162210096339679521576248537514813055641658722582914817481701142826861992970974206985137736311670025047752207632786439134855261541672012123572997654885689727972923659090161642085293034838535696206768459211817851404605357080649176502772728128885161 c = 5560665954852260703690321742771294743847646190564920056638605621636133720600072404637746086157764356927591996611862975162275415163691292729424412545560091018172812509230401361899309377868998693154480684535377865697939714965280441927137203589475324582174585416573174423912557361267766810988676863548944796515 dm = 0x2498aa4c85de5a33d5766f28d879f0df7175f43dd71cd4ab56ab67bf76334e6e3dcb dl = 0x4c21c14305c34ed8f5e8879452c4ce569ce0789e6b39 d_zj=???
from Crypto.Util.number import * from tqdm import * import itertools
#coppersmith defsmall_roots(f, bounds, m=1, d=None): ifnot d: d = f.degree() R = f.base_ring() N = R.cardinality() f /= f.coefficients().pop(0) f = f.change_ring(ZZ) G = Sequence([], f.parent()) for i inrange(m + 1): base = N ^ (m - i) * f ^ i for shifts in itertools.product(range(d), repeat=f.nvariables()): g = base * prod(map(power, f.variables(), shifts)) G.append(g) B, monomials = G.coefficients_monomials() monomials = vector(monomials) factors = [monomial(*bounds) for monomial in monomials] for i, factor inenumerate(factors): B.rescale_col(i, factor) B = B.dense_matrix().LLL() B = B.change_ring(QQ) for i, factor inenumerate(factors): B.rescale_col(i, 1 / factor) H = Sequence([], f.parent().change_ring(QQ)) for h infilter(None, B * monomials): H.append(h) I = H.ideal() if I.dimension() == -1: H.pop() elif I.dimension() == 0: roots = [] for root in I.variety(ring=ZZ): root = tuple(R(root[var]) for var in f.variables()) roots.append(root) return roots return []
n = 149172698687247343307484774427463947040435385939538317995577802933708356659744781308849658149199463270402946054959026247011496643609722381036883462993606208405454448793748282856217226973570288117498818638210423816294135228225752144034736417495450129714250843040389723696691326017062575682989124677170212774709 e = 117932126002671581139669626170313849654365346787524775666511151162210096339679521576248537514813055641658722582914817481701142826861992970974206985137736311670025047752207632786439134855261541672012123572997654885689727972923659090161642085293034838535696206768459211817851404605357080649176502772728128885161 c = 5560665954852260703690321742771294743847646190564920056638605621636133720600072404637746086157764356927591996611862975162275415163691292729424412545560091018172812509230401361899309377868998693154480684535377865697939714965280441927137203589475324582174585416573174423912557361267766810988676863548944796515 dm = 0x2498aa4c85de5a33d5766f28d879f0df7175f43dd71cd4ab56ab67bf76334e6e3dcb dl = 0x4c21c14305c34ed8f5e8879452c4ce569ce0789e6b39
from Crypto.Cipher import AES from Crypto.Util.Padding import pad import os
FLAG = b"SHCTF{XXX_FAKE_FLAG_XXX}" p = 334641907675981737343904379204876337859127829299172648068105540032137951559908027120450949854596026146898543 G = [random_matrix(GF(p), 2) for _ inrange(64)] I = identity_matrix(GF(p), 2) save(G, "G.sobj") key = os.urandom(8)
H = lambda m: prod([G[i%64] ifint(j) else I for i,j inenumerate(bin(int(m.hex(), 16))[2:])]) Q = list(H(key)) c = AES.new(2*key, AES.MODE_ECB).encrypt(pad(FLAG,16)).hex() print(f"{Q = }") print(f"{c = }") """ Q = [(92408373140638310582912266568541040708090711689280871505631689622417484347016487049244869849344848494009962, 53959869712387349430336059834241967356744173550876450413296700728311848545577500067458604734684838108665050), (252347024205859090718692136370078190718071419535216876332667850755617010322625175614169994287981074023442001, 248109129148524862390611680382928105844063942809716627922076622327580907465285046951446750474905265881834033)] c = 'bbf4e7820865cc2fa3739a1d86006d83015180776a3285d4c14f5ee95685ac1ef64122e0f3603a794b4f170ec827dbb1' """
G = load("G.sobj") p = 334641907675981737343904379204876337859127829299172648068105540032137951559908027120450949854596026146898543 F = GF(p) I = identity_matrix(F, 2)
################################################################################ dlp Gr = [] g = F(pow(5,r,p)) for i inrange(64): Gr.append(discrete_log(G[i].det()^r,g))
c = discrete_log(Q.det()^r,g)
1 2 3 4 5 6 7 8 9 10 11
from Crypto.Util.number import *
K = (p-1)//r L = block_matrix(ZZ,[ [1,(Matrix(ZZ,Gr).T).stack(vector(ZZ,[-c]))], [0,K] ]) L[:,-1:] *= 2^10 res = L.BKZ()[0][:64]
c = 'bbf4e7820865cc2fa3739a1d86006d83015180776a3285d4c14f5ee95685ac1ef64122e0f3603a794b4f170ec827dbb1' flag = AES.new(2*key, AES.MODE_ECB).decrypt(bytes.fromhex(c))
print(flag)
#SHCTF{master_of_the_linear_algebra}
[Week4] BabyHash2
题目描述:
1
Magic Hash : )
题目:
1 2 3 4 5 6 7 8 9 10
FLAG = "SHCTF{XXX_FAKE_FLAG_XXX}" p = 1167195242552699154956050457 A = matrix(Zmod(p), [[1, 1], [0, 1]]) B = matrix(Zmod(p), [[1, 0], [1, 1]])
H = lambda m: prod([A ifint(i) else B for i inbin(int(m.hex(), 16))[2:]]) msg = bytes.fromhex(input("msg > ")) assert msg != b"$ Welcome to SHCTF!!! :)" if H(msg) == H(b"$ Welcome to SHCTF!!! :)") andlen(msg) < 100: print("Congrats", FLAG)
本题先给出模p下的两个矩阵:
定义一个消息m的哈希函数为:
1
H = lambda m: prod([A ifint(i) else B for i inbin(int(m.hex(), 16))[2:]])
from Crypto.Util.number import * from gmpy2 import next_prime, invert, powmod from random import getrandbits, randint
defEuclid(a,b): t = [] while(1): t.append(a // b) a, b = b, a%b if(b == 0): return t p = 1167195242552699154956050457 F = GF(p) A = matrix(F, [[1, 1], [0, 1]]) B = matrix(F, [[1, 0], [1, 1]]) I = identity_matrix(F,2)