from Crypto.Util.number import * from random import getrandbits from secret import flag
defgetN(): N = 1 for i inrange(16): tmp = getPrime(32) N *= tmp return N
mask = getrandbits(256) e = 65537 n = getN() m = bytes_to_long(flag) c = pow(m*mask,e,n) print(f'n = {n}') print(f'e = {e}') print(f'c = {c}') print(f'mask = {mask}')
''' n = 93099494899964317992000886585964221136368777219322402558083737546844067074234332564205970300159140111778084916162471993849233358306940868232157447540597 e = 65537 c = 54352122428332145724828674757308827564883974087400720449151348825082737474080849774814293027988784740602148317713402758353653028988960687525211635107801 mask = 54257528450885974256117108479579183871895740052660152544049844968621224899247 '''
mask = 54257528450885974256117108479579183871895740052660152544049844968621224899247 phi = 1 for i in pq: phi*=(i-1) d = gmpy2.invert(e, phi) m1 = pow(c, d, n) flag = long_to_bytes(m1//mask) print(flag) #b'0xGame{Magic_M@th_Make_Crypt0}'
Vigenere
密文
0dGmqk{79ap4i0522g0a67m6i196he52357q60f}
根据flag头是0xGame,反推出密钥是game,最后找一个在线网站解密即可得到flag。
密码,觅码,先有*再密
源码
from secret import flag #从中导入秘密的flag,这是我们要破解的信息 from Crypto.Util.number import bytes_to_long #从函数库导入一些编码函数 from base64 import b64encode
#hint:也许下列函数库会对你有些帮助,但是要怎么用呢…… from base64 import b64decode from gmpy2 import iroot from Crypto.Util.number import long_to_bytes
flag = flag.encode() lent = len(flag) flag = [flag[i*(lent//4):(i+1)*(lent//4)] for i inrange(4)]#将flag切割成四份
c1 = bytes_to_long(flag[0]) c2 = ''.join([str(bin(i))[2:] for i in flag[1]]) c3 = b64encode(flag[2]) c4 = flag[3].hex() print(f'c1?= {pow(c1,5)}\nc2 = {c2}\nc3 = {c3}\nc4 = {c4}')
# -*- coding: utf-8 -*- from base64 import b64decode from gmpy2 import iroot from Crypto.Util.number import long_to_bytes c1= 2607076237872456265701394408859286660368327415582106508683648834772020887801353062171214554351749058553609022833985773083200356284531601339221590756213276590896143894954053902973407638214851164171968630602313844022016135428560081844499356672695981757804756591891049233334352061975924028218309004551 c2 = "10010000100001101110100010100111101000111110010010111010100001101110010010111111101000011110011010000001101011111110011010011000101011111110010110100110100000101110010010111101100101011110011110111100" c3 = b'lueggeeahO+8jOmCo+S5iOW8gOWni+aIkQ==' c4 = "e4bbace79a8443727970746fe68c91e68898e590a72121217d"
defbin_to_hex(c): m = len(c)//8 flag="" for i inrange(m): str1=c[i*8:i*8+8] a = int(str1,2) flag+=hex(a)[2:] #print(i,hex(a)[2:]) #print(i,str1) return flag
defpad(text): iflen(text)%8: return text else: pad = 8-(len(text)%8) text += pad.to_bytes(1,'big')*pad return text
defEncrypt_CBC(text,iv,key): result = b'' text = pad(text) block=[text[_*8:(_+1)*8] for _ inrange(len(text)//8)] for i in block: tmp = bytes_xor(iv,i) iv = encrypt(tmp,key) result += iv return result
defencrypt(text,key): result = b'' for i in text: result += ((i^key)).to_bytes(1,'big') return result
defbytes_xor(a,b): a,b=bytes_to_long(a),bytes_to_long(b) return long_to_bytes(a^b) defdecrypt(text,key): result = b'' for i in text: result += ((i^key)).to_bytes(1,'big') return result
defdecrypt_CBC(text,iv,key): result = b'' #text = pad(text) block=[text[_*8:(_+1)*8] for _ inrange(len(text)//8)] print(block) for i in block: iv1 = decrypt(i,key) tmp = bytes_xor(iv,iv1) result += tmp iv=i return result
iv = b'11111111' key=143 enc = b"\x8e\xc6\xf9\xdf\xd3\xdb\xc5\x8e8q\x10f>7.5\x81\xcc\xae\x8d\x82\x8f\x92\xd9o'D6h8.d\xd6\x9a\xfc\xdb\xd3\xd1\x97\x96Q\x1d{\\TV\x10\x11" print(len(enc)) flag = decrypt_CBC(enc,iv,key) print(flag) #b'0xGame{098f6bcd4621d373cade4e832627b4f6}\x08\x08\x08\x08\x08\x08\x08\x08'
猜谜
源码
from secret import flag,key from Crypto.Util.number import *
defdec(text): text = text.decode() code = 'AP3IXYxn4DmwqOlT0Q/JbKFecN8isvE6gWrto+yf7M5d2pjBuk1Hh9aCRZGUVzLS' unpad = 0 tmp = '' if (text[-1] == '=') & (text[-2:] != '=='): text = text[:-1] unpad = -1 if text[-2:] == '==': text = text[:-2] unpad = -2 for i in text: tmp += str(bin(code.index(i)))[2:].zfill(3) tmp = tmp[:unpad] result = long_to_bytes(int(tmp,2)) return result
defenc(text): code = 'AP3IXYxn4DmwqOlT0Q/JbKFecN8isvE6gWrto+yf7M5d2pjBuk1Hh9aCRZGUVzLS' text = ''.join([str(bin(i))[2:].zfill(8) for i in text]) length = len(text) pad = b'' if length%3 == 1: text += '00' pad = b'==' elif length%3 == 2: text += '0' pad = b'=' result = [code[int(text[3*i:3*(i+1)],2)] for i inrange(0,len(text)//3)] return''.join(result).encode()+pad
defencrypt(flag): result = b'' for i inrange(len(flag)): result += (key[i%7]^(flag[i]+i)).to_bytes(1,'big') return result
c = enc(encrypt(flag)) print(f'c = {c}')
''' c = b'IPxYIYPYXPAn3nXX3IXA3YIAPn3xAYnYnPIIPAYYIA3nxxInXAYnIPAIxnXYYYIXIIPAXn3XYXIYAA3AXnx=' '''
没有密钥,拿flag头0xGame{去推。
由(key[i%7]^(flag[i]+i))可以知道key的长度是7 。
源码自带了dec的源码,不用纠结enc了。
分析encrypt,可以知道加密过程是移位+异或。
解密脚本
from Crypto.Util.number import * defdec(text): text = text.decode() code = 'AP3IXYxn4DmwqOlT0Q/JbKFecN8isvE6gWrto+yf7M5d2pjBuk1Hh9aCRZGUVzLS' unpad = 0 tmp = '' if (text[-1] == '=') & (text[-2:] != '=='): text = text[:-1] unpad = -1 if text[-2:] == '==': text = text[:-2] unpad = -2 for i in text: tmp += str(bin(code.index(i)))[2:].zfill(3) tmp = tmp[:unpad] result = long_to_bytes(int(tmp,2)) return result
c = b'IPxYIYPYXPAn3nXX3IXA3YIAPn3xAYnYnPIIPAYYIA3nxxInXAYnIPAIxnXYYYIXIIPAXn3XYXIYAA3AXnx=' c = dec(c) #print(c)
#求key defgetkey(flag): key=b'' result=b'0xGame{' for i inrange(len(result)): key+=((flag[i]^(result[i]+i))).to_bytes(1,'big') return key
key = getkey(c) #print(key)
defdecrypt(enc): result = b'' for i inrange(len(enc)): a = (key[i%7]^(enc[i]))-i result += a.to_bytes(1,'big') return result
flag = decrypt(c) print(flag) #b'0xGame{Kn0wn_pl@intext_Att@ck!}'
Take my bag!
源码:
from Crypto.Util.number import * from secret import flag
defencrypt(m): m = str(bin(m))[2:][::-1] enc = 0 for i inrange(len(m)): enc += init[i] * int(m[i]) % n return enc
w = getPrime(64) n = getPrime(512) init = [w*pow(3, i) % n for i inrange(512)]
import random from collections import namedtuple import gmpy2 from Crypto.Util.number import isPrime, bytes_to_long, inverse, long_to_bytes from sympy import nextprime from tqdm import tqdm
w=16221818045491479713 n=9702074289348763131102174377899883904548584105641045150269763589431293826913348632496775173099776917930517270317586740686008539085898910110442820776001061 c=4795969289572314590787467990865205548430190921556722879891721107719262822789483863742356553249935437004378475661668768893462652103739250038700528111 pubkey=[w*pow(3, i) % n for i inrange(512)] #print(len(pubkey)) #272 nbit=512 #随机找一个符合条件的N N=nextprime(gmpy2.iroot(nbit,2)[0]//2) L=Matrix(QQ,nbit + 1, nbit + 1) #构造矩阵L for i inrange(nbit): L[i,i]=1
for i inrange(nbit): L[i,nbit]=pubkey[i]*N
for i inrange(nbit): L[nbit,i]=1/2
L[nbit,nbit]=c*N
print("LLL start") res=L.LLL()
for i in tqdm(range(0, nbit + 1)): # print solution M = res.row(i).list()[:-1]#最后面密文恢复后变成0 flag = True for m in M: if m != 1/2and m != -1/2:#根据破解原理,恢复的明文应只包含-1/2和1/2 flag = False break if flag: mm="" print (i, M) for j in M: if j==-1/2:#不确定-1/2和1/2哪个代表二进制1 mm+="1" else: mm+="0" flag=mm[::-1] print(long_to_bytes(int(flag,2))) #b'0xGame{Welc0me_2_Crypt0_G@me!#$&%}'