0%

2024-CryptoCTF-wp-crypto

期末总算过去了,终于有时间来更新更新已经过去了大半个月的CryptoCTF。这次比赛和认识的一些师傅一起组了个小队cyberCryer,最后拿到了第二名:

image-20240630091043812

虽然说这次比赛的一些题目实在有点无力吐槽,但是比赛中途大家一起加班加点肝题的乐趣是实打实的,再次感谢cyberCryer的各位师傅。( つ•̀ω•́)つ

由于大家一起在做题,所以部分题目的wp是我赛后复现的,并不一定是做题的师傅的思路。同时我也记录了一些比赛中的有趣的动态,留作纪念。

队伍其他师傅的wp指路:

Zima:CRYPTOCTF2024(part1) | Zimablue’ Blog

……



Warm-up

Welcome! 👋(367 solves , 27 pts)

题目描述:

1
We are excited that you will be participating in 6th CryptoCTF, an engaging online event focused on challenging and improving your cryptography abilities. You'll have the opportunity to dive into the captivating realm of ciphers, codes, and modern cryptosystems. 💪 ⚡ 💥 🔥

直接交:

1
flag: CCTF{Harn3sS_Y0ur_CrYptO9rAphy_Pr0wEs5_💪_⚡_💥_🔥!}



Easy

Alibos(181 solves , 36 pts)

题目描述:

1
Alibos, a classic cryptographic algorithm, is designed to safeguard non-sensitive data, providing a reliable solution for routine information protection.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3

from Crypto.Util.number import *
from gmpy2 import *
from secret import d, flag

get_context().precision = 1337

def pad(m, d):
if len(str(m)) < d:
m = str(m) + '1' * (d - len(str(m)))
return int(m)

def genkey(d):
skey = getRandomRange(10 ** (d - 1), 10 ** d)
pkey = int(10**d * (sqrt(skey) - floor(sqrt(skey))))
return pkey, skey

def encrypt(m, pkey):
m = pad(m, len(str(pkey)))
d = len(str(pkey))
c = (pkey + d ** 2 * m) % (10 ** d)
return c

pkey, skey = genkey(d)

m = bytes_to_long(flag)
c = encrypt(m, pkey)

print(f'pkey = {pkey}')
print(f'enc = {c}')

output.txt:

1
2
pkey = 8582435512564229286688465405009040056856016872134514945016805951785759509953023638490767572236748566493023965794194297026085882082781147026501124183913218900918532638964014591302221504335115379744625749001902791287122243760312557423006862735120339132655680911213722073949690947638446354528576541717311700749946777
enc = 6314597738211377086770535291073179315279171595861180001679392971498929017818237394074266448467963648845725270238638741470530326527225591470945568628357663345362977083408459035746665948779559824189070193446347235731566688204757001867451307179564783577100125355658166518394135392082890798973020986161756145194380336

题目含有一个未知的量d,利用这个d生成了随机的skey和pkey,然后按如下方式加密:

然而仔细看的话会发现加密式子里的d实际上是pkey的字符串长度,所以可以直接解出m然后去掉pad就行。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from Crypto.Util.number import *

pkey = 8582435512564229286688465405009040056856016872134514945016805951785759509953023638490767572236748566493023965794194297026085882082781147026501124183913218900918532638964014591302221504335115379744625749001902791287122243760312557423006862735120339132655680911213722073949690947638446354528576541717311700749946777
enc = 6314597738211377086770535291073179315279171595861180001679392971498929017818237394074266448467963648845725270238638741470530326527225591470945568628357663345362977083408459035746665948779559824189070193446347235731566688204757001867451307179564783577100125355658166518394135392082890798973020986161756145194380336

d = len(str(pkey))
m = (enc - pkey)*inverse(d**2,10**d) % 10**d

for i in range(1,300):
flag = long_to_bytes(int(str(m)[:-i]))
if(b"CCTF" in flag):
print(flag)
break


#CCTF{h0M3_m4De_cRyp70_5ySTeM_1N_CryptoCTF!!!}


Beheaded(32 solves , 131 pts)

题目描述:

1
The beheaded flags have had their headers removed, making them encrypted. Can a living entity truly survive without a head?

题目:

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

source secrets.sh

FLAGS="all_flags.txt"
rm -f "all_flags.enc"

while read flag; do
magick -background white -fill blue -pointsize 72 -size "$X"x"$Y" -gravity North caption:"$flag" flag.ppm
tail -n +4 flag.ppm > tail
openssl enc -aes-256-ecb -pbkdf2 -nosalt -pass pass:"$KEY" -in tail >> "all_flags.enc"
done < "$FLAGS"

题目将flag串写在图片上,然后对这张图片进行ECB加密,最后得到密文文件。

原理的话其实并不复杂,对于一张图片来说,其分为了很多色块,而由于是ECB加密,所以对于相同的色块(也就是背景色),其密文也一定是相同的,而这也说明一点——不是背景色块密文的地方,就一定有内容。所以可以统计密文来大致还原轮廓。而对于这个题目来讲,其就是把文字写在图片上的图片,所以还原轮廓就等于还原flag。

不过具体操作上应该还是挺有讲究,比如ppm格式、加密分组怎么对应色块等我都不是很清楚,但@Zima正好了解这些,就用了一个工具直接把他梭掉了。


Mashy(68 solves , 71 pts)

题目描述:

1
Mashy may seem like a simple cracking task, but you'll need to open your eyes to identify the right things to crack.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3

import sys
from hashlib import md5
from binascii import *
from secret import salt, flag

def die(*args):
pr(*args)
quit()

def pr(*args):
s = " ".join(map(str, args))
sys.stdout.write(s + "\n")
sys.stdout.flush()

def sc():
return sys.stdin.buffer.readline()

def xor(s1, s2):
return bytes([s1[_] ^ s2[_] for _ in range(len(s1))])

def main():
border = "┃"
pr( "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
pr(border, ".: Hi all, she did Mashy, you should do it too! Are you ready? :. ", border)
pr( "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")

REC = []
cnt, STEP = 0, 7
sh = md5(salt).digest()

while True:
pr(border, f'Please send your first input: ')
d1 = sc().strip()
pr(border, f'Please send your second input: ')
d2 = sc().strip()
try:
d1 = hexlify(unhexlify(d1))
d2 = hexlify(unhexlify(d2))
h1 = md5(unhexlify(d1)).digest()
h2 = md5(unhexlify(d2)).digest()
except:
die(border, 'Your inputs are not valid! Bye!!!')
if d1 != d2 and d1 not in REC and d2 not in REC:
if md5(xor(d1, d2)).hexdigest() != 'ae09d7510659ca40eda3e45ca70e9606':
if hexlify(xor(xor(h1, h2), sh)) == b'a483b30944cbf762d4a3afc154aad825':
REC += [d1, d2]
if cnt == STEP:
die(border, f'Congrats! the flag: {flag}')
pr(border, 'Good job, try next level :P')
cnt += 1
else:
die(border, 'Your input is not correct! Bye!')
else:
die(border, 'No this one! Sorry!!')
else:
die(border, 'Kidding me!? Bye!!')

if __name__ == '__main__':
main()

这个题总的来说要完成七轮挑战,每一轮都需要输入两个十六进制串d1、d2,靶机会计算他们的md5值h1、h2,这些值如果满足以下要求就可以通过本轮挑战:

  • d1不等于d2

  • d1、d2之前没有输入过

  • $d_1 \oplus d_2$的md5值不为’ae09d7510659ca40eda3e45ca70e9606’

  • $h_1 \oplus h_2 \oplus sh$的值为’a483b30944cbf762d4a3afc154aad825’,其中sh为(salt未知):

    1
    sh = md5(salt).digest()

怎么看都想不通要干什么,首先这个sh是不知道的,所以也就不知道h1异或h2到底要满足什么东西,只知道需要是一个定值;其次就是d1异或d2的md5值也不知道是什么,不过这个还好,仅仅是要求不为多少,很容易就能做到。

一头雾水的时候发现这个题解数蹭蹭涨,所以盲猜应该只是个md5的碰撞,也就是让d1、d2不等的情况下h1、h2相等,这也就意味着sh就是’a483b30944cbf762d4a3afc154aad825’。上在线网站生成几组前缀碰撞然后交互就好:

MD5 在线碰撞 - 百川在线工具箱 (chaitin.cn)

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from pwn import *

#context.log_level = 'debug'

sh = remote("01.cr.yp.toc.tf",13771)

msg = [(b"31000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000fe11ecc10c7ab8b4fe112d7f29ceb2dae1f39ce7a691488bfb817b7685ad087a162b0ce0ea69b140e7274b44c43f183578392f9719b43d4966c321cc0a10e6fbd002869b42f9fad9eb869dc55d5d349835961b1fd36a0bbe76a9bb5f4f5cc54136dec48d74497bf6579a6bf9721b81078637b429cff958886bd816dc4333a338",b"31000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000fe11ecc10c7ab8b4fe112d7f29ceb2dae1f39c67a691488bfb817b7685ad087a162b0ce0ea69b140e7274b44c4bf183578392f9719b43d4966c3214c0a10e6fbd002869b42f9fad9eb869dc55d5d349835961b9fd36a0bbe76a9bb5f4f5cc54136dec48d74497bf6579a6bf9729b80078637b429cff958886bd8165c4333a338"),
(b"32000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ebcd0390c198c8f33b4a08cce32f47451b5002e68895b7505d12824a0a460d133de3eff34e4de89ceb27ed3bce211b4696fec6736c059058d8b60f6e473f6c701068abf2d6bc645c4589a6f0f5211f5fc903c96e789d91f8abb300d8176088b7d31d825897c9001cf409c45b3a50005e93e33f4f908f9df944a664c927d28d55",b"32000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ebcd0390c198c8f33b4a08cce32f47451b5002668895b7505d12824a0a460d133de3eff34e4de89ceb27ed3bcea11b4696fec6736c059058d8b60fee473f6c701068abf2d6bc645c4589a6f0f5211f5fc903c9ee789d91f8abb300d8176088b7d31d825897c9001cf409c45b3ad0ff5d93e33f4f908f9df944a6644927d28d55"),
(b"3300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000068270588dbc19cfe24db79019b0070d1823c4a4d6d816cdb22c2401a32081455e5db01c54c0ccf1b6706f061eec3e58ead12b5173dee55f954f9ac52a1a4bbedc32ab0ee3cae3896f9908a49d38cc5535c6c80661d262e1ec91a8639ecc7e1654086c61bf4cfe7fc6a7378f7809416ef39ae4ccc7fc29570c4c3a51b03fed7b9",b"3300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000068270588dbc19cfe24db79019b0070d1823c4acd6d816cdb22c2401a32081455e5db01c54c0ccf1b6706f061ee43e68ead12b5173dee55f954f9acd2a1a4bbedc32ab0ee3cae3896f9908a49d38cc5535c6c80e61d262e1ec91a8639ecc7e1654086c61bf4cfe7fc6a7378f7801416ef39ae4ccc7fc29570c4c3a59b03fed7b9"),
(b"3400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000025a80516e996c87574dc568d274da9d2d281b09bbc72f7ef46767961b4a708e969ebffe06a6e0945cbefe2f2fc41d89cb89c2574afb1fdbac5aca131cdf52d68b9e3c95615888cbc3d187ccd32f60d5b35636e925c1aa3002bb330c81b6088d8cbeb8014afc24f5e374ad05ab2f30d1fac7221195b7f3f1f19f5219ecaf948a2",b"3400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000025a80516e996c87574dc568d274da9d2d281b01bbc72f7ef46767961b4a708e969ebffe06a6e0945cbefe2f2fcc1d89cb89c2574afb1fdbac5aca1b1cdf52d68b9e3c95615888cbc3d187ccd32f60d5b35636e125c1aa3002bb330c81b6088d8cbeb8014afc24f5e374ad05ab2730d1fac7221195b7f3f1f19f5211ecaf948a2"),
(b"35000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f56f1da5ad57f483059ae0869962f05204f954e2de9d56c55b168139ec372cefb1b307cbe84ce477cbff5df4d40bc46c08ceef8bb18bfc996b3b9fdaa7b72c17ee1cb3491794d6e2ff7d9914525cdf2d88b9314505ef864d201d2b364a6e25ae4d183e738c3c86e53e5ad1a1910c825cb1bcd59b97d1a3cf979668fbac6a138b",b"35000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f56f1da5ad57f483059ae0869962f05204f95462de9d56c55b168139ec372cefb1b307cbe84ce477cbff5df4d48bc46c08ceef8bb18bfc996b3b9f5aa7b72c17ee1cb3491794d6e2ff7d9914525cdf2d88b931c505ef864d201d2b364a6e25ae4d183e738c3c86e53e5ad1a1918c815cb1bcd59b97d1a3cf9796687bac6a138b"),
(b"36000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f2b343c2bb614b7b4acc4e0ade8a5e4b2abb4eefe97ef7991832793e4e301637cf0becd84c43dbafeb43d0da12c8e2883e0d02ada60e678e3470ee866abe2fa713b8a521a986e75d821f74221fee2ea441b3ab462f29b56f862b00d05f60cdc1a9a1c92d04457bd6e693673963ab6e6b6c3552cd1c54efe627b342fecdfd8dea",b"36000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f2b343c2bb614b7b4acc4e0ade8a5e4b2abb4e6fe97ef7991832793e4e301637cf0becd84c43dbafeb43d0da1248e3883e0d02ada60e678e3470ee066abe2fa713b8a521a986e75d821f74221fee2ea441b3abc62f29b56f862b00d05f60cdc1a9a1c92d04457bd6e6936739632b6e6b6c3552cd1c54efe627b3427ecdfd8dea"),
(b"370000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001d6afba61c0910b5ca21a9164b65134753b3845080d9afe6460237a1d938a8385204b0e85cce2b686b40d9b9c517e64b08ee01d02fa63470aa3b34c1e550d11356d867ea70e96b0257d9b1b20df1ee22603111180739001ad17bf6c8dd707932b34e97b62e74936197adcc2f93164b46c7c7e4bb7b6c1a55a21958961f7378be",b"370000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001d6afba61c0910b5ca21a9164b65134753b384d080d9afe6460237a1d938a8385204b0e85cce2b686b40d9b9c597e64b08ee01d02fa63470aa3b3441e550d11356d867ea70e96b0257d9b1b20df1ee22603111980739001ad17bf6c8dd707932b34e97b62e74936197adcc2f93964a46c7c7e4bb7b6c1a55a21958161f7378be"),
(b"38000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c6eb639aedf271805029b40d4dc783ab8d769d35cb9032f9184641b94c881615f5f902cb5e8ba3176f2660d4f821828fdbed111365cc4dfbbf001ca884fcb74df4f681c17ed38f53b8346319fee0c2eaecf2ea2597f0ca2c04ed6c064e9e3ddd7d37ff3e54908e889a58455a8fd411bc0f75e93cadc453f6f89d16e8cd8e7c2a",b"38000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c6eb639aedf271805029b40d4dc783ab8d769db5cb9032f9184641b94c881615f5f902cb5e8ba3176f2660d4f8a1828fdbed111365cc4dfbbf001c2884fcb74df4f681c17ed38f53b8346319fee0c2eaecf2eaa597f0ca2c04ed6c064e9e3ddd7d37ff3e54908e889a58455a8f5411bc0f75e93cadc453f6f89d1668cd8e7c2a")]

for i in range(8):
sh.sendline(msg[i][0])
sh.sendline(msg[i][1])

sh.recvuntil(b"Congrats! the flag: ")

print(sh.recvline())


#CCTF{mD5_h4Sh_cOlL!Si0N_CrYp7o_ch41lEnGe!!!}



Medium

Ahoo(29 solves , 142 pts)

题目描述:

1
Ahoo, the swiftest and most graceful gazelle in Iran, is renowned for her unmatched beauty and uncanny ability to evade even the most cunning traps.

没有附件,题目的题意是:

  • 给定一个正整数n(大概在几千这个数量级),要求给出最小的正整数c,使得t=nc的汉明重量最小

最开始当然只想到爆破,但是实际效果来说,爆破能够到的范围太小了,很容易出错。之后@yolbby想了一个用格的方法,差不多是:

  • 把t看作是一个背包,背包里的值是2^k,造格规约出来的短向量就是汉明重量比较小的。
  • 这样能找到的c的范围就和格的维数直接相关,比如格大小为256,那么格能找到的最优解就是2^256以内的c,肯定是比爆破要好的

  • 然后再单独处理一下汉明重量可能为2的情况。若汉明重量是2,那么可以写成t = 2^r(2^s+1) = nc,其实也就可以等价成c=2^s+1/n,所以可以再单独找一下比较大的c,就可以大于2^256。

造格似乎有点不太好理解,其实就是利用这个等式:

既然要让t汉明重量较小,所以如果把他用二进制表示的话,系数是很稀疏的,也就是如果写成:

那么ai应该很多0,只有极少数是1。

那么把等式移一下项就变成了:

所以就可以造格。

256这个数完全是自己选择的,要平衡一下能找到的范围和格规约的时间才行,选择256代表着可以找到2^256以内的t,大一点查找范围自然也大一些,但是规约就会慢。

上手实现了一下发现确实行,不过仍然有失败的可能,由于要通过20轮,优化了很久都没搞定,最多挂到十五十六轮的样子,所以这肯定不是通法。不过还是把solver发上来留作纪念:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from pwn import *

while(1):
sh = remote("00.cr.yp.toc.tf",17371)
while(1):
sh.recvuntil(b"n = ")
nn = int(sh.recvline().strip().decode().split(",")[0])
n = nn
print(n)
while(n % 2 == 0):
n >>= 1

nums = 256
K = 2^100
L = Matrix(ZZ,nums+1,nums+1)
for i in range(nums):
L[i,i] = 1
L[i,-1] = 2^i*K
L[-1,-1] = n*K
res = L.BKZ()

mini = 2000
c = 2^512
for i in res:
temp = i
ans = 0
if((all(j <= 0 for j in temp[:-1])) or (all(j >= 0 for j in temp[:-1]))):
for k in range(len(temp[:-1])):
ans += temp[:-1][k] * 2^k
cc = ans // n
while(cc % 2 == 0 and cc > 0):
cc = cc >> 1
temp = (bin(ans)[2:].count("1"))
if(temp < mini and cc != 0 and cc > 0):
mini = temp
c = cc
elif(temp == mini and cc != 0 and cc > 0 and cc < c):
mini = temp
c = cc

#check 2
for i in range(1,100000):
tt = "1" + "0"*i + "1"
if(int(tt,2) % n == 0):
mini = 2
c = int(tt,2) // n
break

print(bin(nn*c))
print(str(mini).encode() + b"," + str(c).encode())

sh.sendline(str(mini).encode() + b"," + str(c).encode())
tt = (sh.recvline())
if(b"not" in tt):
print("Not this time")
print()
sh.close()
sleep(5)
break
else:
print(sh.recvline())

怎么都优化不出来的时候@leukocyte登场,他看出这实际上是一个关于同余最短路的算法题,所以直接交给他做,果然很快就秒了。但我的算法功底很烂,所以应该理解不了。

赛后看春哥的wp发现说可以求出前几项之后上OEIS查数列,就能查到关于通解的论文,以及前10000项的结果,所以可以查表然后发回去。


Alilbols(59 solves , 80 pts)

题目描述:

1
Alilbols, a modified version of the Alibos cryptographic algorithm, offers enhanced security features to protect sensitive and confidential data.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python3

from Crypto.Util.number import *
from gmpy2 import *
from secret import d, flag

def genkey(d):
while True:
f = getRandomRange(1, int(sqrt(2) * 10 ** d))
g = getRandomRange(10 ** d, int(sqrt(2) * 10 ** d))
if gcd(f, 10 * g) == 1:
q = 4 * 100 ** d
h = inverse(f, q) * g % q
if gcd(h, 10 * d) == 1:
break
pkey, skey = (d, h), (f, g)
return pkey, skey

def encrypt(m, pkey):
d, h = pkey
q = 4 * 100 ** d
assert m < 10 ** d
r = getRandomRange(1, 10 ** d // 2)
c = (r * h + m + r) % q
return c

pkey, _ = genkey(d)
m = bytes_to_long(flag)
c = encrypt(m, pkey)

print(f'h = {pkey[1]}')
print(f'c = {c}')

output.txt:

1
2
h = 1051643987107349427988807326909852110640860009433515828832892541964729933410444984350917250524103015414239941369074041041830326426044333499878031164851095096864048639115431370526747014210332286314344073411522846701723463410585601251886732229726828022089809603850477551571014006202841406236367999378786782206165205893353928598469661871284779486855440579818275314024966224282757807716013799903830828885606714972634243850947534165272668985513949964901606268939300116019465522042467054120201087606016018354238401711720121586874288767235317479748890350702705575809130664969776549574720593740409234863974057904204809404816059921579771581800937241591669455683460570640868196509926763901079838233646036933530095891316054589051458146768287967886035091641162494322987627448810201550901588438560433001422233269632915351406169253963308421081459981594969405377353502889363324282815864766827664453823780238352371809048289845094882346227809082005375092441877966603138648719670349093616548820955566204871333952902983753935678447080673827214244142614295192263451840766771122229866931492260663320087497820892824540996643905125018452302747847009
c = 11913143174789215053772744981113562063689725867199301496294410323568897757042952642806438602327917861884988292757318755590132189620231444302311290566584065812614959093870787195145654508262419270742989923415342357807325941686508030706603920412262004324188375072184983301522882728578077572816154054220606088703932092256905881975876112779175003897105313776239681492514925430817300633974666123599685062340158348009344351002327049272743679109535286730751345284084148118733529966364414749672437370878526710641430471595906340522772252875146681541656231708112317601000655090279925720590940060372738708208419449824043905057860829031242339842131799965043031307394209699264362321397162645220002253271689678364848888381499587038475895945238726252440250183268252483198408039250213490525880829604473555612305513974817850974135874728084839426045420913060975464553734293001460752648937744531874552694145500413222582269910431269597066268600572899619407093373565994271589940926018891922169454906132284552523035481664164354874071831210264979733079749696197917769435226866441989054017071332158916586376454753209296136133271926449919437888563234409

题目仍然是有一个未知的量d,利用这个d先生成了一组密钥pkey和skey,然后再用公钥pkey加密m得到密文c。

那么就先看这个genkey,其步骤是:

  • 生成f、g,满足:

  • 生成q,满足$q = 4 \cdot 10^{2d}$

  • 计算h:

  • 得到公钥(d,h),私钥(f,g)

之后看encrypt:

  • q还是上面那个q

  • 取一个随机数r,$r \in (1,\frac{10^d}{2})$

  • 计算密文c:

给出h、c,要求还原m。

可以看出他的genkey其实是一个ntru,所以有q的情况下可以造格得到f、g。然而问题在于题目没有给出q也没有给出d,但是可以发现c、h都是模q下的量,所以q应该会略大于c、h,因此可以简单在范围附近爆破一下d,检查一下规约出的f、g是否合理就行。

得到f、g之后就等于得到了私钥,之后类似ntru去解密。由于有:

所以:

和ntru一样,由于各个数字数量级均有限制,所以整个式子不模q也是成立的,也就是:

所以有:

然后再在f+g下对f求逆就可以得到m。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from Crypto.Util.number import *

h = 1051643987107349427988807326909852110640860009433515828832892541964729933410444984350917250524103015414239941369074041041830326426044333499878031164851095096864048639115431370526747014210332286314344073411522846701723463410585601251886732229726828022089809603850477551571014006202841406236367999378786782206165205893353928598469661871284779486855440579818275314024966224282757807716013799903830828885606714972634243850947534165272668985513949964901606268939300116019465522042467054120201087606016018354238401711720121586874288767235317479748890350702705575809130664969776549574720593740409234863974057904204809404816059921579771581800937241591669455683460570640868196509926763901079838233646036933530095891316054589051458146768287967886035091641162494322987627448810201550901588438560433001422233269632915351406169253963308421081459981594969405377353502889363324282815864766827664453823780238352371809048289845094882346227809082005375092441877966603138648719670349093616548820955566204871333952902983753935678447080673827214244142614295192263451840766771122229866931492260663320087497820892824540996643905125018452302747847009
c = 11913143174789215053772744981113562063689725867199301496294410323568897757042952642806438602327917861884988292757318755590132189620231444302311290566584065812614959093870787195145654508262419270742989923415342357807325941686508030706603920412262004324188375072184983301522882728578077572816154054220606088703932092256905881975876112779175003897105313776239681492514925430817300633974666123599685062340158348009344351002327049272743679109535286730751345284084148118733529966364414749672437370878526710641430471595906340522772252875146681541656231708112317601000655090279925720590940060372738708208419449824043905057860829031242339842131799965043031307394209699264362321397162645220002253271689678364848888381499587038475895945238726252440250183268252483198408039250213490525880829604473555612305513974817850974135874728084839426045420913060975464553734293001460752648937744531874552694145500413222582269910431269597066268600572899619407093373565994271589940926018891922169454906132284552523035481664164354874071831210264979733079749696197917769435226866441989054017071332158916586376454753209296136133271926449919437888563234409

for d in range(560,580):
q = 4 * 100 ** d
if(q > c):
F = int(sqrt(2) * 10 ** d)
G = int(sqrt(2) * 10 ** d)
L = Matrix(ZZ, [[1, h],
[0, q]])
res = L.LLL()[0]
f, g = res[0] , res[1]
if(abs(f) < F and abs(g) > 10 ** d and abs(g) < G and f*g > 0):
f = abs(f)
g = abs(g)
mf = c*f % q % (f+g)
m = mf * inverse(f,f+g) % (f+g)
print(long_to_bytes(m))
break

#CCTF{4_c0N9rU3n7!aL_Pu81iC_k3Y_cRyp70_5ySTeM_1N_CCTF!!}


Ally(22 solves , 174 pts)

题目描述:

1
Ally enjoys the challenge of solving Diophantine equations, so help them tackle this latest complex equation as well.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3

import sys
from Crypto.Util.number import *
from flag import flag

def die(*args):
pr(*args)
quit()

def pr(*args):
s = " ".join(map(str, args))
sys.stdout.write(s + "\n")
sys.stdout.flush()

def sc():
return sys.stdin.buffer.readline()

def main():
border = "┃"
pr( "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
pr(border, ".:: Ally is my best friend, help him to solve his tough task ::.", border)
pr( "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")

nbit = 14
level, step = 0, 19
while True:
pr(border, f'Please send your {nbit}-bit prime: ')
p = sc().strip()
try:
p = int(p)
except:
die(border, 'Your input is not valid! Bye!!!')
if isPrime(p) and p.bit_length() == nbit:
pr(border, 'Send the solution of the following Diophantine equation in positive integers x, y')
pr(border, f'{p} * (x - y)**3 = (x**2 + y) * (x + y**2)')
xy = sc().strip().decode()
try:
x, y = [int(_) for _ in xy.split(',')]
except:
die(border, 'Your answer is not valid! Bye!!!')
if p * (x - y)**3 == (x**2 + y) * (x + y**2) and x > 0 and y > 0:
if level == step:
die(border, f'Congratz! You got the flag: {flag}')
else:
pr(border, f'Good job, try the next step {level + 2}')
level += 1
nbit = int(1.2*nbit) + getRandomRange(0, 6)
else:
die(border, 'Your answer is not correct! Bye!!')
else:
die(border, 'Kidding me!? Bye!!')

if __name__ == '__main__':
main()

题目共19轮挑战,每一轮挑战需要满足:

  • 输入一个指定大小的素数p

  • 给出下面丢番图方程的一个正整数解:

赛中的时候研究这个研究了好久,既没有搜到啥论文,也没想到能怎么自己构造。最后@yolbby给出了一个巧妙的思路:

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from pwn import *
from Crypto.Util.number import *
from tqdm import *

sh = remote('01.cr.yp.toc.tf',13777)
for i in trange(20):
sh.recvuntil(b'Please send your ')
a = sh.recvuntil(b'-')[:-1]
a = int(a)
sh.recvline()

while 1:
p = getPrime(a)
if p % 4 == 1:
y = (p-1)//4
x = 2*y+1
sh.sendline(str(p).encode())
sh.recvline()
sh.sendline(f'{x},{y}'.encode())
sh.recv()
break

print(sh.recvline())


#CCTF{Di0phaNtinE_eQuaT1on_iZ_4n_equ4tion_wiTh_int3ger_solu7Ions_0nly!}'


Bada(51 solves , 90 pts)

题目描述:

1
The Bada equation contains an undetermined function. By closely examining how this equation behaves, you may be able to discover the concealed flag.

依然没有附件,题目内容是:

存在一个未知的函数f,满足:

其中所有值都是整数,靶机会给出两个值:

要求求出x,y。

可以看出,从(1,1)到(x,y)的迭代过程完全可以看作是两个不同的等差数列,也就是:

而这两个等差数列又显然可以合成一个等差数列,也就是:

用求和公式就是:

记:

所以有:

然后交互过程中又发现了一个事情,那就是d总是一个素数,所以就很好办了,要使上式成立就只能:

然后解方程就可以得到x、y了——然而发回去不对。

当时可能正是夜里三四点钟,@Zima看了这题过后死活没看出解法哪里有问题,就叫我来;我看了看也死活没看出问题,就拉来了@yolbby;结果他也没看出来解法问题在哪,三脸懵逼。

第二天早上@hash-hash起来之后就把他解掉了,看了看思路发现说其实上面的式子还有一个可能就是:

而这样去解方程再发回去就正确了,只能说他的check逻辑不是太对(估计是直接判断是否和第二种这个解相等)。


Duzly(0 solves , 500 pts)

题目描述:

1
Duzly is a straightforward hash function design based on congruence relationships over a prime number modulus.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env sage

from Crypto.Util.number import *
from os import urandom
from flag import flag

def pad(m):
m += b'\x8f' * (8 - len(m) % 8)
return m

def duzly(m, C):
ow, E = 0, [2**24 + 17, 2**24 + 3, 3, 2, 1, 0]
for _ in range(6):
ow += C[_] * pow(m, E[_], p)
return ow % p

def pashan(msg):
msg = pad(msg)
pash, msg = b'', [msg[8*i:8*(i+1)] for i in range(len(msg) // 8)]
for m in msg:
_h = duzly(bytes_to_long(m), C).to_bytes(8, 'big')
pash += _h
return pash

p = 2**64 - 59
C = [1] + [randint(0, p) for _ in range(5)]
flag = urandom(getRandomRange(0, 110)) + flag + urandom(getRandomRange(0, 110))
_pash = pashan(flag)

f = open('_pash_updated', 'wb')
f.write(str(C).encode() + b'\n')
f.write(_pash)
f.close()

题目不长,但是够狠。加密流程为:

  • 生成一个长度为6的随机列表C,第一个值是1,其他均为0-p的随机数

  • 将flag首位进行两段长度未知的随机填充

  • 将flag切分为长度为8的多个分组

  • 对每个分组mi,进行如下加密得到对应密文ci:

由于update里给出了C,所以这其实是一个关于mi的单变量方程,所以自然想到直接求根,然而这个多项式度是$2^{24}+17$,实在太大了,根本建不出来,所以要想下怎么降次。但是模数p是一个非常正常的素数(也就是longlong型的最大素数),2^24虽然大,但完全不会是某个元素的阶,所以消不掉。

一筹莫展的时候想了点歪点子,首先可以知道,flag虽然在首尾进行了填充,但是”CCTF{“这五个字符仍然一定在里面。又因为八个字符八个字符分了组,所以这个flag头可能有如下八种切分方式:

1
2
3
4
5
6
7
8
CCTF{ + YYY
X + CCTF{ + YY
XX + CCTF{ + Y
XXX + CCTF{
XXXX + CCTF | { + YYYYYYY
XXXXX + CCT | F{ + YYYYYY
XXXXXX + CC | TF{ + YYYYY
XXXXXXX + C | CTF{ + YYYY

其中,X代表纯随机字符,Y代表可见字符。可以看出,八种情况中,只有5、6两组复杂度稍大,其他的情况复杂度都不算特别高,可以进行爆破。而期望是这样:如果得到了正确的flag头的内容,比如”CCTF{i_lo”这种,就可以合理猜测下一个分组的是ve开头,从而连蒙带猜往后一个分组一个分组爆。

然而事与愿违,我一开始密文读取方式错了,导致zima师傅写的c++的多进程爆破完全就没用对数据,自然也没结果。时间又很紧张,所以最后没有搞出来flag头,自然也就不存在往后的连蒙带猜了。

赛后看discord的讨论,突然反应过来正解应该怎么做。首先对于上面的ci,可以把它看成一个以mi为根的多项式:

而在模p下天然有一个以mi为根的多项式:

所以如果求解两者的gcd就会得到:

就有mi了。

但是,f、g的次数太大仍然是个问题,对于这一点的处理和我在NSSCTF的dlc出的一道Franklin有点类似。由于f的次数较低,所以可以用f做商环,将g一步一步lift上去,之后再用HGCD求出两者的公因式,而在商环上lift的过程sage内部是有实现的。

然而,2^24这个数量级好像刚好超过了sage能处理的次数上限,所以没法直接做,并且由于有多个分组都要这么处理,所以可能要自己用c++手动写个多项式的lift,并且用更大内存的机器来跑才行。

要我写这个的话,我短时间是肯定憋不出来的,但是如果赛中早点想到这个思路,然后交给@leukocyte感觉肯定能写,还是有点可惜,自己出过的类似题没有联想上。


Forghan(36 solves , 119 pts)

题目描述:

1
The Forghan, the combination of RSA and DLP cryptography, may in certain instances prove more accessible than employing either method individually.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env sage

import sys
from Crypto.Util.number import *
from hashlib import sha256
from flag import flag

def die(*args):
pr(*args)
quit()

def pr(*args):
s = " ".join(map(str, args))
sys.stdout.write(s + "\n")
sys.stdout.flush()

def sc():
return sys.stdin.buffer.readline()

def find_gen(p):
while True:
g = getRandomRange(2, p - 1)
if pow(g, (p-1)//2 , p) != 1:
return g

def main():
border = "┃"
pr( "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
pr(border, "Hi all, now it's time to solve a strange and unusual RSA and DLP ", border)
pr(border, "challenge about encryption! Follow the questions and find the secret", border)
pr(border, "flag! :) ", border)
pr( "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")
nbit, b = 256, False
while True:
pr(f"| Options: \n|\t[G]et encrypted flag \n|\t[P]ublic parameters \n|\t[S]ubmit {nbit} primes \n|\t[Q]uit")
ans = sc().decode().lower().strip()
if ans == 'g':
if b == True:
l, n = len(flag), (p**2 - 1) * (q**2 - 1)
gp, gq = find_gen(p), find_gen(q)
sp, sq = getRandomRange(1, p), getRandomRange(1, q)
flagp, flagq = flag[:l // 2], flag[l // 2:]
yp, yq = pow(gp, sp, p), pow(gq, sq, q)
cp, cq = pow(bytes_to_long(flagp), yp, n), pow(bytes_to_long(flagq), yq, n)
pr(border, f'cp = {cp}')
pr(border, f'cq = {cq}')
else: pr(border, 'Please first send your primes! :P')
elif ans == 's':
pr(border, 'Send your desired prime numbers separated by comma: ')
P = sc()
try:
p, q = P.split(b',')
p, q = int(p), int(q)
except: die(border, 'Your input are not integer! Bye!!')
if p != q and isPrime(p) and isPrime(q) and p.bit_length() == q.bit_length() == nbit:
b = True
pr(border, 'Now you can get the encrypted flag in main menu!')
else: die(border, 'Sorry, your integers are not valid :/')
elif ans == 'p':
if b == True:
pr(border, f' gp = {gp}')
pr(border, f' gq = {gq}')
pr(border, f' yp = {yp}')
pr(border, f' yq = {yq}')
else: pr(border, 'Please first send your primes! :P')
elif ans == 'q':
die(border, 'Quitting...')
else:
die(border, 'You should select valid choice!')

if __name__ == '__main__':
main()

题目有三个选项,但是显然是有顺序的,具体来说应该按照下面来:

  • 选择”S”,可以输入256bit的素数p、q
  • 选择”G”,靶机对flag进行加密,加密流程如下:
    • 生成n,$n = (p^2-1)(q^2-1)$
    • 分别生成p、q下的一个随机二次非剩余gp、gq
    • 生成p、q下的随机数sp、sq
    • 将flag分为两段,记为flagp、flagq,对应数字记为mp、mq
    • 计算yp、yq,$y_p = g_p^{s_p}\quad(mod\;p) , y_q = g_q^{s_q}\quad(mod\;q)$
    • 计算密文cp、cq,$c_p = m_p^{y_p}\quad(mod\;n) , c_q = m_q^{y_q}\quad(mod\;n)$
  • 选择”P”,可以拿到gp、gq、yp、yq的值

由于有yp、yq,所以对于密文最后一步的计算来说,其实就相当于解一个RSA,只是模数n变成了:

由于flag是静态的,所以完全可以放在一个子群里去求解,比如放在模p-1下:

也就是说,只要知道p-1的分解,就可以求phi(p-1),然后就可以解RSA解出mp、mq在p-1下的值,又由于flag是静态的,所以交互多次求crt,就能得到flag的完整值。而p、q都是自己构造的,所以想满足p-1能分解太容易了,比如最简单的情况就构造:

令k是一个素数,就有:

之后的求解就很容易。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from Crypto.Util.number import *
from sympy.ntheory.modular import crt
from pwn import *

sh = remote("00.cr.yp.toc.tf", "13337")

def gen_prime():
while True:
p = getPrime(255)
if isPrime(2*p+1):
return 2*p+1

nums = 10
P = [gen_prime() for i in range(nums)]
mp = []
mq = []
for i in range(nums):
sh.sendline(b"s")
sh.recvline(b'Send your desired prime numbers separated by comma: ')
q = getPrime(256)
sh.sendline((str(P[i])+','+str(q).encode()))

sh.sendline(b"g")
sh.recvuntil("cp = ")
cp = int(sh.recvline().strip().decode())
sh.recvuntil("cq = ")
cq = int(sh.recvline().strip().decode())


sh.sendline(b"p")
sh.recvuntil("gp = ")
gp = int(sh.recvline().strip().decode())
sh.recvuntil("gq = ")
gq = int(sh.recvline().strip().decode())
sh.recvuntil("yp = ")
yp = int(sh.recvline().strip().decode())
sh.recvuntil("yq = ")
yq = int(sh.recvline().strip().decode())

k = (P[i] - 1) // 2
mp.append(pow(cp,inverse(yp,k-1),P[i]))
mq.append(pow(cq,inverse(yq,k-1),P[i]))


Mp = crt(P,mp)[0]
Mq = crt(P,mq)[0]

print(long_to_bytes(Mp))
print(long_to_bytes(Mq))

如果这题需要用到DLP的话,那构造p-1光滑应该是好点,然而sp、sq完全用不到,所以不用考虑。


Honey(48 solves , 95 pts)

题目描述:

1
Honey is a concealed cryptographic algorithm designed to provide secure encryption for sensitive messages.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3

from Crypto.Util.number import *
from math import sqrt
from flag import flag

def gen_params(nbit):
p, Q, R, S = getPrime(nbit), [], [], []
d = int(sqrt(nbit << 1))
for _ in range(d):
Q.append(getRandomRange(1, p - 1))
R.append(getRandomRange(0, p - 1))
S.append(getRandomRange(0, p - 1))
return p, Q, R, S

def encrypt(m, params):
p, Q, R, S = params
assert m < p
d = int(sqrt(p.bit_length() << 1))
C = []
for _ in range(d):
r, s = [getRandomNBitInteger(d) for _ in '01']
c = Q[_] * m + r * R[_] + s * S[_]
C.append(c % p)
return C


nbit = 512
params = gen_params(512)
m = bytes_to_long(flag)
C = encrypt(m, params)
f = open('params_enc.txt', 'w')
f.write(f'p = {params[0]}\n')
f.write(f'Q = {params[1]}\n')
f.write(f'R = {params[2]}\n')
f.write(f'S = {params[3]}\n')
f.write(f'C = {C}')
f.close()

params_enc.txt:

1
2
3
4
5
p = 10580731215444436219213907263947534038012197972307836319229421193761088798378768844649759133142120180834573817149711299466707823017636232456526471274387917
Q = [6718668664591596190749745980002066645380242844394957953766947533978323053938214647829798301606252456858132121628517723050462291300790766055200866765561610, 8738840830394886495658505803088103824478963010774845789433253508554356383249611502157307334585157729703873877797759121271071421201959116272886732798936523, 4388762712805764363921857352899834586382140923234814556069490536704913653848525595836491615636446563386705915348021173847271741862809075809151508973332816, 3663706247989213864330218414789109172658418861584264092087052781618522795676355371739296186667918464732397854703792563460353675590182379535358561615166754, 10397730940373180549512945920847346184926672474430866208628825035104473525758952069910968144296138220861205803231072660136999110567752870928953292888013817, 7950983396364741732874562189206547723862955251595526752956177377987683115942827501152009639962778147887569469649852864894630521759276627026901168996371682, 4533165373271154956275563812280832107592547920299130443910706773435844651231402604986107252454770256826684895729900344877444002682896222483157835711226276, 234527648838985018479849393379369972316648524004050156622962478277970628212208049885578895953447529009579881605066831810617219492342569126524704457098602, 870946394610707169333783318085559836426827503011955242395779894700298192587685130680102208909573921059078858816359885827349281892186755920839352494224983, 5025393101560564396356619431938053414422489109406396725377683594254768077793838379117179092536396259414266971990125716674162127500490058572594571364978294, 5914192169617888877888201158713062853387719050723973971874176184801960297380845845091259990664210178355882621876155306106794686897854891414999449337977781, 2315609284318723939818174971181608382568372323836754133265010413836452399043354382657889810068171230196496294710150903020084150025606782486579490225752163, 7278167622527482910919537896950398911672667355929695202886784227621290914468002099686668156909569183497765732237075799745632960354306268680765408194219074, 3334584846711780119716613440720851482199838919351927489954344426327712973616885459737471991960238971274273643566666607141850753561424427088261862873317161, 10418397794302836806813296591826255845845380563102199636140458670781241436466478273414138749593148481207713557016996462238845705251702717702279222414513227, 8928267741189301140931758366885435860144549752680480164627323600501278650103818630920534258909582593266779000356145650416276415560226176904904552964196921, 3371514716248416335324237152123624115597170761078576760713238311485522010416246207731778456501311675041827750001670698984335573888627309609534659722697174, 3802217515688979225517388823688564230191961273039428065604146662684362686419670392762755984078261072614619934671572327384351597903233780846163893180876244, 2363399549320749312563257796730356164186914867645487733176402567625567991193120859507532138374793392237469893062402307645814490930874653088116729203724851, 10221767730316631512882371487958453352895713655492241322139865275323706929127996334620105099751973217983332705204535216804829987329684674632121801754529472, 2309440702968799222696132507585555901684240893289264193546892279919675495689944211715270510347394465285317239758024734161176699837687742722383530547735726, 5768185970426887633783322939933762164095055854707759575404348181421158843802654199133776622565384843677610666356549368021172121302926969754865633752131083, 3229518654067354667224244723968136366109210341994628338992752927462431316675552817799465987425719486448276862390918033544772400219803600904202906882850710, 9261930571620802581580992489567043178258044978932724785185749415955521702331959591680386023118398455066896674799447994060060156036800986190467008880726445, 2202162000802916921896507108984564185119471354726317829850206292755815439071735424116041491064894922930900262540589602333676063925873357256494020426758849, 1449198886322850923273373987785167865779477069790726992542423846556138762913056524689998313112599261515223680429584445016474216381861998363527368722991846, 8372052560034461177472546395510480308837387803551378399004608402463282066003862653368875671946863748209051044725913651881213858335224750549176875818647614, 1554572177534183877889322947083536921680809086638640689433294150868422460719624621258231995856944045776876555664816113444481761122803489100212103150005950, 10304720995101153898600333216365877709018991122985401884049110267116778080162237155472336396606109372189653686733770775660895990549286854727042270667997449, 9702831715765050072649082617719386580543641764650576307262259428035281535816750758018451681417440458250169771442858047124693499821240155899358663466925522, 4983986466837077050816175727911146971276599399191126236666755181304580074902883995859030897874305902445659355326021836801836996577665092276845357389900055, 983210432268706135181772492997088637756153310076409721616450343187239073604891492532080986521255675448499887782529162112441952861587689660048849369850769]
R = [8922553268219903948421811612403588317187402276064169063400419617931637566221670948925221403527928336656976084021780421068421112325215152715630319708159148, 1960697234007608888633325436691320507012497445090151112947144246088597317322303593936974554881161091213718329061011703885436579261559669838148253146762736, 5876944865176517279136475269375710514719489457672467542904948332791352244229042691977616905122711469012731652511966060550749288338599104284986451669586397, 10560136356360422684127995001096871178761635291417665630629868184624616925939841693017240341233504589896610287855672892399213367967385666891688408976236926, 477096251733995832965266836067676961413182629438628989729801411920869543083075545758386996801847267554704996660190900105380190625423846876906822024677634, 7001514955678391252130680521301315254365131738168087539892724691183104608345983375763663321467662055864103520121513349630333839818372656472731157470876337, 3894488356665934776074291789545516608573077899099880057920210752197885106521699021123658077208789295954145485399473386424677782787720074605813565437678385, 6084641463140385894120457093933350076091808742065950836200598466936433741373700151235571333073965513559605688011967994215074945374832957951384088589789549, 6429023674027239778212676334778900557043774851747196071499704016422574667955121145943674677254291410204037372996870181094090300709994594837656541352954321, 974464046378184190666336249935678253753198649184272269465353741281529523364289314148552833155935080422698020734516538377662137879089845095565536185765703, 5275384626572520577089146723078373110279682903874445477027602487061203157331398955233780208890725281673434536867800450553737656852356536771518379108118530, 7195767683001605593626199889963767438584070902169912442665828755648712278523740276132000225353756257232761885567134364203207306842108602537457408438487869, 1960155276835684836483043402384876176138942980630724993481564836712252228823186653467973806509964860286129730847244573674203959835565358735171611743573721, 6414607819273473402861616605113355396690170400984793217629732347141796693554056264547151212348354332930126795040213921009711560216969452391748438893237750, 3344964019305423035551401397282638905496921053458747459062023331678484337724980196690184898986671355848101101352105363764794975082772539709853216580762130, 5599385562588302350377616767130563945375472947459594912838322011021483641484084692396462025157944023828872267520122213089523093591160284337483108416804096, 9490045098551912983475126253859124028793327975874647810947095579199208473971726763051464010595903748080632329362605820021188148160826282120484098519748832, 980400922804123638777377509067942595214234592161404114303498013065244168965487370334832921426484278913774963761155003569793282819717667783351454812080994, 2772369382727364487748752144766350276094463435156818133766210304998890765120785013435379746537938377688617479112518505844159436213691418873590807740501183, 2338271137215119073302625621754099128003710500967811947590498668688826448575898279561111743087002717955161536157254689000474464404087443266193145449601031, 11773104862539842923315548180471993854469651503131148446111338098437913152663007837927793233931818835121946274135336436554801339749906320765919816150768, 7022131435585860015767289025786078578929344550873832550705723505006303288639135070849692494020122369694090619694760175970759059981608871097298624956669246, 9062158671807923729547552523768819638817707994079942965412925347441853183471973898478745711676153003756450481351777543455041815438536630317647408008349357, 641428039466528875429106458292463262780539744201857515833377175893334510962304373010401729801987725599439620073783868719664061882399367340158128062151780, 9636846145225909579086917154824161374486916920345217833190333031690359415616710658317308376119096253360425924297858397657697468296746934075225665559206030, 480796179229094678008918831581475398488769027287954488301969143979307355466114726392701279092691566949082439457297527772794149828193400419285116237063338, 7726757640854263742178678415761753342099387727318458723726925176786999799204056696883433346568600944559959849253863013300907357120126559055735593380638961, 551534415366550033034654093228329954588738781407008585088422737955391043381667720876717529438845777841508116853874405455280903517864352822604194884203656, 2135598634800053933515509095633720732412542790075717247869348997292949946344227105219967710854885153055617943193159899627517699960920415492992774438014041, 1680242449902645450228202409049382139482100876672724104665964994414865664877710770701122475977142858601068246748276652246938681740022531970523695731126206, 436320498659370791836085607479390848415464961245449366885835640616716752879866126057724425537944356751844317980881005608075886211154052092270888058429787, 4102523817041969170017113001399252035243358293719837916039521471412151711746047572340376418109842112515873700164747338421862615679590520014959369523777063]
S = [4389820170235953517860364281419052587762385444517203945856665517072263529411621622474249480804818285716662154444854074939122170918939074730520091008754679, 10274488321950407401790238304858775303749809597573328350457719402010568443492374388509295355784873292280254116167747959663379913408799485392015111636115272, 7413556897779783875511997253964328306011661062497568739340607424890313315434489440969977560260194970241163209968988660276109536299848001455072705137702052, 10385679419514177727801069016719283683697529805734389950368725106396939072509839502286093730172215158898251533786500497578747904349856564856054102927847131, 6386209810414906928429352824399745808560059492214757951425372272591372428785367326567204502131184260857447725722068674542931096991341451850846209503213310, 699060860182347708294712482476262878008951502586470987307146194685783850677219935862211847525612405824474382122868643521962157410369483858428437433802395, 3482063900392942412450041046839813864192488604261419419162091087116878838189062406913587662602295096636481252242577251563441565388543873530900962308450590, 5752782775722534186844731777766684686828626762801341093006843547008915880093927377014105260314050196609699164926717998841388194942556402699285523644651583, 4477944786670422304592908174461368396168580171651041832449759231077050962444090886920563302603784981035116689839335069690875797644553684152368376089740517, 5663531682848751034979876476955107364567717396957230368140915480695954150088052156264521615690161327525898382714131905580517915060427960250740110846321921, 5343294510843224726960051115907810365241970373986845465490090821566645536683695042467985025063439338770116316455211944030125820601506874250685838077414765, 8432688027895892738831218358369397874948328355540048945541749304849571948638169902486647265119397307193449002927427840453857643697175667051504836511635801, 8974418251288081645329898313903422373193272601106837017923535786950979222125740970411725976034947989767143786286397290996756916960291029965672692318584390, 3367471493907450997862234076499110882206518716388236141312548251406626961838619595519805436447472248133422979102036617759221276869918910189482246041609756, 9787518698663334219763616437865759554315749369906898604904977007297368987715422535407542350049111845605011307829034744443271718643291528381794716569586150, 7423298611637442306439453382713995476685370148677494272333567929180589186347708172471032286209947149761644993151042408431548488945145292037694546868884030, 5591656321514817104832226351286824953278894430528912246855770091334337189386156994498331013600732669813560603667226878637404391237947398475280409452591013, 6425108527382412819259579180312607504985609322110590326058191420486308943785080395001318724782575187718535133704219706236946232717333616646838328259100174, 1986072520726417425427679478185786673218113837249425820170849864471171328875606626923270664642579676457285438864056080352157246995774517812392120146048999, 5085759490547346080963649291763871068034757573012867868532204864022791365366745839456310985148663327808561601274994541505734129506831803550600738607170703, 6883330982174382523423400136207766091739299571009207949454774998664997037115272990237878573502506026799645646664956134682833693219974019271740996314084220, 3008412285222633004812698063455902388460013611944185187747762649311442328686187279186972723448077521425052318221865300502697494418867793831761784796146329, 9547325670583212399117684599284936334790443591395268814958834705867928621240882386996085347052830889322724160452729359284992294890584292625829913315628265, 1387461799657365419593288084566026147529011675413652640829187706708872066267641962620159408303872024421411747751690792902149761832079721583778430712988299, 2093635222672050417361673475374966389314266702410603863643637918543784930893405832179904330416148995313591588929177333026382745340633946128791737631559286, 8413424329301946557805781268429552577339778139904799140486659388945506980343653897206690153402822892272378378085908749995513711021119914769801970795906117, 6743445162380434316634371637355512041805280159276931422012464596435558338432827675791139455542184532473287436049595748235961499758716697674776760822162391, 1706184129470213725753097073154985919221181897433450716898208607625832464228411992900434982560203334218404626447424408012817411257665032706434306723906871, 1188503046129710474378492502021666062040985983530761819828342979325166292485440608536173516789179154601587903599847267180858095017007891547798890802090080, 6398862094282462456834873185064362330936738060688527379705123125559724680574585721467123968313071365176318010618981170019917278363707049487278610027058585, 1827205671808118170959457466922915851906256049105694256301380823860317738339383848734269783842511423911532135506082729000044271560428423457063013176002958, 4296939764995860347968082436772567300063081379392577066061720737920992283479200350022263164173968370055486217109628430523063986426005741344085155171587948]
C = [2312453804397990204892582347458673184184584053391181580849656202381982276483135032773767708029907426388840618138608941250788745829088238589339462137662516, 10523194306636352419831471584744199603299973937259007033119401866043123235256118686290774285411893111835433604432082195914308420845569896693674208644701928, 8625321422409900297730698589684636810222608572364004227411854906715574297306124060030713845833976664653817196364988412308992879167741430256046508164902265, 2203036357494864574281685606458518951592160562338358743010726696365683441724958156704554110208947394458113054047499157258753175446414976136008162642109757, 7440676439428237992864596387460092947449968530635365999432104198675297081825315672568959667296286389774687367936168132053293001589687795737604457160906082, 2264157050840270501520646271743213633490605461967539030408028006097728678459599299655371587125945029431528702214373899493252891890106222569122603386717360, 6089828885562252420652081197249651438359320954630388805129416420415135298321118452771481678140904854945253504361325478946359729157231808943846058944117026, 10539688120079451218718476929576210805778407047878263373562960368159158621424931104363627052374700437454925765616146962059684388174993415278124860987310938, 6303692164422236243124789748609418445914012315334563733630138248326249660310698584154622748275297380026916444293448955157206727703202478794453250926439643, 881942845076614931271891025655927553295291244201690295230440723375006916146507322259936249039275621554840488955637696630030022348109669072833863866985225, 10106348190249831893088197931090851286229747534150777530156601113972916238286905365616240501204440809598108220364456446320022747806334510176621958436096123, 8324531392881516178541244041903222514911964835924579628962940318936570586739198442236357026438613345385230182029810965182157993322744038688573053225547374, 5078708553426349749719952343873018594648548601008242439539543130067536764970709366523519348572603124221309659983256536103306454063139670063244331611755453, 8542547993117228744822361667182819410791296436924016725757178153507707272004493213120780743910507865839367749205817741703451968423781285521621014600059589, 3073352723766074586873446203285051955281142348730058300259816322651361605792426446390729367386148254808552616968786890277451301146248756651016086606742804, 5269945451683706496607356863109615383647000556701922770404180133092195426319754674753614204346587769298246334991876508268642550219392732660290378353338400, 6262348731181694593163187372863680743743096474259867989757989072530511417286900866334036812204673457159090130621478933266270029789176903023636735673595513, 3297899863181037954379261853482229450757449782777910450819629975965660022355584717412057615899333569245281976118829898854119850433763380827003244277020101, 7487260566995972228255217362172107520309457024141075543923988636564806307827017535285595701845830793533604534902349660541692391232785351590333159515799384, 10324279404125558600858330541468243667961239247663241031255287540601841176203292036413076990028875224629890818418992943759206077129550049357885152924273782, 8044925106089251438446731137002808317322635126054485149617494154471416674741758172633019611642437845768869330746423337854351186668227486117398013073824286, 4234270952814400783009242730655483882561531800274439758860142245952005010632014464198770218689605527760730047723966950012227395838571254801244673898812828, 638896609043108497262778277770178050665854883307447937008504779907661322405672174519391657570064336725509768185637608044022219246206471015980252602095129, 5180216795602211214280366461349507739255238657924206736673167715462995927060334503420979145327254053283011116720223275075476559911219732336496503542596869, 4889582376915206124736222022750168286228699065224704085027955184917144323557844593386817223260498520607466742269645929630472738644444652704289510157100913, 5012604326308554636339480431591200785633930724891396744558818436357664982175033998281443810436271724799610836412819040278374041390604150781759163613994682, 3219038638665180576932584279254771209098300305384572525709292133614238552451580810601286362213655499134538083785424726484119059950676285763438331520089227, 6419694420004223063062285264931096360778228156157853232796125041025067958687740255716444718822744614217377927689465894996156144384079594697465808095114901, 9910546009146267464166501179537825349776178695164070160517872596078275117785713000045215455672195660329319962408970209137481672915542900445933477806115760, 1420603945244588398788478464408905133932325733716125030735921688979482121297328679317495694245547716568441401137633433150951713462958551532394178628021900, 6208847506587273852328049116772424002595270959861992270025537759079430511161476024600259347318966551584005222005704567718008394909230998308545154993159673, 10385926333158107236552504228168776101396372491461292301486697050842973861408756486936932908479675556344542947425765714101814836237112561730341100901890350]

题目给了d组如下等式:

所有c、Q、R、S均已知,要求还原m。同时,每次的ri、si都是d bit的数,p是512bit。

由列表长度可以知道d就是32,所以r、s都是很小的量,因此自然想到消去m做一个HNP。消去的方式也很朴素,就是取第0组和第i组的等式:

分别将m前系数乘至相等:

然后作差就消掉m了,之后就是造个格子去做HNP就行。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from Crypto.Util.number import *

p = 10580731215444436219213907263947534038012197972307836319229421193761088798378768844649759133142120180834573817149711299466707823017636232456526471274387917
Q = [6718668664591596190749745980002066645380242844394957953766947533978323053938214647829798301606252456858132121628517723050462291300790766055200866765561610, 8738840830394886495658505803088103824478963010774845789433253508554356383249611502157307334585157729703873877797759121271071421201959116272886732798936523, 4388762712805764363921857352899834586382140923234814556069490536704913653848525595836491615636446563386705915348021173847271741862809075809151508973332816, 3663706247989213864330218414789109172658418861584264092087052781618522795676355371739296186667918464732397854703792563460353675590182379535358561615166754, 10397730940373180549512945920847346184926672474430866208628825035104473525758952069910968144296138220861205803231072660136999110567752870928953292888013817, 7950983396364741732874562189206547723862955251595526752956177377987683115942827501152009639962778147887569469649852864894630521759276627026901168996371682, 4533165373271154956275563812280832107592547920299130443910706773435844651231402604986107252454770256826684895729900344877444002682896222483157835711226276, 234527648838985018479849393379369972316648524004050156622962478277970628212208049885578895953447529009579881605066831810617219492342569126524704457098602, 870946394610707169333783318085559836426827503011955242395779894700298192587685130680102208909573921059078858816359885827349281892186755920839352494224983, 5025393101560564396356619431938053414422489109406396725377683594254768077793838379117179092536396259414266971990125716674162127500490058572594571364978294, 5914192169617888877888201158713062853387719050723973971874176184801960297380845845091259990664210178355882621876155306106794686897854891414999449337977781, 2315609284318723939818174971181608382568372323836754133265010413836452399043354382657889810068171230196496294710150903020084150025606782486579490225752163, 7278167622527482910919537896950398911672667355929695202886784227621290914468002099686668156909569183497765732237075799745632960354306268680765408194219074, 3334584846711780119716613440720851482199838919351927489954344426327712973616885459737471991960238971274273643566666607141850753561424427088261862873317161, 10418397794302836806813296591826255845845380563102199636140458670781241436466478273414138749593148481207713557016996462238845705251702717702279222414513227, 8928267741189301140931758366885435860144549752680480164627323600501278650103818630920534258909582593266779000356145650416276415560226176904904552964196921, 3371514716248416335324237152123624115597170761078576760713238311485522010416246207731778456501311675041827750001670698984335573888627309609534659722697174, 3802217515688979225517388823688564230191961273039428065604146662684362686419670392762755984078261072614619934671572327384351597903233780846163893180876244, 2363399549320749312563257796730356164186914867645487733176402567625567991193120859507532138374793392237469893062402307645814490930874653088116729203724851, 10221767730316631512882371487958453352895713655492241322139865275323706929127996334620105099751973217983332705204535216804829987329684674632121801754529472, 2309440702968799222696132507585555901684240893289264193546892279919675495689944211715270510347394465285317239758024734161176699837687742722383530547735726, 5768185970426887633783322939933762164095055854707759575404348181421158843802654199133776622565384843677610666356549368021172121302926969754865633752131083, 3229518654067354667224244723968136366109210341994628338992752927462431316675552817799465987425719486448276862390918033544772400219803600904202906882850710, 9261930571620802581580992489567043178258044978932724785185749415955521702331959591680386023118398455066896674799447994060060156036800986190467008880726445, 2202162000802916921896507108984564185119471354726317829850206292755815439071735424116041491064894922930900262540589602333676063925873357256494020426758849, 1449198886322850923273373987785167865779477069790726992542423846556138762913056524689998313112599261515223680429584445016474216381861998363527368722991846, 8372052560034461177472546395510480308837387803551378399004608402463282066003862653368875671946863748209051044725913651881213858335224750549176875818647614, 1554572177534183877889322947083536921680809086638640689433294150868422460719624621258231995856944045776876555664816113444481761122803489100212103150005950, 10304720995101153898600333216365877709018991122985401884049110267116778080162237155472336396606109372189653686733770775660895990549286854727042270667997449, 9702831715765050072649082617719386580543641764650576307262259428035281535816750758018451681417440458250169771442858047124693499821240155899358663466925522, 4983986466837077050816175727911146971276599399191126236666755181304580074902883995859030897874305902445659355326021836801836996577665092276845357389900055, 983210432268706135181772492997088637756153310076409721616450343187239073604891492532080986521255675448499887782529162112441952861587689660048849369850769]
R = [8922553268219903948421811612403588317187402276064169063400419617931637566221670948925221403527928336656976084021780421068421112325215152715630319708159148, 1960697234007608888633325436691320507012497445090151112947144246088597317322303593936974554881161091213718329061011703885436579261559669838148253146762736, 5876944865176517279136475269375710514719489457672467542904948332791352244229042691977616905122711469012731652511966060550749288338599104284986451669586397, 10560136356360422684127995001096871178761635291417665630629868184624616925939841693017240341233504589896610287855672892399213367967385666891688408976236926, 477096251733995832965266836067676961413182629438628989729801411920869543083075545758386996801847267554704996660190900105380190625423846876906822024677634, 7001514955678391252130680521301315254365131738168087539892724691183104608345983375763663321467662055864103520121513349630333839818372656472731157470876337, 3894488356665934776074291789545516608573077899099880057920210752197885106521699021123658077208789295954145485399473386424677782787720074605813565437678385, 6084641463140385894120457093933350076091808742065950836200598466936433741373700151235571333073965513559605688011967994215074945374832957951384088589789549, 6429023674027239778212676334778900557043774851747196071499704016422574667955121145943674677254291410204037372996870181094090300709994594837656541352954321, 974464046378184190666336249935678253753198649184272269465353741281529523364289314148552833155935080422698020734516538377662137879089845095565536185765703, 5275384626572520577089146723078373110279682903874445477027602487061203157331398955233780208890725281673434536867800450553737656852356536771518379108118530, 7195767683001605593626199889963767438584070902169912442665828755648712278523740276132000225353756257232761885567134364203207306842108602537457408438487869, 1960155276835684836483043402384876176138942980630724993481564836712252228823186653467973806509964860286129730847244573674203959835565358735171611743573721, 6414607819273473402861616605113355396690170400984793217629732347141796693554056264547151212348354332930126795040213921009711560216969452391748438893237750, 3344964019305423035551401397282638905496921053458747459062023331678484337724980196690184898986671355848101101352105363764794975082772539709853216580762130, 5599385562588302350377616767130563945375472947459594912838322011021483641484084692396462025157944023828872267520122213089523093591160284337483108416804096, 9490045098551912983475126253859124028793327975874647810947095579199208473971726763051464010595903748080632329362605820021188148160826282120484098519748832, 980400922804123638777377509067942595214234592161404114303498013065244168965487370334832921426484278913774963761155003569793282819717667783351454812080994, 2772369382727364487748752144766350276094463435156818133766210304998890765120785013435379746537938377688617479112518505844159436213691418873590807740501183, 2338271137215119073302625621754099128003710500967811947590498668688826448575898279561111743087002717955161536157254689000474464404087443266193145449601031, 11773104862539842923315548180471993854469651503131148446111338098437913152663007837927793233931818835121946274135336436554801339749906320765919816150768, 7022131435585860015767289025786078578929344550873832550705723505006303288639135070849692494020122369694090619694760175970759059981608871097298624956669246, 9062158671807923729547552523768819638817707994079942965412925347441853183471973898478745711676153003756450481351777543455041815438536630317647408008349357, 641428039466528875429106458292463262780539744201857515833377175893334510962304373010401729801987725599439620073783868719664061882399367340158128062151780, 9636846145225909579086917154824161374486916920345217833190333031690359415616710658317308376119096253360425924297858397657697468296746934075225665559206030, 480796179229094678008918831581475398488769027287954488301969143979307355466114726392701279092691566949082439457297527772794149828193400419285116237063338, 7726757640854263742178678415761753342099387727318458723726925176786999799204056696883433346568600944559959849253863013300907357120126559055735593380638961, 551534415366550033034654093228329954588738781407008585088422737955391043381667720876717529438845777841508116853874405455280903517864352822604194884203656, 2135598634800053933515509095633720732412542790075717247869348997292949946344227105219967710854885153055617943193159899627517699960920415492992774438014041, 1680242449902645450228202409049382139482100876672724104665964994414865664877710770701122475977142858601068246748276652246938681740022531970523695731126206, 436320498659370791836085607479390848415464961245449366885835640616716752879866126057724425537944356751844317980881005608075886211154052092270888058429787, 4102523817041969170017113001399252035243358293719837916039521471412151711746047572340376418109842112515873700164747338421862615679590520014959369523777063]
S = [4389820170235953517860364281419052587762385444517203945856665517072263529411621622474249480804818285716662154444854074939122170918939074730520091008754679, 10274488321950407401790238304858775303749809597573328350457719402010568443492374388509295355784873292280254116167747959663379913408799485392015111636115272, 7413556897779783875511997253964328306011661062497568739340607424890313315434489440969977560260194970241163209968988660276109536299848001455072705137702052, 10385679419514177727801069016719283683697529805734389950368725106396939072509839502286093730172215158898251533786500497578747904349856564856054102927847131, 6386209810414906928429352824399745808560059492214757951425372272591372428785367326567204502131184260857447725722068674542931096991341451850846209503213310, 699060860182347708294712482476262878008951502586470987307146194685783850677219935862211847525612405824474382122868643521962157410369483858428437433802395, 3482063900392942412450041046839813864192488604261419419162091087116878838189062406913587662602295096636481252242577251563441565388543873530900962308450590, 5752782775722534186844731777766684686828626762801341093006843547008915880093927377014105260314050196609699164926717998841388194942556402699285523644651583, 4477944786670422304592908174461368396168580171651041832449759231077050962444090886920563302603784981035116689839335069690875797644553684152368376089740517, 5663531682848751034979876476955107364567717396957230368140915480695954150088052156264521615690161327525898382714131905580517915060427960250740110846321921, 5343294510843224726960051115907810365241970373986845465490090821566645536683695042467985025063439338770116316455211944030125820601506874250685838077414765, 8432688027895892738831218358369397874948328355540048945541749304849571948638169902486647265119397307193449002927427840453857643697175667051504836511635801, 8974418251288081645329898313903422373193272601106837017923535786950979222125740970411725976034947989767143786286397290996756916960291029965672692318584390, 3367471493907450997862234076499110882206518716388236141312548251406626961838619595519805436447472248133422979102036617759221276869918910189482246041609756, 9787518698663334219763616437865759554315749369906898604904977007297368987715422535407542350049111845605011307829034744443271718643291528381794716569586150, 7423298611637442306439453382713995476685370148677494272333567929180589186347708172471032286209947149761644993151042408431548488945145292037694546868884030, 5591656321514817104832226351286824953278894430528912246855770091334337189386156994498331013600732669813560603667226878637404391237947398475280409452591013, 6425108527382412819259579180312607504985609322110590326058191420486308943785080395001318724782575187718535133704219706236946232717333616646838328259100174, 1986072520726417425427679478185786673218113837249425820170849864471171328875606626923270664642579676457285438864056080352157246995774517812392120146048999, 5085759490547346080963649291763871068034757573012867868532204864022791365366745839456310985148663327808561601274994541505734129506831803550600738607170703, 6883330982174382523423400136207766091739299571009207949454774998664997037115272990237878573502506026799645646664956134682833693219974019271740996314084220, 3008412285222633004812698063455902388460013611944185187747762649311442328686187279186972723448077521425052318221865300502697494418867793831761784796146329, 9547325670583212399117684599284936334790443591395268814958834705867928621240882386996085347052830889322724160452729359284992294890584292625829913315628265, 1387461799657365419593288084566026147529011675413652640829187706708872066267641962620159408303872024421411747751690792902149761832079721583778430712988299, 2093635222672050417361673475374966389314266702410603863643637918543784930893405832179904330416148995313591588929177333026382745340633946128791737631559286, 8413424329301946557805781268429552577339778139904799140486659388945506980343653897206690153402822892272378378085908749995513711021119914769801970795906117, 6743445162380434316634371637355512041805280159276931422012464596435558338432827675791139455542184532473287436049595748235961499758716697674776760822162391, 1706184129470213725753097073154985919221181897433450716898208607625832464228411992900434982560203334218404626447424408012817411257665032706434306723906871, 1188503046129710474378492502021666062040985983530761819828342979325166292485440608536173516789179154601587903599847267180858095017007891547798890802090080, 6398862094282462456834873185064362330936738060688527379705123125559724680574585721467123968313071365176318010618981170019917278363707049487278610027058585, 1827205671808118170959457466922915851906256049105694256301380823860317738339383848734269783842511423911532135506082729000044271560428423457063013176002958, 4296939764995860347968082436772567300063081379392577066061720737920992283479200350022263164173968370055486217109628430523063986426005741344085155171587948]
C = [2312453804397990204892582347458673184184584053391181580849656202381982276483135032773767708029907426388840618138608941250788745829088238589339462137662516, 10523194306636352419831471584744199603299973937259007033119401866043123235256118686290774285411893111835433604432082195914308420845569896693674208644701928, 8625321422409900297730698589684636810222608572364004227411854906715574297306124060030713845833976664653817196364988412308992879167741430256046508164902265, 2203036357494864574281685606458518951592160562338358743010726696365683441724958156704554110208947394458113054047499157258753175446414976136008162642109757, 7440676439428237992864596387460092947449968530635365999432104198675297081825315672568959667296286389774687367936168132053293001589687795737604457160906082, 2264157050840270501520646271743213633490605461967539030408028006097728678459599299655371587125945029431528702214373899493252891890106222569122603386717360, 6089828885562252420652081197249651438359320954630388805129416420415135298321118452771481678140904854945253504361325478946359729157231808943846058944117026, 10539688120079451218718476929576210805778407047878263373562960368159158621424931104363627052374700437454925765616146962059684388174993415278124860987310938, 6303692164422236243124789748609418445914012315334563733630138248326249660310698584154622748275297380026916444293448955157206727703202478794453250926439643, 881942845076614931271891025655927553295291244201690295230440723375006916146507322259936249039275621554840488955637696630030022348109669072833863866985225, 10106348190249831893088197931090851286229747534150777530156601113972916238286905365616240501204440809598108220364456446320022747806334510176621958436096123, 8324531392881516178541244041903222514911964835924579628962940318936570586739198442236357026438613345385230182029810965182157993322744038688573053225547374, 5078708553426349749719952343873018594648548601008242439539543130067536764970709366523519348572603124221309659983256536103306454063139670063244331611755453, 8542547993117228744822361667182819410791296436924016725757178153507707272004493213120780743910507865839367749205817741703451968423781285521621014600059589, 3073352723766074586873446203285051955281142348730058300259816322651361605792426446390729367386148254808552616968786890277451301146248756651016086606742804, 5269945451683706496607356863109615383647000556701922770404180133092195426319754674753614204346587769298246334991876508268642550219392732660290378353338400, 6262348731181694593163187372863680743743096474259867989757989072530511417286900866334036812204673457159090130621478933266270029789176903023636735673595513, 3297899863181037954379261853482229450757449782777910450819629975965660022355584717412057615899333569245281976118829898854119850433763380827003244277020101, 7487260566995972228255217362172107520309457024141075543923988636564806307827017535285595701845830793533604534902349660541692391232785351590333159515799384, 10324279404125558600858330541468243667961239247663241031255287540601841176203292036413076990028875224629890818418992943759206077129550049357885152924273782, 8044925106089251438446731137002808317322635126054485149617494154471416674741758172633019611642437845768869330746423337854351186668227486117398013073824286, 4234270952814400783009242730655483882561531800274439758860142245952005010632014464198770218689605527760730047723966950012227395838571254801244673898812828, 638896609043108497262778277770178050665854883307447937008504779907661322405672174519391657570064336725509768185637608044022219246206471015980252602095129, 5180216795602211214280366461349507739255238657924206736673167715462995927060334503420979145327254053283011116720223275075476559911219732336496503542596869, 4889582376915206124736222022750168286228699065224704085027955184917144323557844593386817223260498520607466742269645929630472738644444652704289510157100913, 5012604326308554636339480431591200785633930724891396744558818436357664982175033998281443810436271724799610836412819040278374041390604150781759163613994682, 3219038638665180576932584279254771209098300305384572525709292133614238552451580810601286362213655499134538083785424726484119059950676285763438331520089227, 6419694420004223063062285264931096360778228156157853232796125041025067958687740255716444718822744614217377927689465894996156144384079594697465808095114901, 9910546009146267464166501179537825349776178695164070160517872596078275117785713000045215455672195660329319962408970209137481672915542900445933477806115760, 1420603945244588398788478464408905133932325733716125030735921688979482121297328679317495694245547716568441401137633433150951713462958551532394178628021900, 6208847506587273852328049116772424002595270959861992270025537759079430511161476024600259347318966551584005222005704567718008394909230998308545154993159673, 10385926333158107236552504228168776101396372491461292301486697050842973861408756486936932908479675556344542947425765714101814836237112561730341100901890350]

d = len(C)
nums = len(C)

L = Matrix(ZZ,3*nums,3*nums)
for i in range(2*nums):
L[i,i] = 1
L[2*nums,2*nums] = 2^32
for i in range(1,nums):
L[0,2*nums+i] = R[0]*Q[i]
L[1,2*nums+i] = S[0]*Q[i]
L[2*i,2*nums+i] = -R[i]*Q[0]
L[1+2*i,2*nums+i] = -S[i]*Q[0]
L[2*nums,2*nums+i] = C[i]*Q[0] - C[0]*Q[i]
for i in range(1,nums):
L[-i,-i] = p
L[:,-1:] *= 1


res = L.LLL()
r0,s0 = res[0][0],res[0][1]
m = (C[0] - r0*R[0] - s0*S[0])*inverse(Q[0],p) % p
print(long_to_bytes(int(m)))


#CCTF{3X7eNdED_H!dD3n_nNm8eR_pR0Bl3m_iN_CCTF!!}

可以看到m其实也不大,所以不消m直接规约也可以。


Joe-19(120 solves , 47 pts)

题目描述:

1
Joe-19 is a cryptographic system that leverages a top-secret version of GPT AI technology to develop advanced and robust cryptographic tools.

题目:

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env sage

from GPT import GPT6 # deep fake
from Crypto.Util.number import *
from flag import flag

P = [GPT6('A 512-bit prime appears in consecutive digits of e') for _ in range(4)]
n, m = prod(P), bytes_to_long(flag)
c = pow(m, 0x10001, n)
print(f'n = {n}')
print(f'c = {c}')

output.txt:

1
2
n = 8098851734937207931222242323719278262039311278408396153102939840336549151541408692581651429325092535316359074019383926520363453725271849258924996783681725111665666420297112252565291898169877088446887149672943461236879128453847442584868198963005276340812322871768679441501282681171263391133217373094824601748838255306528243603493400515452224778867670063040337191204276832576625227337670689681430055765023322478267339944312535862682499007423158988134472889946113994555274385595499503495488202251032898470224056637967019786473820952632846823442509236976892995505554046850101313269847925347047514591030406052185186963433
c = 7109666883988892105091816608945789114105575520302872143453259352879355990908149124303310269223886289484842913063773914475282456079383409262649058768777227206800315566373109284537693635270488429501591721126853086090237488579840160957328710017268493911400151764046320861154478494943928510792105098343926542515526432005970840321142196894715037239909959538873866099850417570975505565638622448664580282210383639403173773002795595142150433695880167315674091756597784809792396452578104130341085213443116999368555639128246707794076354522200892568943534878523445909591352323861659891882091917178199085781803940677425823784662

看这题的时候一脸懵逼,这意思是不是用65537去连续拼接得到一个512bit的素数p?尝试了一下无果之后就先跑去做别的题目了。

之后回来看发现已经被@Astrageldon解决了,不过文档里没有写过程。之后又问了下@Zima,他告诉我可以:

image-20240630113251927

XD

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from Crypto.Util.number import *

n = 8098851734937207931222242323719278262039311278408396153102939840336549151541408692581651429325092535316359074019383926520363453725271849258924996783681725111665666420297112252565291898169877088446887149672943461236879128453847442584868198963005276340812322871768679441501282681171263391133217373094824601748838255306528243603493400515452224778867670063040337191204276832576625227337670689681430055765023322478267339944312535862682499007423158988134472889946113994555274385595499503495488202251032898470224056637967019786473820952632846823442509236976892995505554046850101313269847925347047514591030406052185186963433
c = 7109666883988892105091816608945789114105575520302872143453259352879355990908149124303310269223886289484842913063773914475282456079383409262649058768777227206800315566373109284537693635270488429501591721126853086090237488579840160957328710017268493911400151764046320861154478494943928510792105098343926542515526432005970840321142196894715037239909959538873866099850417570975505565638622448664580282210383639403173773002795595142150433695880167315674091756597784809792396452578104130341085213443116999368555639128246707794076354522200892568943534878523445909591352323861659891882091917178199085781803940677425823784662

p = 7728751393377105569802455757436190501772466214587592374418657530064998056688376964229825501195065837843125232135309371235243969149662310110328243570065781
q = 9688632098638681429535439991332657144752666147923336383829750592576742104399942931057096761773496510622226977570278994077236841491368959008277469453600569
r = 10019005372961705640183251650710051163228093250949727357306333102512304273058618645339800283588040423877666492199352609508401454089083503146788384653241593
s = 10795109107229646654467923653403055635071360620150038008453082390943756377071343139771120080956310498862485323957447467376538994662280143050510681877597429

m = pow(c, inverse(65537,(p-1)*(q-1)*(r-1)*(s-1)), n)
print(long_to_bytes(m))


#CCTF{ASIS_h1r3_7aL3nT5_t0_cO1La8orAt3_!N_Crypto_CTF!}

之后看春哥的wp才知道题面的e说的是自然对数那个e,所以预期应该是要枚举这个数字的连续bit去找素数。


Melek(66 solves , 73 pts)

题目描述:

1
Melek is a secret sharing scheme that may be relatively straightforward to break - what are your thoughts on the best way to approach it?

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env sage

from Crypto.Util.number import *
from flag import flag

def encrypt(msg, nbit):
m, p = bytes_to_long(msg), getPrime(nbit)
assert m < p
e, t = randint(1, p - 1), randint(1, nbit - 1)
C = [randint(0, p - 1) for _ in range(t - 1)] + [pow(m, e, p)]
R.<x> = GF(p)[]
f = R(0)
for i in range(t): f += x**(t - i - 1) * C[i]
P = [list(range(nbit))]
shuffle(P)
P = P[:t]
PT = [(a, f(a)) for a in [randint(1, p - 1) for _ in range(t)]]
return e, p, PT

nbit = 512
enc = encrypt(flag, nbit)
print(f'enc = {enc}')

output太长就不放在这,可以去NSSCTF下载附件。

这个题目过于直白,首先在模p下建立一个度为t-1的多项式f,其中常数项是flag加密后的密文,其他系数均未知。然后随机给出了t个点值对,并且给了e、p的值。显然拉格朗日插值过后再对e求一次关于p-1的逆就行。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from Crypto.Util.number import *

e,p,PT =

PR.<x> = PolynomialRing(Zmod(p))
recover_f = PR.lagrange_polynomial(PT)
c = recover_f(0)

m2 = pow(c,inverse(e//2,p-1),p)
PR.<y> = PolynomialRing(Zmod(p))
f = y^2 - m2
res = f.roots()
print(long_to_bytes(int(res[1][0])))

#CCTF{SSS_iZ_4n_3fF!ciEn7_5ecr3T_ShArIn9_alGorItHm!}


Nabat(25 solves , 159 pts)

题目描述:

1
Nabat is a cryptographic challenge that explores the representation of polynomials within a specific polynomial ring structure.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env sage

import sys
from flag import flag

def die(*args):
pr(*args)
quit()

def pr(*args):
s = " ".join(map(str, args))
sys.stdout.write(s + "\n")
sys.stdout.flush()

def sc():
return sys.stdin.buffer.readline()

def randstr(l):
return ''.join([printable[randint(0, 90)] for _ in range(l)])

def check(f, l):
R = PolynomialRing(ZZ, 'x')
f, g = R(f), R(x^2 + x + 2)
coefs = f.list()
_b1 = all(abs(_) <= 1 for _ in coefs)
_b2 = f.degree() + 1 - 2 * n(log(l)) >= 0
_b3 = coefs.count(0) >= 2 * f.degree() // 3 - 3
_b4 = (f - l) % g == 0
if _b1 and _b2 and _b3 and _b4:
return True
return False

def main():
border = "┃"
pr( "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
pr(border, "Welcome to the NABAT challenge, your mission is to validate the main", border)
pr(border, "check function in the provided system, Try your best to find flag :)", border)
pr( "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")
step = 12
R = PolynomialRing(ZZ, 'x')
pr(border, f"Send a polynomial that satisfies the check function for each given `n'.")
for i in range(1, step):
n = randint(2**i, 2**(i + 1))
pr(border, f"Your are in step {i} and n = {n}, please send the polynomial f:")
_f = sc().decode()
try:
_f = R(_f)
except:
die(border, f"The polynomial you provided is is not valid!")
_b = check(_f, n)
if _b:
if i == step - 1:
die(border, f'Congrats, you got the flag: {flag}')
else:
pr(border, f'You have successfully passed step {i}. Please proceed to the next step :)')
else:
die(border, f"Your input does not meet the requirements!!!")

if __name__ == '__main__':
main()

题目有12轮挑战,每轮挑战会先生成一个随机数n(随着轮数变大),需要给出一个多项式f,f满足四个条件就可以通过本轮挑战:

  • 所有系数需要为1,0,-1
  • f的度d要大于等于$2logn - 1$
  • 系数为0的数量要大于等于$\lfloor \frac{2d}{3} \rfloor-3$
  • f-n是g的因子

其中,g是:

和其他商环类题一样的思路,由于在模g下有:

所以:

同理就有:

所以说其实整个多项式f的系数的产生过程,其实可以看作是从常数n开始的向高次项系数的迭代。而为了满足剩下三个要求,通俗一点来说就是要迭代的越彻底越好,也就是每一项的系数都要尽可能消成0。

那就从常数n开始思考,假设他约减了k0个2之后变成1、0或者-1,记这个系数为a0,也就是:

那么接下来,一次项和二次项的系数就变成了k0,对于一次项来说,其约减k1个2之后变成a1,a1同样只能取1、0或者-1,那么这个k1又会贡献给二次项和三次项,所以二次项会从k0+k1开始约减,依此类推。整个多项式系数就会变成如下的线性迭代过程:

所以对上面的格进行规约就可以得到需要的结果。至于度怎么选择,为了满足题目条件的话应该是越大越好?这里我取了400。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from random import randint

def check(f, l):
R = PolynomialRing(ZZ, 'x')
f, g = R(f), R(x^2 + x + 2)
coefs = f.list()
_b1 = all(abs(_) <= 1 for _ in coefs)
_b2 = f.degree() + 1 - 2 * int(log(l)) >= 0
_b3 = coefs.count(0) >= 2 * f.degree() // 3 - 3
_b4 = (f - l) % g == 0
if _b1 and _b2 and _b3 and _b4:
return True
return False

def getf(l):
d = 400
assert d > 2*int(log(l)) + 1
L = Matrix(ZZ,d,d+2)
L[0,0] = 1
L[0,1] = l
for i in range(1,d):
L[i,i] = 2
L[i,i+1] = 1
L[i,i+2] = 1
res = L.LLL()

PR.<x> = PolynomialRing(ZZ)
for v in res:
if v[0] == -1:
v = -v
elif v[0] == 1:
f = PR(v[1:].list())
if check(f, l):
return f
break


step = 12
R = PolynomialRing(ZZ, 'x')
for i in range(1, step):
n = randint(2**i, 2**(i + 1))
f = getf(n)
print(i,f)

这个题目赛中的时候,在未更新之前,其实一共需要通过40轮,并且条件3限制为:

  • 系数为0的数量要大于等于$\lfloor \frac{2d}{3} \rfloor$

可以用这个方法检查一下,发现通过概率低了很多很多。赛中@leukocyte@hash-hash也是利用三值多项式来约减系数,不过应该是用了另一个策略来满足后三个条件。然后大家一起挂那个脚本,最好的时候也就挂到了28轮。看discord才发现果然又是题的问题。


Nazdone(35 solves , 122 pts)

题目描述:

1
Nazdone is a cryptographic exercise focused on the practical challenges of generating random prime numbers for real-world applications.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3

from Crypto.Util.number import *
from random import *
from secret import params, flag

def sol(m, a, z):
p = m * (a - 1) % 2 + 1
while True:
R = list(range(1, a))
shuffle(R)
for r in R[:z]:
p += getRandomRange(0, 2) * m ** r
if isPrime(p):
return p
else:
p = m * (a - 1) % 2 + 1


p, q, r = [sol(*params) for _ in '007']
n = p * q * r
m = bytes_to_long(flag)
c = pow(m, params[0] ** 3 + params[2] - 2, n)
print(f'n = {n}')
print(f'c = {c}')

output.txt:

1
2
n = 301929748923678449872944933611657670834216889867340028357609265175830693931365828840717548752313862343315133541384709574659039910206634528428504034051556622114290811586746168354731258756502196637977942743110508997919976400864419640496894428180120687863921269087080600917900477624095004141559042793509244689248253036809126205146653922738685595903222471152317095497914809983689734189245440774658145462867680027337
c = 104375152140523502741159687674899095271676058870899569351687154311685938980840028326701029233383897490722759532494438442871187152038720886122756131781086198384270569105043114469786514257765392820254951665751573388426239366215033932234329514161827069071792449190823827669673064646681779764841034307000600929149689291216313319444583032339045277433847691961234044840927155960887984372868669401051358701522484473320

题目的params由三个未知量m、a、z构成,利用这三个值生成了三个素数p、q、r,具体生成过程也就是sol函数,流程是:

  • 取p初始值,为1或者2

  • 随机打乱1到a的列表,并取前z项,记为a1,a2,…,az,之后将p加上如下值:

    其中bi是随机的0或者1

  • 检查p是否是素数,是的话就返回

可以看出,其实p就等于:

或者:

而p、q、r都是这种类型的素数,其乘积构成n。最后按如下方式加密:

仅给出n、c,要求还原flag。

首先,对于params的三个参数m、a、z可以说是一无所知,必须要利用n来还原他们,还原的依据就是p、q、r满足的等式。不妨假设他们都是第一种类型(第二种同理),那么n就该等于:

可以看出,如果进行展开的话,在模m下n就为1(如果是第二种类型就为8),因此我们可以简单爆破一下,就可以拿到m:

1
2
3
4
5
6
7
8
from Crypto.Util.number import *

n = 301929748923678449872944933611657670834216889867340028357609265175830693931365828840717548752313862343315133541384709574659039910206634528428504034051556622114290811586746168354731258756502196637977942743110508997919976400864419640496894428180120687863921269087080600917900477624095004141559042793509244689248253036809126205146653922738685595903222471152317095497914809983689734189245440774658145462867680027337
c = 104375152140523502741159687674899095271676058870899569351687154311685938980840028326701029233383897490722759532494438442871187152038720886122756131781086198384270569105043114469786514257765392820254951665751573388426239366215033932234329514161827069071792449190823827669673064646681779764841034307000600929149689291216313319444583032339045277433847691961234044840927155960887984372868669401051358701522484473320

for m in range(2,2000):
if(n % m == 1 or n % m == 8):
print(m)

但是这样可以发现备选项还是有一点多,这个时候就要利用另一个事实,也就是bi只会是0或1这个条件。仍然用第一种情形进行讨论,n减去1后,他应该是一个较稀疏的m进制数才对(第二种类型则为n-8)。所以可以优化一下刚才的脚本来得到更符合要求的m:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from Crypto.Util.number import *

def getbase(num,base):
li = []
while(num > 0):
li.append(num % base)
num //= base
return sum(li)


n = 301929748923678449872944933611657670834216889867340028357609265175830693931365828840717548752313862343315133541384709574659039910206634528428504034051556622114290811586746168354731258756502196637977942743110508997919976400864419640496894428180120687863921269087080600917900477624095004141559042793509244689248253036809126205146653922738685595903222471152317095497914809983689734189245440774658145462867680027337
c = 104375152140523502741159687674899095271676058870899569351687154311685938980840028326701029233383897490722759532494438442871187152038720886122756131781086198384270569105043114469786514257765392820254951665751573388426239366215033932234329514161827069071792449190823827669673064646681779764841034307000600929149689291216313319444583032339045277433847691961234044840927155960887984372868669401051358701522484473320

for m in range(2,2000):
if(n % m == 1):
li = getbase(n-1,m)
print(m,li)
elif(n % m == 8):
li = getbase(n-8,m)
print(m,li)

输出的数据是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2 706
3 862
4 1059
6 1226
8 1629
9 1744
12 2004
18 2954
19 839
24 3424
29 3960
36 4296
53 6012
58 6201
72 7464
87 9354
106 10491
116 11106
159 15052
174 15315
212 18447
232 18933
261 22756
318 25573
348 28334
361 8489
424 34794
477 37056
522 38266
607 42389
636 49836
696 47656
954 67644
1044 76347
1272 85161
1537 93384
1908 119410

可以看出m很可能等于19。

然后用.log检查一下n在19进制下的最大次数,可以发现是321,说明p、q、r三个素数的次数差不多都在107左右。而又因为作为m进制的数字,其系数都为0或1,因此就只需要做个深搜即可,深搜思路和二进制下深搜基本没差:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def find(pl,ql,rl,h):
if(h > 200):
return

mask = m**h
if(pl*ql*rl % mask != n % mask):
return

if(n % pl == 0):
print(pl)
if(n % ql == 0):
print(ql)
if(n % rl == 0):
print(rl)

else:
for i in range(2):
for j in range(2):
for k in range(2):
find(i*19**h+pl,j*19**h+ql,k*19**h+rl,h+1)

find(2,2,2,0)

得到p、q、r:

1
2
3
p = 3530869780887683268140728773245395170410635845517928489976021534009516369358447511411853596093628646212566313257712207746790503704123439
q = 1274643990900453659882765495755380673132370043776742526720540623203930562265302201405804416214498793910236254519606076438989349099317154363
r = 67086525836865982094673880600810256005961617151283317566078153226175705789594094624822652838168627925461219084263915001883644637391279141

然后就是常规RSA解密,虽然z并不知道,但是显然范围也很小,所以爆破就好。

完整exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from Crypto.Util.number import *

def getbase(num,base):
li = []
while(num > 0):
li.append(num % base)
num //= base
return sum(li)


n = 301929748923678449872944933611657670834216889867340028357609265175830693931365828840717548752313862343315133541384709574659039910206634528428504034051556622114290811586746168354731258756502196637977942743110508997919976400864419640496894428180120687863921269087080600917900477624095004141559042793509244689248253036809126205146653922738685595903222471152317095497914809983689734189245440774658145462867680027337
c = 104375152140523502741159687674899095271676058870899569351687154311685938980840028326701029233383897490722759532494438442871187152038720886122756131781086198384270569105043114469786514257765392820254951665751573388426239366215033932234329514161827069071792449190823827669673064646681779764841034307000600929149689291216313319444583032339045277433847691961234044840927155960887984372868669401051358701522484473320

############################################### part1 get m
if(0):
for m in range(2,2000):
if(n % m == 1):
li = getbase(n-1,m)
print(m,li)
elif(n % m == 8):
li = getbase(n-8,m)
print(m,li)
m = 19


############################################### part2 find p,q,r
#print(int(n.log(m)))

def find(pl,ql,rl,h):
if(h > 200):
return

mask = m**h
if(pl*ql*rl % mask != n % mask):
return

if(n % pl == 0):
print(pl)
if(n % ql == 0):
print(ql)
if(n % rl == 0):
print(rl)

else:
for i in range(2):
for j in range(2):
for k in range(2):
find(i*19**h+pl,j*19**h+ql,k*19**h+rl,h+1)

#find(2,2,2,0)
p = 3530869780887683268140728773245395170410635845517928489976021534009516369358447511411853596093628646212566313257712207746790503704123439
q = 1274643990900453659882765495755380673132370043776742526720540623203930562265302201405804416214498793910236254519606076438989349099317154363
r = 67086525836865982094673880600810256005961617151283317566078153226175705789594094624822652838168627925461219084263915001883644637391279141


############################################### part3 dec
phi = (p-1)*(q-1)*(r-1)

for i in range(0,321):
e = 19 ** 3 + i - 2
try:
d = inverse(e,phi)
flag = long_to_bytes(int(pow(c,d,n)))
if(b"CCTF" in flag):
print(flag)
except:
continue


#CCTF{nUmb3r5_1N_D!fFerEn7_8As35_4r3_n!cE!?}


RM2(64 solves , 75 pts)

题目描述:

1
The RM2 cryptosystem is a minimalist design that exhibits remarkable resilience, making it exceptionally difficult to compromise.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3

import sys
from Crypto.Util.number import *
from string import *
from random import *
from flag import flag

def die(*args):
pr(*args)
quit()

def pr(*args):
s = " ".join(map(str, args))
sys.stdout.write(s + "\n")
sys.stdout.flush()

def sc():
return sys.stdin.buffer.readline()

def randstr(l):
return ''.join([printable[randint(0, 90)] for _ in range(l)])

def encrypt(msg, p, q):
e = 65537
m1, m2 = msg[:len(msg) >> 1], msg[len(msg) >> 1:]
m1, m2 = bytes_to_long(m1), bytes_to_long(m2)
c1, c2 = pow(m1, e, (p - 1) * (q - 1)), pow(m2, e, (2*p + 1) * (2*q + 1))
return (c1, c2)

def main():
border = "┃"
pr( "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
pr(border, ".: Welcome to RM2 task! Your mission is break our cryptosystem :. ", border)
pr( "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")
nbit, _b = 1024, False
pr(border, f"Please provide your desired {nbit}-bit prime numbers p, q:")
inp = sc().decode()
try:
p, q = [int(_) for _ in inp.split(',')]
if p.bit_length() == q.bit_length() == nbit and isPrime(p) and isPrime(q) and p != q:
_b = True
except:
die(border, f"The input you provided is is not valid!")
if _b:
e, n = 65537, p * q
s = randstr(nbit >> 4).encode()
m = bytes_to_long(s)
assert m < n >> 2
c1, c2 = encrypt(s, p, q)
pr(border, f'c1 = {c1}')
pr(border, f'c2 = {c2}')
pr(border, f'Now, send us the secret string to get the flag: ')
_m = sc().strip()
if _m == s:
die(border, f'Congrats, you got the flag: {flag}')
else:
die(border, f'The secret string is not correct! Bye!!')
else:
die(border, f"Your input does not meet the requirements!!!")

if __name__ == '__main__':
main()

简单来说,题目可以输入两个1024bit的素数p、q,之后会生成一个随机字符串s,将其分为两部分后分别变为整数m1、m2,并计算:

给出c1、c2,如果之后能正确输入s,就可以拿到flag。

两个都可以看做是RSA,所以只要知道模数的全部分解就可以做了。而对于两者来说都可以用一个很简单的方法去构造:

  • 生成素数k
  • 令p=2k+1
  • 检查p和2p+1是否都是素数

如此一来就有:

然后就解RSA就行了。


Soufia(36 solves , 119 pts)

题目描述:

1
The Soufia equation incorporates an undisclosed random oracle function. Can you analyze the equation and infer the unknown function's value?

还是没有附件,和Bada类似,题目内容是:

存在一个未知的函数f,满足:

给出两个点值对:

要求可以对任意的f(x)求出x。

这种函数方程题目一般来说要做以下几个事情:

  • 去掉多层复合
  • 代一些特值

比如对于这个题目的函数f,先令x=0有

然后令y=0,x=y就得到:

作差就可以去掉双层复合,得到:

其实到这里可以看出一点端倪,就是:

这其实也可以看作是:

上式对于任意的y恒成立,也就有:

是一个常函数。而显然当f(x)是线性函数一定满足这个条件,所以直接由题目两点建一个线性的f(x)就可以了。

而说到这个题目就不得不吐槽一下他的靶机,这个靶机从开始到比赛结束,整个队伍里都只有@上辰能连上,发邮件给主办方,他们也没增加这个题目的靶机。@hash-hash在做这个题的时候怎么连怎么挂,非常折磨。


Vantuk(51 solves , 90 pts)

题目描述:

1
The Vantuk equation is complex, designed to conceal flag components, yet it appears deceptively simple on the surface.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env sage

from Crypto.Util.number import *
from flag import flag

def u(a, x, y):
assert a.is_integer() and x.is_rational() and y.is_rational()
return x + Rational((a * x - y)/(x ** 2 + y ** 2))

def v(a, x, y):
assert a.is_integer() and x.is_rational() and y.is_rational()
return y - Rational((x + a * y)/(x ** 2 + y ** 2))

m1 = Integer(bytes_to_long(flag[:len(flag)//2]))
m2 = Integer(bytes_to_long(flag[len(flag)//2:]))
a = Integer(randint(1, 1 << 512))

print(f'A = {u(5, a, 4*a)}')
print(f'U = {u(a, m1, m2)}')
print(f'V = {v(a, m1, m2)}')

output.txt:

1
2
3
A = 6080057478320734754578252336954411086329731226445881868123716230995225973869803901199434606333357820515656618869146654158788168766842914410452961599054518002813068771365518772891986864276289860125347726759503163130747954047189098354503529975642910040243893426023284760560550058749486622149336255123273699589/10166660077500992696786674322778747305573988490459101951030888617339232488971703619809763229396514541455656973227690713112602531083990085142454453827397614
U = 3225614773582213369706292127090052479554140270383744354251548034114969532022146352828696162628127070196943244336606099417210627640399143341122777407316956319347428454301338989662689983156270502206905873768685192940264891098471650041034871787036353839986435/9195042623204647899565271327907071916397082689301388805795886223781949921278129819112624089473306486581983153439866384171645444456400131619437018878598534536108398238424609
V = 1971582892158351181843851788527088806814104010680626247728311504906886858748378948163011806974145871263749452213375101951129675358232283650086419295655854343862361076089682606804214329522917382524296561295274823374483828323983651110722084223144007926678084087/9195042623204647899565271327907071916397082689301388805795886223781949921278129819112624089473306486581983153439866384171645444456400131619437018878598534536108398238424609

题目将flag分为两段m1、m2,并取一个随机数a,之后给出以下几个值:

要求还原flag。

由于A仅仅是关于a的式子,所以利用其分子分母两个方程应该可以解出a来,注意到分子分母可能有经过化简,所以把化简的倍数k也当作变量,去解一个二元方程组:

1
2
3
4
5
6
7
8
9
t1 = 6080057478320734754578252336954411086329731226445881868123716230995225973869803901199434606333357820515656618869146654158788168766842914410452961599054518002813068771365518772891986864276289860125347726759503163130747954047189098354503529975642910040243893426023284760560550058749486622149336255123273699589
t2 = 10166660077500992696786674322778747305573988490459101951030888617339232488971703619809763229396514541455656973227690713112602531083990085142454453827397614
PR.<k,a> = PolynomialRing(ZZ)
f1 = 17*a^2 + 1 - k*t1
f2 = 17*a - k*t2
h = f1.sylvester_matrix(f2, k).det()
res = h.univariate_polynomial().monic().roots()
a = int(res[0][0])
assert a < (1 << 512)

解得a为:

1
598038828088293688046274960163455723857293440615241291237111095137601911115982565871162542905677325967979821954570041947800148887293534420144379636905742

得到a之后的步骤就同理了,只不过变成利用U、V去解关于m1、m2、k1、k2的方程而已。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from Crypto.Util.number import *

######################################################### part1 get a
A1 = 6080057478320734754578252336954411086329731226445881868123716230995225973869803901199434606333357820515656618869146654158788168766842914410452961599054518002813068771365518772891986864276289860125347726759503163130747954047189098354503529975642910040243893426023284760560550058749486622149336255123273699589
A2 = 10166660077500992696786674322778747305573988490459101951030888617339232488971703619809763229396514541455656973227690713112602531083990085142454453827397614
PR.<k,a> = PolynomialRing(ZZ)
f1 = 17*a^2 + 1 - k*A1
f2 = 17*a - k*A2
h = f1.sylvester_matrix(f2, k).det()
res = h.univariate_polynomial().monic().roots()
a = int(res[0][0])
assert a < (1 << 512)


######################################################### part2 get m1,m2
U1 = 3225614773582213369706292127090052479554140270383744354251548034114969532022146352828696162628127070196943244336606099417210627640399143341122777407316956319347428454301338989662689983156270502206905873768685192940264891098471650041034871787036353839986435
U2 = 9195042623204647899565271327907071916397082689301388805795886223781949921278129819112624089473306486581983153439866384171645444456400131619437018878598534536108398238424609
V1 = 1971582892158351181843851788527088806814104010680626247728311504906886858748378948163011806974145871263749452213375101951129675358232283650086419295655854343862361076089682606804214329522917382524296561295274823374483828323983651110722084223144007926678084087
V2 = 9195042623204647899565271327907071916397082689301388805795886223781949921278129819112624089473306486581983153439866384171645444456400131619437018878598534536108398238424609

PR.<k1,k2,m1,m2> = PolynomialRing(ZZ)
f1 = m1*(m1^2+m2^2) + a*m1 - m2 - k1*U1
f2 = m1^2 + m2^2 - k1*U2
f3 = m2*(m1^2+m2^2) - m1 - a*m2 - k2*V1
f4 = m1^2 + m2^2 - k2*V2

g1 = f1.sylvester_matrix(f2, k1).det()
g2 = f3.sylvester_matrix(f4, k2).det()

hm1 = g1.sylvester_matrix(g2, m2).det().univariate_polynomial().monic().roots()
hm2 = g1.sylvester_matrix(g2, m1).det().univariate_polynomial().monic().roots()

print(long_to_bytes(int(hm1[0][0])) + long_to_bytes(int(hm2[0][0])))


#CCTF{d!D_y0U_5oLv3_7HiS_eQu4T!On_wItH_uSing_c0mPlEx_Num8erS!!?}



Hard

Chochol(15 solves , 226 pts)

题目描述:

1
Chochol is an unusual cryptosystem that encrypts messages by blending them within elliptic curves - see if you can crack it.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env sage

from Crypto.Util.number import *
from flag import flag

def keygen(nbit):
while True:
p, q = [getPrime(nbit) for _ in range(2)]
if p % 4 == q % 4 == 3 and p < q:
a = randint(1, p - 1)
Ep, Eq = EllipticCurve(GF(p), [a, 0]), EllipticCurve(GF(q), [a, 0])
try:
Gp, Gq = Ep.lift_x(2024), Eq.lift_x(2024)
s = randint(1, p)
Hp, Hq = s * Gp, s * Gq
pkey = (Gp, Hp, Gq, Hq)
skey = (p, q, s)
return pkey, skey
except:
continue

pkey, skey = keygen(128)
p, q, s = skey
n = p * q
m = bytes_to_long(flag.lstrip(b'CCTF{').rstrip(b'}'))
assert m < n
c = pow(m, s, n)

print(f'Gp = {pkey[0].xy()}')
print(f'Hp = {pkey[1].xy()}')
print(f'Gq = {pkey[2].xy()}')
print(f'Hq = {pkey[3].xy()}')
print(f'c = {c}')

output.txt:

1
2
3
4
5
Gp = (2024, 77522466419731346352388161327659294096)
Hp = (187001239996895215821259450553198409012, 158495938026527642884038157170741730943)
Gq = (2024, 92609909821520263487623088269239797003)
Hq = (191534442430060308634251661645421139195, 102273233384427938890774177710170123915)
c = 15084463560924811262750235394027264639346464192638172940901706702947534963652

题目非常非常的直白,给出两条椭圆曲线:

然后生成随机数s,并给出四个点Gp,Gq,Hp,Hq,满足:

最后把flag进行RSA加密并给出c:

要求还原flag。

要解最后的RSA,不仅要有加密指数s,也需要有n的分解,而我们的数据仅有密文c和四个点Gp,Gq,Hp,Hq,并没有他们所在有限域的信息。所以第一步只能是利用点在曲线上这个信息,去消a或者做一次groebner得到一个p的倍数k1p,同理也就有k2q。由于p、q都只有128bit,所以挂yafu有机会分解出来,又因为两个曲线a相同,所以两个一起挂,谁更快挂出来就可以直接求a,那另一个就可以不用挂了。

得到p、q之后就好办了,对同源熟悉一些的话可以知道p、q模4余3并且是这个曲线方程形式时,这是一条超奇异椭圆曲线,阶为p+1,所以既可以做MOV attack求出s,也可以选取p+1的一些小因子和q+1的一些小因子去Pohlig-Hellman,再crt起来就行。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from Crypto.Util.number import *
from gmpy2 import *
from tqdm import *

Gp = (2024, 77522466419731346352388161327659294096)
Hp = (187001239996895215821259450553198409012, 158495938026527642884038157170741730943)
Gq = (2024, 92609909821520263487623088269239797003)
Hq = (191534442430060308634251661645421139195, 102273233384427938890774177710170123915)
c = 15084463560924811262750235394027264639346464192638172940901706702947534963652

############################# get q
q = 278451262698064898668334196027031252819
PR.<a> = PolynomialRing(Zmod(q))
x3,y3 = Gq[0],Gq[1]
x4,y4 = Hq[0],Hq[1]
f3 = y3^2 - x3^3 - a*x3
f4 = y4^2 - x4^3 - a*x4
qq = 1293030084926293436183849323949823514874151405643244735656543151754612840701244335038404810744976684941536215639890425
q = 278451262698064898668334196027031252819
fac = 28338689773015872412188935645719088445835579
res = Ideal([f3,f4]).groebner_basis()
a = q - 150779601446914247367064450767283168919

############################# get p
x1,y1 = Gp[0],Gp[1]
x2,y2 = Hp[0],Hp[1]
f1 = y1^2 - x1^3 - a*x1
f2 = y2^2 - x2^3 - a*x2
pp = 1408163190634528995462435641953100501680643957675634911266005150804547379301555317411069040554339057760687942005173
p = 243678574849421895808521345944938402807


Ep, Eq = EllipticCurve(GF(p), [a, 0]), EllipticCurve(GF(q), [a, 0])
Gp = Ep(Gp)
Hp = Ep(Hp)
Gq = Eq(Gq)
Hq = Eq(Hq)

############################
if(0):
ordp = Ep.order()
ordq = Eq.order()
print(factor(ordp))
print(factor(ordq))

r2 = 321810878759529563693
sp = (Hp).log(Gp)
print(sp)
sq = (r2*Hq).log(r2*Gq)

modp = Ep.order()
modq = Eq.order() // r2

s = crt([modp,modq] , [sp,sq])[0]
print(s)


s = 172134561985710786674839705035313724070
assert s*Gp == Hp and s*Gq == Hq
tt = GCD(s,(p-1)*(q-1))

d = inverse(s//tt,(p-1)*(q-1))
n = p*q
m16 = pow(c,d,n)


d = inverse(s//6,(p-1)*(q-1))
m = pow(c,d,p*q)
mp = m%p
mq = m%q

R.<x> = PolynomialRing(GF(p))
f = x^6-mp
pp = (f.roots())
R.<x> = PolynomialRing(GF(q))
f = x^6-mq
qq = (f.roots())
for i in pp:
for j in qq:
m = crt([int(i[0]),int(j[0])],[p,q])
try:
print(long_to_bytes(int(m)).decode())
except:
pass


#CCTF{m1X!n9__3cC__&_R54_!}

这个题开始flag值不对,和@yolbby一起做出来之后怎么都交不上。这全是可见字符,语句还有意义,这还能不对?发邮件给主办方之后发现确实是他们设置错了。。。


Imen(19 solves , 194 pts)

题目描述:

1
Imen presents a challenging task involving a novel and creative cryptosystem, inviting you to attempt to break it and obtain the flag.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env sage

import sys
from Crypto.Util.number import *
load('secret.sage')

def die(*args):
pr(*args)
quit()

def pr(*args):
s = " ".join(map(str, args))
sys.stdout.write(s + "\n")
sys.stdout.flush()

def sc():
return sys.stdin.buffer.readline()

def genimen(k, p, _B):
while True:
A = random_matrix(GF(p), k)
for i in range(k):
for j in range(k):
A[i, j] = int(A[i, j]) % (_B + 1)
if det(A) != 0:
return A

def main():
border = "┃"
pr( "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
pr(border, "Hi all, now it's time to solve a new and creative IMEN challenge ", border)
pr(border, "In each step, try to find the unknown permutation to get the flag! ", border)
pr( "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")
nbit, step = 128, 12
for level in range(step):
k, l, p, B = 3, 12 + level, getPrime(nbit), 14 + (level + 1)
nbit = int((k * l) ** B).bit_length() + 5
while True:
A = [genimen(k, p, B) for _ in range(l)]
L = list(range(l))
shuffle(L)
M = prod([A[_] for _ in L])
if check(A, M): break
while True:
pr(f"| Options: \n|\t[G]et {l} matrices \n|\t[P]roduct of matrices \n|\t[S]ubmit the permutation \n|\t[Q]uit")
ans = sc().decode().lower().strip()
if ans == 'g':
for i in range(l):
pr(border, f'{A[i]}')
elif ans == 'p':
pr(border, f'M = {M}')
elif ans == 's':
_p = sc().decode()
try:
_p = [int(_) for _ in _p.split(',')]
except:
die(border, f'Your permutation is not valid!')
if _p == L:
if level == step - 1:
die(border, f'Congratulation! You got the flag: {flag}')
else:
pr(border, f'gj, you got the {level}, try the next level now!')
break
else:
die(border, f'The permutation is not corr3ct! Bye!!')
elif ans == 'q':
die(border, 'Quitting...')
else:
die(border, 'You should select valid choice!')

if __name__ == '__main__':
main()

题目共12轮挑战,每一轮挑战流程是:

  • 生成素数p,大小随轮数增加

  • 生成 $l$ 个模p下的矩阵,个数也随轮数增加:

  • 随机打乱矩阵次序后,计算乘积M:

  • 给出M和按顺序的矩阵列表,要求计算出这个置换

完成全部12轮挑战就可以拿到flag了。

注意到生成矩阵Ai时,除了固定的维数k、随机的素数p以外,还有一个参数是B,他把A中的每个元素限制在了0-B内。生成几组数据可以发现,这样做之后,M其实根本就没有模p,是在ZZ上成立的。

所以思路就很好办,我们枚举A中的所有元素As,去计算:

逆矩阵在ZZ下求即可,可以发现,由于矩阵乘法没有交换律,所以当且仅当枚举到As为Ai的时候,恰好可以消掉,也就是:

所以仍是一个ZZ下的矩阵,而其他的矩阵会导致T不再是ZZ下的矩阵,因此就可以求出置换了。(因为ZZ并不是一个域,里面除去0的元素并不都有乘法逆元,所以求逆会出现分数)

可以用下面的脚本简单验证这个思路的正确性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from Crypto.Util.number import *

def genimen(k, p, _B):
while True:
A = random_matrix(GF(p), k)
for i in range(k):
for j in range(k):
A[i, j] = int(A[i, j]) % (_B + 1)
if det(A) != 0:
return A

nbit, step = 128, 12
for level in range(step):
k, l, p, B = 3, 12 + level, getPrime(nbit), 14 + (level + 1)
nbit = int((k * l) ** B).bit_length() + 5
while True:
A = [genimen(k, p, B) for _ in range(l)]
L = list(range(l))
M = prod([A[_] for _ in L])
break

print(f"Round {level}:")
print(Matrix(ZZ,A[0])^(-1)*Matrix(ZZ,M))
print(Matrix(ZZ,A[1])^(-1)*Matrix(ZZ,M))

这个题目在没有update之前,好像最后几轮矩阵的值会超过p,可能就不太好做了。还有这个题目的输入输出处理更是依托。


Latifa(22 solves , 174 pts)

题目描述:

1
Latifa is a tricky yet enjoyable challenge, akin to solving a puzzle in a dream-like state.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env sage

from Crypto.Util.number import *
from secret import b, g, flag

def encrypt(m, b, g):
i, c = 0, 0
while True:
N, D = b * g, n(pow(g, Rational(2*i/m)), prec = b) + g
s = N/D
c += s
i += 1
if i >= m:
return c

m = bytes_to_long(flag.lstrip(b'CCTF{').rstrip(b'}'))
c = encrypt(m, b, g)
# Takes many many years :(
print(f'c = {c}')

output.txt:

1
c = 1.802740943639362458394687428102524270510281665315000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e47

6.9的凌晨六点左右,我和@yolbby做完Chochol之后怎么都交不上flag(原因就是上面说的,主办方flag设置错了)。然后他说他去看点别的,就去看了这个Latifa。

结果10分钟左右他就做完回来了:

image-20240701095249427

image-20240701095316721

然后就好奇地来看了一下这个题目,他用未知的b、g,对flag进行了如下加密:

把上下的g约一下得到:

由于这学期刚好选修了一门电子信息数学基础,重温了下高数,所以看到这个想到了定积分,但是既然十分钟就能搞定,应该不是这么做的吧?

首先可以发现c最后的值一定包含因子b,所以先查一下c的所有因子组合找一下b。但是根本不知道怎么判断b正不正确,所以打印了一下long_to_bytes,结果就出了……

我也很懵,看了下yolbby写在文档的东西也是这么做的,等他期末考完问问他当时做这个是怎么想。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from Crypto.Util.number import *
from itertools import *

a = [3,5,379,39719,8959787,891059823255825019255461632769683]

for t in range(1,7):
for i in combinations(a,t):
l = prod(i)
try:
print(long_to_bytes(int(l)).decode())
except:
pass

#CCTF{h4Lf_7UrN_5ym3Try!}


O7R(23 solves , 169 pts)

题目描述:

1
The O7R cryptosystem combines RSA key recovery techniques with a 7-segment digit representation, resulting in a unique and robust design.

题目给的是pdf文件,核心一点的部分是:

image-20240701103055895

image-20240701103126855

数据:

image-20240701103144061

image-20240701103159097

image-20240701103349848

先阐述下题目内容,题目给出了用七段数码管表示的p、q、n、n^2、c等五个数值,他们满足:

  • c是没有损坏的(也就是c的所有数字都完好)
  • 剩余的四个数值,都是经过了损坏的,损坏的含义是:每个七段数码管表示的数字,有50%的概率爆掉随机一根数码管

要求还原flag。

首先明确这个损坏的含义,50%的概率爆掉随机一根数码管,也就是说一个数字可能根本不会坏,就算损坏了,也最多是一根数码管不亮。所以对于其中每一个数字,他的可能性都是有限的,比如举几个例子:

image-20240701103950864

这个可能是5、6或9。

image-20240701104026332

这个可能是1、7。

image-20240701104109464

这个只能是4,因为最多掉一根管,他补任何一个地方都不会成为其他数字。

这么看来的话,只需要把每个数字的可能性都列出来,然后深搜剪枝就可以了。由于每个数字的可能性都不多(最多也就是上面这个5的情况,共3种),所以其实完全不需要n^2,直接用pq=n来剪枝就行。

思路很清晰,但是怎么样做才能方便地把每个数字的可能性都列出来?和@yolbby合计了一下,与其切分图片提像素然后分类,不如直接肉眼一个一个写,我写p、他写q,然后再分行写n。为了统一,把可能性写成二重列表的形式如下(用p的前几位举例):

1
[[7],[3],[2],[6,8],...]

折磨就这么开始了,凌晨的时候,两个人盯着电脑屏幕老眼昏花的写,而且每个位置还要仔细地去想所有的可能性,因为漏了就很麻烦。

然后又拉着@Zima一起折磨,好不容易把p、q、n全部写完了,一剪发现连10位都剪不出来,说明最后十位就有错。然后三个人又一起debug。debug老半天终于把p、q剪出来了,然后发现好像还没有弄c,不过c挺好办,因为没有损坏,但是直接OCR识别出来的还是不少错(特别容易漏1),然后又是一起肉眼debug才把c弄出来。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from Crypto.Util.number import *

n = [["5","6","9"],["1","7"],["8"],["4"],["0","8"],["6"],["5","6","9"],["3"],["7"],["6","8"],["6","8"],["1","7"],["5","3"],["5"],["2"],["5"],["7"],["5","6","9"],["5","6","9"],["5","6","9"],["7"],["0","8"],["5"],["7"],["6","8"],["4"],["1"],["1"],["0","8"],["5","6","9"],["0","8"],["6"],["5"],["4"],["6","8"],["1","7"],["1"],["8"],["6","8"],["7"],
["6","8"],["0","8"],["8"],["5","6","9"],["7"],["0"],["8"],["0","8"],["0","8"],["4"],["0","8"],["1","7"],["3"],["4"],["3"],["5","6","9"],["0","6"],["0","8"],["2"],["8","9"],["7"],["0"],["2"],["2"],["0","8"],["9"],["2"],["6"],["2"],["5","6","9"],["5"],["2"],["8"],["3"],["8"],["1"],["6","8"],["8"],["7"],["8","9"],
["8"],["2"],["1"],["0","8"],["5"],["0","8"],["4"],["2"],["6","8"],["1"],["1","7"],["5","6","9"],["4"],["4"],["2"],["6","8"],["5"],["1","7"],["8"],["0","8"],["0","8"],["2"],["9"],["4"],["7"],["1"],["4"],["3","5"],["1","7"],["8"],["0","8"],["0","8"],["2"],["9"],["4"],["7"],["1"],["4"],["3","5"],["8","9"],["6","8"],["5","6","9"],["4"],["3"],["3"],["8"],["7"],["9"],["3"],["6","8"],["8","9"],
["0","9"],["2"],["3","9"],["6","8"],["7"],["3","9"],["8","9"],["5","6","9"],["7"],["8"],["3","9"],["2"],["5","6","9"],["6"],["8"],["1","7"],["0","8"],["0","8"],["0","9"],["9"],["6","8"],["4"],["3","5"],["5"],["5"],["2"],["2"],["3","5"],["3"],["4"],["4"],["1"],["1","7"],["4"],["7"],["5","6","9"],["8"],["1","7"],["3"],["5"],
["8"],["7"],["7"],["1"],["8"],["5"],["8","9"],["5","6","9"],["4"],["0","9"],["0","8"],["1","7"],["8"],["5","3"],["3","9"],["7"],["6","8"],["8","9"],["2","3"],["4"],["7"],["3"],["5"],["0","8"],["6"],["7"],["1"],["7"],["4"],["5"],["1"],["8"],["5"],["2"],["0","6"],["4"],["8","9"],["8","9"],["2","3"],["5","6","9"],
["1","7"],["3","5"],["7"],["0","6"],["6","8"],["3"],["1","7"],["7"],["6"],["6","8"],["0","6"],["4"],["8"],["5"],["4"],["1","7"],["2"],["2"],["3"],["6","8"],["5"],["7"],["8"],["8","9"],["6","8"],["3","5"],["4"],["0"],["0","8"],["2"],["8"],["8"],["8"],["1","7"],["3","5"],["4"],["1","7"],["4"],["0","6"],["8"],
["8"],["8"],["4"],["8","9"],["0","8"],["5","6","9"],["5","6","9"],["2","3"],["5"],["0","8"],["0","8"],["2"],["6","8"],["0"],["8","9"],["5","6","9"],["5","6","9"],["5","6","9"],["1","7"],["1"],["6","8"],["7"],["8"],["0","8"],["1","7"],["6","8"],["7"],["3"],["8"],["2"],["8","9"],["1","7"],["3","5"],["4"],["5","6","9"],["7"],["5","6","9"],["8","9"],["3","5"],["1","7"],
["6"],["3","9"],["5","6","9"],["1","7"],["2"],["1"],["8","9"],["4"],["2"],["4"],["7"],["8"],["2","3"],["4"],["0","6"],["1","7"],["7"],["4"],["0","6"],["7"],["6","8"],["3","9"],["7"],["3","9"],["1","7"],["2"],["7"],["3"]]

p = [["7"],["3"],["2"],["6","8"],["6"],["7"],["3"],["9"],["7"],["6"],["7"],["7"],["5"],["6","8"],["7"],["7"],["4"],["0"],["3"],["0","6"],["1"],["5","6","9"],["9"],["0","8"],["2"],["8","9"],["5","6","9"],["2"],["7"],["7"],["5","6","9"],["7"],["8"],["6","8"],["0","8"],["0","8"],["3","9"],["7"],["8"],["0"],
["6"],["8","9"],["6","8"],["8"],["5","6","9"],["4"],["3","9"],["5"],["1","7"],["8"],["1","7"],["0","9"],["1","7"],["1","7"],["3","9"],["8","9"],["6"],["8","9"],["5"],["2"],["0","8"],["6","8"],["0","8"],["3","9"],["5","6","9"],["5"],["8","9"],["5","6","9"],["2","3"],["5","3"],["5"],["3"],["0","8"],["6"],["1","7"],["8"],["3","9"],["8"],["2"],["6"],
["3","5"],["1","7"],["8"],["6","8"],["6"],["6"],["3","9"],["2"],["3","9"],["4"],["1","7"],["8","9"],["5","6","9"],["8"],["3","5"],["8"],["8"],["5"],["7"],["0","8"],["4"],["8"],["8"],["5","6","9"],["4"],["3"],["0"],["1","7"],["1"],["6"],["3"],["8"],["0","8"],["2"],["7"],["0","6"],["7"],["2"],["6"],["3"],
["4"],["0","8"],["6","8"],["7"],["2","3"],["8","9"],["7"],["3"],["6"],["1"],["1"],["3"],["2"],["7"],["8","9"],["5","6","9"],["2"],["8"],["4"],["1","7"],["2"],["7"],["6","8"],["4"],["8"],["9"],["5","6","9"],["4"],["8"],["1","7"],["4"],["5","6","9"],["8","9"],["7"]]


q = [["7"],["0","8"],["7"],["5","6","9"],["6","8"],["0"],["5"],["3","9"],["7"],["4"],["3","9"],["4"],["3","9"],["3","9"],["1","7"],["0"],["3","9"],["9","8"],["3","9"],["6","8"],["5","6","9"],["0"],["1","7"],["3"],["6","8"],["8"],["4"],["1","7"],["1","7"],["8"],["7"],["7"],["2"],["1","7"],["5","6","9"],["8"],["7"],["5","6","9"],["1"],["1"],
["1","7"],["8"],["4"],["6","8"],["4"],["1"],["0","8"],["2","3"],["3","9"],["4"],["2"],["3","9"],["1","7"],["9","8"],["9"],["3"],["5","6","9"],["5","6","9"],["5","6","9"],["2"],["3","9"],["4"],["2"],["9","8"],["0","8"],["4"],["4"],["3","5"],["8"],["1"],["8"],["1"],["0","9"],["6"],["3","9"],["0","8"],["8"],["4"],["2"],["0","6"],
["0","8"],["8"],["6","8"],["3","9"],["1"],["5","6","9"],["1","7"],["2"],["0","9"],["3","9"],["8"],["7"],["6","0"],["0","9"],["6","8"],["6","8"],["6","8"],["1","7"],["5","6","9"],["5","6","9"],["5","6","9"],["3","9"],["3","9"],["8"],["2"],["7"],["3"],["8","9"],["5"],["6","8"],["6","8"],["2"],["4"],["2"],["5","6","9"],["7"],["9","8"],["9","8"],["6"],["0","8"],
["7"],["4"],["6"],["4"],["2"],["3","9"],["7"],["1"],["6"],["8"],["5"],["5","3"],["7"],["0","8"],["3","9"],["8","9"],["4"],["6","8"],["3","9"],["4"],["6","8"],["1","7"],["9"],["4"],["0","8"],["9","8"],["7"],["3"],["8"],["0"],["6","8"],["7"],["0","8"],["9"]]

P = p[::-1]
Q = q[::-1]
N = n[::-1]

def find(pl,ql,nl,h):
p_pos = P[h-1]
q_pos = Q[h-1]
n_pos = N[h-1]

mask = 10**h
if(h == len(Q)):
print(int(pl))
print(int(ql))
return

for i in p_pos:
for j in q_pos:
for k in n_pos:
ppl = int(i + pl)
qql = int(j + ql)
nnl = int(k + nl)
if(ppl*qql % mask == nnl % mask):
find(i+pl,j+ql,k+nl,h+1)

#find("","","",1)

p = 7326673976775677403615902952775786009780696864351810113969520603559523530618382637866632341958388570488543071638027072634087397361132795284727648994814597
q = 7075605374343310393660736847187727587911784641033429799366923429044581810690842608691612098700666156539827395662425799607464237168537039463467940973806709
n = p*q
c = 3562663791006368034141977288433903435480729754256243271724528203580472770993094486280368310543613382256743525891807443983661459665243901555404810179525594817342115941001759547407703653781422057871347356603203075709275979343680289210665741824992476238299997444201948575831891922291318008008982215564064059476

d = inverse(65537,(p-1)*(q-1))
flag = long_to_bytes(pow(c,d,n))
if(b"CCTF") in flag:
print(flag)


#CCTF{5eVEn_S39Men7_RSA_w!Th_k3Y_rEc0v3Ry!!!}

痛苦。TT


Solmaz(11 solves , 271 pts)

题目描述:

1
Solmaz has developed a simple and visually appealing cryptosystem based on Elliptic Curve Cryptography, but its potential vulnerabilities require further investigation.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env sage

from Crypto.Util.number import *
from flag import flag

def gen_param(nbit):
while True:
t = prod([getPrime(nbit >> 3) for _ in range(4)])
p = 4 * t ** 2 + 1
if is_prime(p):
c = randint(3, p - 3) ** 2 % p
E = EllipticCurve(GF(p), [c, 0])
if E.order() == p - 1:
return p, c

def encrypt(m, x, p, c):
E = EllipticCurve(GF(p), [c, 0])
while True:
try:
P = E.lift_x(x)
break
except:
x += 1
assert m < p - 1
Q = m * P
return P, Q, m

nbit, x = 256, 1337
m = bytes_to_long(flag.lstrip(b'CCTF{').rstrip(b'}'))
p, c = gen_param(nbit)
P, Q, m = encrypt(m, x, p, c)

print(f'P = {P.x(), P.y()}')
print(f'Q = {Q.x(), Q.y()}')

output.txt:

1
2
P = (1338, 9218578132576071095213927906233283616907115389852794510465118810355739314264)
Q = (3454561753909947353764378180794889923919743476068813953002808647958908878895, 17267599534808803050751150297274989016063324917454246976792837120400888025519)

题目生成4个32bit的素数,记为q1、q2、q3、q4,然后计算:

然后取随机数c,得到模p下的椭圆曲线,满足阶为p-1:

由于p模4余1,所以这个构造和前面的Chochol又不一样,虽然形式相同,但并不是超奇异的,所以才能使阶为p-1

之后给出曲线上的两个点P、Q,满足Q=mP,要求还原m。

依然没有模数p和c,所以同样做groebner得到一个p的倍数kp,然后由于p-1显然是个光滑数,所以想到利用Pollard p-1来分解他。

但是虽然说确实光滑,但是32bit的素数好像还是稍微大了一些,但是之前在maple博客有一篇文章看到过一个叫gmp-ecm的工具好像可以用:

N1CTF 2022 Writeups | 廢文集中區 (maple3142.net)

然后就在队伍里问了问有没有师傅用过,@Zima@leukocyte试了一下好像不太行,可能是因为每个素因子都平方了一下的缘故。

所以最后还是想着直接写一个p-1光滑的分解脚本,回顾一下Pollard p-1的原理,其实就是构造一个t,使得:

那么由于费马小定理,就有:

所以可以求:

从而得到p。

关键的一步就是怎么构造出这样的t,由于:

所以只要从4开始,乘上所有32bit素因子的平方,这样的t就一定是p-1的倍数。

而要遍历所有32bit的因子,对于python而言有点大了,所以想的是用C++的NTL库去做这个事情。但配这个环境好像实在有点麻烦,所以交给了Zima。Zima提示说gmpy2的nextprime也许可以,所以我也就写了个python的脚本挂着,结果挂了一小时左右真的出了分解。

分解完就好做了,由于保证了阶为p-1,p-1也光滑,所以直接求DLP就可以。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from Crypto.Util.number import *
from gmpy2 import *
from tqdm import *

P = (1338, 9218578132576071095213927906233283616907115389852794510465118810355739314264)
Q = (3454561753909947353764378180794889923919743476068813953002808647958908878895, 17267599534808803050751150297274989016063324917454246976792837120400888025519)

x1,y1 = P[0],P[1]
x2,y2 = Q[0],Q[1]
PR.<c,t> = PolynomialRing(ZZ)
f1 = y1^2 - x1^3 - c*x1
f2 = y2^2 - x2^3 - c*x2
res = Ideal([f1,f2]).groebner_basis()
print(res)

p = 55454940004513276799611588380059302189664933020838413515384708243928267219228512844352832003082349090934777517286737687281070260517892560156083453894086877367076953177914949876201881341274104406095268673060476748059522884381040212
p = 1846015660040154116831101677935731707303583319663256520946194832443059845383265967606382443176439867135351202904588407039443283680086981350429332914636635325121016081809085910600024068461413790906246868559

if(0):
i = next_prime(2^31)
a = 2^4
while(1):
a = powmod(a,i^2,p)
if(gcd(a-1,p) != 1):
print(gcd(a-1,p))
break
i = next_prime(i)


pp = 30126567747372029007183424263223733382328264316268541293679065617875255137317
PR.<c,t> = PolynomialRing(Zmod(pp))
f1 = y1^2 - x1^3 - c*x1
f2 = y2^2 - x2^3 - c*x2
res = Ideal([f1,f2]).groebner_basis()
print(res)

c = pp - 12551276475019101031129136087416454095594035188486953048519662649803219581851
E = EllipticCurve(GF(pp), [c, 0])
P = E(P)
Q = E(Q)
m = Q.log(P)

print(long_to_bytes(int(m)))

#3cC_d1ViSibil!7Y

这个题也很扯,开始的附件里求完m发现不对,等了一会儿发现果然又要redownload。


Tesvir(27 solves , 150 pts)

题目描述:

1
Tesvir, an advanced and unbreakable cryptosystem, emerged as a revolutionary development in the 20th century cryptographic landscape.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env sage

from Crypto.Util.number import *
from flag import flag

# Note: The provided code is unclean and lacks efficient performance!

def gen_params(p, h):
F = GF(p ** h)
R = PolynomialRing(GF(p), 'x')
P = Permutations(p)
r = P.random_element()

while True:
f = R.random_element(degree = h)
if f.is_irreducible():
f *= f.list()[-1] ** -1
break

g = F.gen()
while True:
_ = randint(1, p ** h - 1)
if gcd(_, p ** h - 1) == 1:
g = g ** _
break

RR.<x> = GF(p**h)[]
f = RR(f)
t = f.roots()[0][0]
return g, t, r

def genkey(p, h, params):
g, t, r = params
pubkey, d = [], randint(1, p ** h - 1)
P = Permutations(p)
pi = P.random_element()
for _ in range(p):
b = discrete_log(t + r[pi[_] - 1] - 1, g)
c = (b + d) % (p ** h - 1)
pubkey.append(c)
privkey = (d, pi, t, g)
return pubkey, privkey

def encode(msg, p, h):
m = bytes_to_long(msg)
B = bin(m)[2:].zfill(8 * len(msg))
BA = [B[i*h:(i+1)*h] for i in range(len(B) // h)]
M = []
for ba in BA:
m = ba.ljust(p, '0')
m = m[:-h + m.count('1')] + '1' * (h - m.count('1'))
M.append(m)
return M

def encrypt(msg, pubkey, p, h):
M, C = encode(msg, p, h), []
for m in M:
s = 0
for i in range(p):
s += int(m[i]) * pubkey[i]
C.append(s % (p ** h - 1))
return C

p, h = 223, 24
params = gen_params(p, h)
g, t, r = params
pubkey, _ = genkey(p, h, params)
enc = encrypt(flag, pubkey, p, h)

print(f'r = {r}')
print(f'pubkey = {pubkey}')
print(f'enc = {enc}')

输出也有点长,不放在这里了。

先梳理一下题目内容。首先是gen_params:

  • 生成有限域:
  • 生成一个1-223的随机置换r
  • 生成度为24的不可约多项式f
  • 生成F的生成元g
  • 把f转到F下并求出根t

然后是genkey:

  • 生成一个随机数d

  • 再生成一个1-223的随机置换pi

  • 对0-223的j,分别计算bj,满足:

  • 计算cj,满足:

  • 所有cj作为公钥

说实话,看到这里真的一头雾水,gen_params的内容以及这个223让我联想到之前学了一点点的Reed Solomon Code,但是本身就学了点皮毛,完全不会用。然后这个genkey也一点没看出要干嘛。

总之先继续往下,还剩一个encode和encrypt,具体来说encode是:

  • 把明文按24bit分成若干组,分别进行加密
  • 对于每个分组m,填充0使其长度为223
  • 之后,从末尾开始将0换成1,使得最终的m一共含有24个1

最后是encrypt:

  • 对于每个m,计算向量m和C在模p^h-1下的内积

看完发现这不就是个背包吗,而且由于每个m其实都只由24bit生成,一一对应,所以直接爆破就好了,也就是对所有24bit的m,计算最后的密文值来建立字典,然后查字典就行。

又因为m只能是可见字符,所以只需要用可见字符建字典就够了。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from Crypto.Util.number import *
from itertools import *
from tqdm import *
from string import printable

r = [115, 149, 46, 15, 122, 10, 81, 136, 65, 166, 219, 156, 129, 170, 214, 210, 223, 21, 179, 72, 67, 85, 220, 183, 5, 212, 134, 106, 139, 188, 146, 102, 125, 22, 221, 87, 126, 51, 153, 159, 116, 32, 144, 205, 55, 127, 222, 93, 71, 201, 20, 75, 123, 41, 25, 33, 114, 192, 120, 70, 96, 211, 8, 180, 26, 147, 28, 34, 177, 199, 92, 11, 198, 29, 89, 37, 74, 162, 133, 97, 79, 69, 35, 80, 124, 101, 216, 61, 117, 173, 176, 53, 98, 150, 4, 42, 39, 56, 112, 195, 38, 218, 178, 157, 19, 206, 172, 181, 17, 62, 103, 182, 45, 13, 161, 171, 175, 95, 148, 217, 193, 187, 66, 90, 104, 24, 76, 60, 108, 6, 131, 196, 128, 184, 163, 137, 105, 189, 86, 174, 91, 64, 57, 118, 77, 140, 190, 194, 200, 43, 16, 44, 30, 207, 94, 9, 68, 135, 169, 204, 143, 160, 100, 23, 191, 113, 111, 185, 213, 58, 130, 197, 59, 40, 186, 54, 52, 12, 18, 132, 83, 82, 202, 3, 141, 2, 1, 165, 14, 63, 84, 109, 152, 155, 36, 7, 138, 154, 49, 78, 168, 73, 151, 50, 209, 158, 119, 110, 145, 88, 47, 203, 142, 31, 208, 27, 121, 164, 167, 48, 107, 215, 99]
pubkey = [4579387258479099442826389140450017183599598717619029205, 50797264141313161541250199298799563140155742676514856070, 219434691099250615018456858310069756006004009383046642143, 11468330660842953225747497191767883930924930581150665808, 42194638083287011574373249814732023116819279406149017814, 134902545392443293573158976620222083109565710059582776902, 3625160144836978085583648268825293444860869777878716136, 166813383914408395764151023241147502314266684547884287332, 29943565577633575933303332141205158945650619101645934495, 17247055180775323745936195042907464159475314878330706940, 132701774141813613088531286364104917554398118755301161215, 206201625677033303772404919447115617309491897222468899200, 128759746088591221813929603681654308197557770578218948759, 183360234828977645180158181331005804200611579381078449242, 3433497919764270981254123021568517019107322906407838775, 180827244701547409769187849611685260472147977186544075612, 90094342976044807455290765016453082784682254114643836710, 140026255208288075756758769477351349475202860424951564344, 44112364626789590294371548773210573275126865083833640129, 151820161852264758050222465177459785347971580873896530205, 222641568922329016629261535558799809898506907527486806640, 35846272444624180096743012590749051207738305050375026589, 110125275231759529474921195973993669124331074146968153872, 11904056771999183668735243652491014687752264483508519866, 119911867667253049327009237856594634409002462712397844820, 161987059546037388210491693488518594653631887673204452021, 190788561858532711625259038792473501768790789526603183037, 171042759190856745199182923448465366839589623183777310937, 101801200452544922434731443664964969246088222257527659764, 140090724075803428561186326582496226733538620993806652027, 176639396674357206316302178630628292675252712623699685490, 224783685350046016548546283210616077635030923321729671618, 157686979388371420253523386799757123908864516455322611545, 28199419400037879567825862396075374687069854583804945245, 171978417782412325892218086069748164824402244074965942435, 150310833853533773405964930253939487392079866821627398318, 98831345742413826709455556687993160848501023377907278651, 154123567141389311086541742847650387097698934736160865023, 121630066975031002990840926074186080216239975029293476812, 31286696313154799066012880007327124505421721519900715948, 101968393996191205796932269707677612467402575382356961248, 2499136180549370600650171447598502321445026784559416298, 16848125897903955338136140634369505392045684150115786655, 47578327339400295189131054328443796093121978922647143447, 24179233030291902452310675469039369826988576939199879176, 105273037901112446462027934415037990047326123775284256167, 57411134139938220106040243797683715192527950831820732678, 10195725519763357104221976240106299814337949772923615115, 58166560063884761465783400200196628538821017285137875256, 54605784905038131389770213608353236551670620624534944596, 38226456600614157062858559660172787082020565146605850918, 197663869365124319457839210919740591516696780039282126370, 54967216556218275683218087499570854735333195406411928855, 35042906559524305301502230096462527221023815082011655179, 171952564704992857457616532638790008890189941636196696639, 13591589636081003740574689687993869647237413563256792156, 57500559744886027958431694115198596269754572013078937199, 156436123898613669232291346505787899872553219088301314205, 57806240192751898104001203282509821193881347139984859692, 75418944937404633800905243484214892003346945987666650321, 217951708406473977895295349132268568429989686999505522686, 176304391015211148283705335593955617239819817843077950051, 55758125847440048397087822777244647091332565999126988186, 67763984529554201139494025200729662864492800619660213727, 191656222762556273307219102885834144624172791167272826527, 227057602273122755633874913712839974408374157556787076376, 33453697550484714961091977965265482703240184817695545931, 12925259442356937434080842924124428578042373548580641181, 129261341826868448249871227617413161190310094555223712007, 71767590488420142133048641448102466710457459547821693469, 103075493305044879489908640369142480134843220688626837743, 12297364715802489173873556831742255265347989101674609502, 150291384287610485111409155980244572563593549745010253623, 33554524283172661611961357763141315669196684041832264667, 107585516815880888762810129132766424476978665361986789693, 36898755542881194892338867205127165804165674781364624017, 138477491659893664966890466537007728539785384371165227574, 65080684456472796307268797791487491498452724689891503205, 54048070387499035173896610221943137253486493038088391758, 173153561765864340705771824025973165939007109224817507817, 99568762534450608460985163776495428624938259974357002753, 108073734079270345964706401004242426736899095097270034663, 211296070795991107573611899784372810320165661065744807048, 191950980282295846436480586220709004688220897961209478055, 219870789829809143290238261647242433619723780668567898644, 82846254389400741644821834447511166580591663882000899601, 147335705565643026701833336777988683175003463278814922435, 6677330657331868597177080912636187709184247213001090915, 65509954025264162376897683857059640795180443611539638608, 97788348508348672587427253412621341354516347030316943263, 50339872017921024171565922882962110596867515107807632791, 20404897329900652246085973695039579378539236291786309377, 121628627709837892062339399628512721395766452440642156526, 117664673632829519874589853389281010878033806284806858518, 3494726644577971223092642563152500568596276988795464645, 223190741523925264571455759912144998793891033522018823048, 124704306247788182879460388204439451710110862604146656179, 179566098748276203619721337178633709517431960525191942726, 173695293570764259997115035606911185447598999607810255972, 98378718420486206578551644649570093764645689842230247773, 36294885445792353105870401865114008348319522604135751238, 88351601718747228918743630494029404621883715463441833300, 136753888328355936111533885450395887852887609650952850511, 88826423819149324073976027277400003130267101605703817417, 153626347770151869159701273708416238028320992726042173456, 48447665799548951912445970765509796863255819192369137317, 25926148818727084305421258039188486622091968754727603495, 75433007506007981941697739028886717357568843374886617854, 163403523743183107337343618869709094774134312669607579337, 90195006643240793119903093662362093956207796050824877543, 76931655268596842019773587962860622955093248369876788943, 167125463295542690682664785803694224887085948995189999120, 165912236533045749391666265104352059105394417883439144504, 102981866240314860142794639939500562112030796419343035128, 123694742410185960631033448288250064254819985873351326165, 146592629552828995525189097760808351453961284758262774466, 203019861676583573314131259559172588655975662889495156847, 10036161500498212898467897368941308147748449303016069174, 87685737716753006215675653796273463223114323170147465929, 70329560627252087252573684487639836859071522052119307709, 136915861244063044189262984270735166984429581745567649014, 73329998651742245723573422155349760954316509103924762273, 113734525875359728198291451178172485335376151605520663473, 208813187221334309596716858611421084130040781080505004952, 103875031262332837491631581905413234953377770241528241376, 13243847546817072175843419982531752763684782213514268707, 120001744407132173260771636243615527375774917295121354322, 89851436867267737223022839170104811357555399505302690249, 19106651045250061786958853204442871931070349664665906834, 208140981493299248238118954460510449680605048319117899909, 120436526754234062175517049489497536862368615535960465554, 41291126508960073783277550372758414648175366207183334071, 2749944545805905720645531699140358480207253897204188026, 152081380532436580972673620869155127882780286091923495345, 100852343092400579536924309146452168877906896247239016614, 72079920508055254975205188574206595876645959712620597055, 64522268400191343447596383230061887992830219982859913739, 152197766648262372890520836531877829770805996935738924903, 210877734181002617812004654242222645876962178158868141209, 24683946164990045540189560349129683887167164299195625775, 206820209167550683642789721762915236268108999576680456003, 57710866387091758853441183389265772964358226230824623991, 179747783113998854176536062668891588251038080701285687124, 111115053514467266934252222133378536223568883659999381505, 194310031249253577037158724020305219028469221832006495335, 54171305756709974148893255631810739380658440555289725183, 36081061592476397164849907656781564201428054975112151104, 166387522179865867529326169813325590271108420005029369186, 50236753450199839053294465580343603952154218305737501432, 70229038648249274204999881080972889501721690256998625762, 169828815141066923955361893760454462461324854178070409119, 26513302826439642958218716242634740776783823741312689484, 185293148966236856497840087050705776290599263752404971816, 210075369853066586284316297419359553539776027083874134267, 41205384335146937893146509772611998036936514483031810977, 204592520934960087493828781217416383428211029411383214329, 76065261425709174633836852053941128687067031775700625939, 203179613168733078332953358441061351071432612584054415916, 66513708250927489690305470142866176469513954748228766009, 20175814755308115791665314547889139205636005255804299913, 41078990455149658170563497862994645535886142813093891945, 228716072689925880543313659773455487756692019817537192658, 88391808755246018940367365181350246171557051014871850843, 139071342756333141894701291100940798956239314294905885889, 34147579027293395946839437099948813145351207426978539631, 104582557632522499285198546400846355094887074031948009088, 193859720380076021440874403745137391037512325878308523794, 225121265357955697363133178349606491945064525800785037545, 160720585717566128665444228898129797771581562781360670661, 195166070960955730159221227940352776779707446869688343508, 133627947376034537976383882910801869861246650572569070695, 16719370811161879662359147931457873453820872914250387809, 155580990893469680143665368454873188094047243027201936841, 18694763141354823690068972812866289937107702217472736692, 180898634569544925760572911334766030238281261919118297851, 159751985510351400082345385715475564587105850170299678853, 155124615247547146942168765500966248268323206179567419205, 39649641204773451404479536238969341384057353458256712174, 216313168003657711664578876360545799473306544830810266276, 2436503838732292885729786856280366925839569007022405596, 29242869292273221923479959280952424708462203556781794240, 21353805727988428836254728455321385646191528588118693349, 184613959938479515383397207452089562397995531746818305735, 140289186800524865867557270107130302338585963835219455920, 14428539381784887761791822212123106533195544480287429965, 40063122489718320389768387980387937974266804567069179915, 55679889320878386368056199019499075551252069356762025349, 228364327140445947910507206533691038804961948329604730314, 137306250863214464757975768638682609074020140712052016591, 152391609702632678722030482025816660799366940635319107692, 194331603613786615373811392732597540342727111461284467597, 112113260165766031933616735889437144400364790129165745472, 164499490550482070591340758879746189249388690835705287235, 20402415569195842441186230361383311044896719963531571028, 225143804839005659587542376622296525537292745802892642188, 219943480713442124639466865807923792140876225471303796193, 100202994621560168061154249542004856938325751300523918584, 166436226032756440467049814076007698369014463998417351566, 18518355241335588006356643517380924818275705817097834578, 169604362625557928746484454468713671095324100635870987551, 159533814543444317784990508257554461271048535710886251037, 133643088722739646000296360061097750513592416981320784101, 25825717351397349919398666003417921265219251686879820730, 103890888915605250679479877052313405985439478970128423397, 175772907838636899849299085810673621817614674781130017660, 43252300614589311522387147190442713053937984366326355858, 16873508457113580715891642673658667475105208209712112109, 204936077584769128504523195223183609999978573880209766347, 146181990984131617454963783388766051702152117818499103400, 226800051676747490358957570441842948948454336820531603559, 13156759590500952498608314082303404240995496109216133020, 162761766626445741079367215153670009011553941246752760271, 123445831125323393499253996280041267200348094380668843306, 121602035722307931614892071798221758823002664577511063923, 23269715190123797016604529624307223622512790793866100396, 201076240655311730254977907588618994829092140461549116438, 186737762068262742773830024339577470536847993539671779909, 64769358039037097203493324824901293008710108086262539570, 76036556626637280604159891162317494917916766168683677624, 122041751030855798908591443027132885210134390844715030960, 6420731920346907316661151593704807667310013944904682276, 133261601326695897123023151238223660543182011673252011668, 30312418798752835863188687757298737341991779619884584800]
enc = [121773698579173930646656942313646818799949564181892514859, 110363069068565613417175306995877029091942876457783351430, 67317447763346252995608047282163289259952724379009239066, 40661215922494279426540413963914475266467025509037412483, 199660192385826369347118319254502285195924547332922588842, 210938210594770518313394647530370135516757130870441793616, 164968164223840366829073701035700449613620360889464719358, 47722780186684500642502897705647627086520536818326027744, 172004040329747663442384678364642020997081017719965572563, 168534236138473024418565955930918724937733991774575045958, 10055390150692383401863041865436699229887022636104189218, 192916357157635434222013845041170212448835303838788802734, 68753529179558868929323016763964628261555073720614388332, 23143793866551418276708722027637360095751061589410241996, 12858601314274634932275239968551622469282315096707517108, 186769443202545717151434669644556005309108741078385143683, 48691603859905562687745717731829643706189835379113081935]

p,h = 223,24
mod = (p ** h - 1)
dic = {}
for i in tqdm(product(printable,repeat=3),total=len(printable)**3):
tt = bin(bytes_to_long("".join(i).encode()))[2:].zfill(24)
m = tt.ljust(p, '0')
m = m[:-h + m.count('1')] + '1' * (h - m.count('1'))
s = 0
for j in range(p):
s += int(m[j]) * pubkey[j]
s %= mod
dic[s] = "".join(i)

for i in enc:
print(dic[i],end="")


#ASIS{h0W_dO_yUo_5oLvE_tH!s_m1X3D_5eMi_H4rD_T4Sk!!?}

不过flag头为什么不是CCTF?



Tough

Capac(5 solves , 375 pts)

题目描述:

1
Capac is a specialized case of elliptic curve cryptography, which we've used to develop a new system. Fully cracking it requires deep understanding of all details.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env sage

from Crypto.Util.number import *
from flag import flag

def iscube(n, p):
return pow(n, (p-1) // gcd(3, p-1), p) == 1

def issquare(n, p):
return Mod(n, p).is_square()

def gen_prime(nbit, t):
P = []
for _ in range(t):
while True:
p = getPrime(nbit)
if p % 3 * p % 4 == 3:
P.append(p)
break
return P

def add(P, Q, c, n):
# Add two points P and Q on curve x^3 + c*y^3 + c^2*z^3 - 3*c*x*y*z = 1 in Zmod(n)
(x1, y1, z1) = P
(x2, y2, z2) = Q
x3 = (x1*x2 + c*(y2*z1 + y1*z2)) % n
y3 = (x2*y1 + x1*y2 + c*z1*z2) % n
z3 = (y1*y2 + x2*z1 + x1*z2) % n
return (x3, y3, z3)


def mul(P, g, c, n):
# Scalar multiplication on curve
(x1, y1, z1) = P
(x2, y2, z2) = (1, 0, 0)
for b in bin(g)[2:]:
(x2, y2, z2) = add((x2, y2, z2), (x2, y2, z2), c, n)
if b == '1':
(x2, y2, z2) = add((x2, y2, z2), (x1, y1, z1), c, n)
return (x2, y2, z2)

def keygen(nbit, r, s):
p, q = gen_prime(nbit, 2)
n, o = p^r * q^s, p*q*(p^2+p+1)*(q^2+q+1)*(p - 1)^2*(q - 1)^2
e = randint(1, n)
while gcd(e, o) != 1: e = randint(1, n)
E = [
p^(2*(r-1))*q^(2*(s-1))*(p^2+p+1)*(q^2+q+1),
p^(2*(r-1))*q^(2*(s-1))*(p - 1)^2*(q - 1)^2,
p^(2*(r-1))*q^(2*(s-1))*(p^2+p+1)*(q - 1)^2,
p^(2*(r-1))*q^(2*(s-1))*(p - 1)^2*(q^2+q+1)
]
D = [inverse(e, _) for _ in E]
return (n, e), (p, q, D)

def encrypt(m, pubkey):
(n, e) = pubkey
(x, y) = m
c = ((1 - x^3) * inverse(y^3, n)) % n
enc = mul((x, y, 0), e, c, n)
return enc

nbit, r, s = 128, 4, 6
pubkey, _ = keygen(nbit, r, s)

l = len(flag)
m = (bytes_to_long(flag[:l//2]), bytes_to_long(flag[l//2:]))
assert m[0] < pubkey[0] and m[1] < pubkey[0]
enc = encrypt(m, pubkey)

print(f'pubkey = {pubkey}')
print(f'enc = {enc}')

output.txt:

1
2
pubkey = (88952423353141033373010851411899063207359252317845617788486152084097996161951607075526045796294269180195617694600310298365306053247057038631088894596240751441036842918276089201617169287026865078594774765749720881882802881828282593949861999972983150274426090825607252511101783484830324425960589823065107734367701062429129988752511807642589018556438468736528286490143324590323667921809, 14614034441012778293137473086633630265649701969941154268770044720080815753509578891278180391973505358690528554774945713350178015180660958849492699471696540533037229348328187546698408636098560882156797223120456953146198082440467610904542891004365972384778900094467795995848069039170326511960815050814119987884996078973626805712610037071776396313451847840676985037388757858071864447123)
enc = (74158970310971046231997292851174632705425701438436982687926541524724544224231671471756658836625071336004647139089957902974649062221314465084271501589880770583941500196989635332899104572053993557369592779214576832520061811752375562631936339531925289336294731237548348991115741285818322588605461239511885627502936265303243765703454352951543594465847846082641044334665902197532903361220, 32862210810953363269397127296728098991061383898392345745080345431931505360414244177467955530544676345008050896351576923265903524343350732466422536502620280273396299390682777548383347188854201456093950612280100041148727882639697090249087208377309058187228075582307715470057023215738254854627890486238852220413501003095534357306896337255070197365694626426757850493485339926618395215692, 8760728942548667764591709653964398229332835041712977477051692400662690767214082230265877984156055902045022941831987525871134103847451840503068333260259579853989011172849487654339072318429238338649112865664749209365538613493979432571794717299576778336030161337610867161636673944099727796251480137420420611432383002785264268560701104663971345262296523136517664986251708476458028224136)

最重量级的一题。

题目基于Cubic Pell Curve的加密,和前段时间DASCTF的一样,关于这个加密系统的论文是:

385.pdf (iacr.org)

具体加密和解密步骤参考论文就可以了,总之只要有私钥——也就是n的分解的话就可以做。其中n是这样的形式:

其中p、q均为128bit的素数。

问题就在于,整个题目就完完全全是把这个加密实现了一遍,完全没看出什么漏洞,那怎么解?不过考虑到p、q都是128bit,说大的话也不算大(前面几个题不也是128bit挂yafu吗),所以还是先给n开了个根之后挂了个yafu。一时半会儿没有动静,就先挂住了。

距离比赛结束还剩几小时的时候,实在觉得又困又饿(通宵+没吃饭),其他题也做的差不多了,就想着差不多出门吃个饭,这个时候想起还挂了个yafu,然后切过去看了看:

image-20240701141056368

太逆天了,硬挂32949.2510秒出了分解,所以说factorCTF不是没有道理,发到群里大火也炸锅了哈哈哈:

image-20240701141412956

image-20240701141459600

image-20240701141537346

image-20240701141559738

之后把这个分解给@hash-hash(因为他之前解过DASCTF那个,应该改一改就能用),也是很快就搞定了,这里偷懒搬一搬他的脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from Crypto.Util.number import *

class Pell_Curve:
def __init__(self, a, N):
self.a = a
self.N = N

def is_on_curve(self, point):
if point is None:
return True
x, y, z = point
return (
x**3 + self.a * y**3 + self.a**2 * z**3 - 3 * self.a * x * y * z
) % self.N == 1

def add(self, P, Q):
x1, y1, z1 = P
x2, y2, z2 = Q
x3 = (x1 * x2 + self.a * (y2 * z1 + y1 * z2)) % self.N
y3 = (x2 * y1 + x1 * y2 + self.a * z1 * z2) % self.N
z3 = (y1 * y2 + x2 * z1 + x1 * z2) % self.N
return (x3, y3, z3)

def mul(self, P, x):
Q = (1, 0, 0)
while x > 0:
if x & 1:
Q = self.add(Q, P)
P = self.add(P, P)
x >>= 1
return Q


def gen(E, Q, r, s):
lcg = LCG(E, Q)
while 1:
p = lcg.get_prime()
q = lcg.get_prime()
print(p, q)
if p % 3 == 1 and q % 3 == 1:
N = p**r * q**s
e = 0x20002
return (N, e)


def encrypt(M, N, e):
xm, ym = M
M = (xm, ym, 0)
a = (1 - xm**3) * inverse(ym**3, N) % N
curve = Pell_Curve(int(a), N)
if curve.is_on_curve(M):
return curve.mul(M, e), curve.is_on_curve(curve.mul(M, e))
return None

q = 171955359080932502551172606124599656543
p = 242195390295637766135570468161415180323

r, s = 4, 6

n, e = (88952423353141033373010851411899063207359252317845617788486152084097996161951607075526045796294269180195617694600310298365306053247057038631088894596240751441036842918276089201617169287026865078594774765749720881882802881828282593949861999972983150274426090825607252511101783484830324425960589823065107734367701062429129988752511807642589018556438468736528286490143324590323667921809, 14614034441012778293137473086633630265649701969941154268770044720080815753509578891278180391973505358690528554774945713350178015180660958849492699471696540533037229348328187546698408636098560882156797223120456953146198082440467610904542891004365972384778900094467795995848069039170326511960815050814119987884996078973626805712610037071776396313451847840676985037388757858071864447123)
x, y, z = (74158970310971046231997292851174632705425701438436982687926541524724544224231671471756658836625071336004647139089957902974649062221314465084271501589880770583941500196989635332899104572053993557369592779214576832520061811752375562631936339531925289336294731237548348991115741285818322588605461239511885627502936265303243765703454352951543594465847846082641044334665902197532903361220, 32862210810953363269397127296728098991061383898392345745080345431931505360414244177467955530544676345008050896351576923265903524343350732466422536502620280273396299390682777548383347188854201456093950612280100041148727882639697090249087208377309058187228075582307715470057023215738254854627890486238852220413501003095534357306896337255070197365694626426757850493485339926618395215692, 8760728942548667764591709653964398229332835041712977477051692400662690767214082230265877984156055902045022941831987525871134103847451840503068333260259579853989011172849487654339072318429238338649112865664749209365538613493979432571794717299576778336030161337610867161636673944099727796251480137420420611432383002785264268560701104663971345262296523136517664986251708476458028224136)


a = [

38082677088809290708888864314473611698208489947149149020141323354876853986846324667486599783099732449253321999219530956251885062836963617473433230328586192652813973364030607478840215477264438623285384369097235663234708836294503355550514113793191654083605154926924302782918924474190008822819680949832855309114998683480624021568578210762241091831893916401635203167722829317883574117304,
55542785903937490819242771302549793176088751965230011448729375621780480702917260980627366579559731499071481465701273803009160786625112585403160409241867914028776478433782380129148977220686475108517789796522840237011806357784338595091229781789385237782508264914170678295844412591496324511913794387127204408153535129355464447351230388778897031953295817190543912286619989423406655977700,
32809102117103053026047707178806708898308641810687342740050761956023499193587988346676441294031383037731625771964079418993097123328311195543468739276724770166588580694731012772209001624219626666752889679553768268558153713398821943876723711708308989355210892928040680077147537640505525324780317757262161253076610556164212322764995420591745497492132140599484611217302174245592265314953,
50269210932231253136401614166882890376188903828768205168638814222927125909658924659817208090491382087549785238445822265750372847116460163473195918190006491542551085764482785422517763367641663151985295106979372842335251234888657183417439379704502573054114002915287055590073025757811841013874431194556510352115147002039052748547647598608401437613534041388393320336199334351115347175349]

C = (74158970310971046231997292851174632705425701438436982687926541524724544224231671471756658836625071336004647139089957902974649062221314465084271501589880770583941500196989635332899104572053993557369592779214576832520061811752375562631936339531925289336294731237548348991115741285818322588605461239511885627502936265303243765703454352951543594465847846082641044334665902197532903361220, 32862210810953363269397127296728098991061383898392345745080345431931505360414244177467955530544676345008050896351576923265903524343350732466422536502620280273396299390682777548383347188854201456093950612280100041148727882639697090249087208377309058187228075582307715470057023215738254854627890486238852220413501003095534357306896337255070197365694626426757850493485339926618395215692, 8760728942548667764591709653964398229332835041712977477051692400662690767214082230265877984156055902045022941831987525871134103847451840503068333260259579853989011172849487654339072318429238338649112865664749209365538613493979432571794717299576778336030161337610867161636673944099727796251480137420420611432383002785264268560701104663971345262296523136517664986251708476458028224136)

Fp = GF(p)
Fq = GF(q)


for a0 in a:
curve = Pell_Curve(int(a0), n)
try:
root = Fp(a0).nth_root(3)
phi1 = p^(2*(r-1))*(p-1)^2
except:
phi1 = p^(2*(r-1))*(p^2+p+1)
try:
root = Fq(a0).nth_root(3)
phi2 = q^(2*(s-1))*(q-1)^2
except:
phi2 = q^(2*(s-1))*(q^2+q+1)

phi = phi1*phi2
d = inverse_mod(e, phi)
print(long_to_bytes(curve.mul(C, d)[0]))
print(long_to_bytes(curve.mul(C, d)[1]))

#CCTF{n3W_Publ1c_k3Y_crYp70sy5T3m_8a5Ed_ON_7h3_cBb!c_Pell_curV3s!!!}

这个题就这么结束了,有点奇妙。

赛后看deebato他们硬挂yafu没直接显示结果,但是在log里找到了分解,突然感觉我这里分解出因子的时候可能也远远不到32949.2510这么多秒,只是程序没运行完没显示罢了,又学到一招。

关于这个题,其实赛中@yolbby想出了一个很合理的思路,只是对于这个题的数据来说并不太适用,我把他的想法出成了一题,放在了NSSCTF

相对于这个题目,仅仅是把p、q改成了512bit,别的都没有变,有兴趣的师傅可以做做看


Jonon(13 solves , 247 pts)

题目描述:

1
The Jonon challenge is a variation of the Imen challenge, and it provides a similar level of difficulty as the original version - it's an enjoyable experience.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env sage

from Crypto.Util.number import *
from string import printable as prn

def genimen(k, p, _B):
while True:
A = random_matrix(GF(p), k)
for i in range(k):
for j in range(k):
A[i, j] = int(A[i, j]) % (_B + 1)
if det(A) != 0: return A

def genkey(k, p, _B, l):
sA = [genimen(k, p, _B) for _ in range(l + 1)]
L = list(range(l))
shuffle(L)
s, pE = randint(1, p - 1), prod([sA[_] for _ in L])
sD, sE = sA[-1], s * pE
pkey = [sE * _ * sD * sE.inverse() for _ in sA[:-1]], sA[:-1], pE
skey = L, s, sE, sD
return pkey, skey

def encrypt(pkey, _p):
pA, psA, pE = pkey
k = pA[0].nrows()
C = identity_matrix(GF(p), k)
for i in _p: C *= pA[i]
return C

nbit = 256
k, l, p, _B = 5, 19, getPrime(nbit), 63
_p = list(range(l))
shuffle(_p)
flag = 'CCTF{' + ''.join([prn[10 + _p[_]] for _ in range(l)]) + '}'

pkey, skey = genkey(k, p, _B, l)
C = encrypt(pkey, _p)
print(f'pkey = {pkey}')
print(f'C = {C}')

output比较长,也不放在这里。

可以看出这个题和Imen相当的神似,只是稍稍变了一些。先把genkey梳理一下:

  • 生成l+1个模p下的矩阵:

  • 生成随机数s

  • 将sA打乱后,计算其乘积pE:

  • 计算:

  • 再依次计算下面矩阵的值得到列表,并和sA(不包含sD)和pE一同作为公钥pkey:

  • 打乱后的列表L,s,sE和sD作为私钥

然后是encrypt:

  • 先生成一个1-l的随机置换pi,并用这个置换生成flag(也就是求出置换就有flag)

  • 生成pkey,skey

  • 计算C:

要求求出置换。

首先,所有矩阵都是模p下的,虽然我们并没有p,但是我们有所有的:

求逆得到:

所以有:

然后又因为逆矩阵具有下面的性质:

所以说上面的式子又可以变成:

也就是:

而这几个矩阵都是公钥里的,所以可以在ZZ上直接求这个矩阵,那么两者的行列式在模p下应该相等,所以可以多求几组作差,然后gcd就得到p了。

有了p过后,又因为:

所以又很轻松就可以在模p下计算出sD来。

之后就很好办了,现在我们拥有C,要求对应的置换:

而可以看出,首位的sE和sE^(-1)无非是对矩阵做了相似,而相似前后,矩阵的迹是不会变的,又因为sA和sD都较小,所以可以通过枚举sA,然后判断迹是增大还是减小来检查枚举的是否正确,就得到了置换。

然而有个细节需要注意,那就是题目设置的数量级会使得最终C的迹也会超过p的大小,所以先枚举出可能的初始路径之后,再进行搜索才行。

exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from Crypto.Util.number import *
from string import printable as prn

T =
sA =
pE =
C =

######################################################## part1 get p
t1 = Matrix(ZZ,T[0])*Matrix(ZZ,T[1])^(-1)
t2 = Matrix(ZZ,pE)*Matrix(ZZ,sA[0])*Matrix(ZZ,sA[1])^(-1)*Matrix(ZZ,pE)^(-1)
t3 = Matrix(ZZ,T[0])*Matrix(ZZ,T[2])^(-1)
t4 = Matrix(ZZ,pE)*Matrix(ZZ,sA[0])*Matrix(ZZ,sA[2])^(-1)*Matrix(ZZ,pE)^(-1)
t5 = Matrix(ZZ,T[0])*Matrix(ZZ,T[3])^(-1)
t6 = Matrix(ZZ,pE)*Matrix(ZZ,sA[0])*Matrix(ZZ,sA[3])^(-1)*Matrix(ZZ,pE)^(-1)


kp = gcd(gcd(t1.det()-t2.det() , t3.det()-t4.det()) , t5.det()-t6.det())
p = kp.numerator()
assert isPrime(p)


######################################################## part2 get sD
F = GF(p)
T = [Matrix(F,i) for i in T]
sA = [Matrix(F,i) for i in sA]
pE = Matrix(F,pE)
C = Matrix(F,C)
sD = sA[0]^(-1)*pE^(-1)*T[0]*pE


######################################################## part3 trace
def find(temp,setT,path):
if(all(i[0] == 0 for i in setT)):
flag = 'CCTF{' + ''.join([prn[10 + path[_]] for _ in range(19)]) + '}'
print(flag)
return

tt = [ii for ii in setT]
for i in range(len(setT)):
if((setT[i][1]^(-1)*temp).trace() < temp.trace()):
tt[i][0] = 0
find(setT[i][1]^(-1)*temp , tt , path+[i])
tt[i][0] = 1


T_table = [[1,i] for i in T]
for i in range(19):
for j in range(19):
for k in range(19):
temp = T[i]^(-1)*T[j]^(-1)*T[k]^(-1)*C
tt = [ii for ii in T_table]
if len(bin(int(temp.trace()))) < 232:
tt[i][0] = 0
tt[j][0] = 0
tt[k][0] = 0
find(temp , tt , [k,j,i])


#CCTF{mpradjnkfcqhilsoebg}

和Imen一样,这个题的输出也是依托,要自己处理一下变成列表形式。


Rehawk(9 solves , 300 pts)

题目描述:

1
Rehawk is an advanced, modern cryptosystem of great complexity that only seasoned cryptographers can comprehend, with few able to successfully break it.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env sage

from hashlib import sha256

def gen_params(m):
C = CyclotomicField(m)
F, PHI = C.maximal_totally_real_subfield()
d = F.degree()
D = [[0 for _ in range(d)] for _ in range(d)]
for i in range(d):
for j in range(i + 1):
if (i - j) % 2 == 0: D[i][j] = binomial(i, (i - j) >> 1)
D = Matrix(D)
O = [F(_) for _ in D.inverse()]
return (C, F, PHI, O, D)

def genkey(F, O, D, _B):
_d, g = F.degree(), F.gen()
a = sum([randint(-_B, _B + 1) * g**_ for _ in range(_d)])
A = a.matrix().LLL()
while True:
b = sum([randint(-_B, _B + 1) * g**_ for _ in range(_d)])
B = b.matrix().LLL()
C = block_matrix([[A], [B]]).change_ring(ZZ)
R, M = C.hermite_form(transformation = True)
if R[0:_d] == identity_matrix(_d): break
c, d = F(-M[0][_d:2*_d]*B) / b, F(M[0][0:_d]*A) / a
n = (a*c + b*d) / (a**2 + b**2)
N = vector(n) * D
_n = sum(round(N[_]) * O[_] for _ in range(_d))
_c, _d = c - _n * a, d - _n * b
skey = Matrix([[a, _c], [b, _d]])
pkey = (skey + identity_matrix(2)) * (skey.T - identity_matrix(2))
return (skey, pkey)

nbit = 4 * 32
C, F, PHI, O, D = gen_params(nbit)
skey, pkey = genkey(F, O, D, nbit >> 2)

flag = str(sum(skey.coefficients()).polynomial()(2^128))
flag = 'CCTF{' + sha256(flag.encode()).hexdigest() + '}'

f = open('flag_update.txt', 'w')
f.write(flag)
f.close()

f = open('pubkey_update', 'wb')
f.write(str(pkey).encode())
f.close()

output.txt:

1
2
[            -4602774218265912404*zeta1280^31 - 25509381853469544908*zeta1280^30 + 129376017982404349488*zeta1280^29 + 716286531689185935744*zeta1280^28 - 1632208466401721293158*zeta1280^27 - 9028003170806549615013*zeta1280^26 + 12206704950441875625482*zeta1280^25 + 67457290883578785545644*zeta1280^24 - 60204416625997809670344*zeta1280^23 - 332433796634335506443469*zeta1280^22 + 206123873952616279215906*zeta1280^21 + 1137323310449735580891532*zeta1280^20 - 502186108465894748616016*zeta1280^19 - 2769056905000016534751538*zeta1280^18 + 878039249325641175319414*zeta1280^17 + 4838663751293786838127040*zeta1280^16 - 1097188014524649363149070*zeta1280^15 - 6043236501081735721161712*zeta1280^14 + 964124818127455367449150*zeta1280^13 + 5308004439355857834330485*zeta1280^12 - 578006232877698984851372*zeta1280^11 - 3181061779748249841736219*zeta1280^10 + 224962785316232564841220*zeta1280^9 + 1237722919632690072191185*zeta1280^8 - 52424883340535911897910*zeta1280^7 - 288372879182213647372365*zeta1280^6 + 6357946814410664097444*zeta1280^5 + 34967804349427160519879*zeta1280^4 - 302245973040480152708*zeta1280^3 - 1662172616495876145310*zeta1280^2 + 2366049046813041208*zeta1280 + 13011646416303986233                     20341475869141650336*zeta1280^31 + 1951592807564566629*zeta1280^30 - 571077856249690441621*zeta1280^29 - 55295108849716726354*zeta1280^28 + 7196660823549758544764*zeta1280^27 + 702767505392614093619*zeta1280^26 - 53765538109270297626741*zeta1280^25 - 5291465118503133518883*zeta1280^24 + 264923914169814211875106*zeta1280^23 + 26259460116858500856269*zeta1280^22 - 906245929396843087071465*zeta1280^21 - 90407733487003834471466*zeta1280^20 + 2206203021593936629146834*zeta1280^19 + 221362527660366178501972*zeta1280^18 - 3854747120193508447272846*zeta1280^17 - 388741550159692397343437*zeta1280^16 + 4813958180021168270877045*zeta1280^15 + 487624064628609110025176*zeta1280^14 - 4227967989714953615984050*zeta1280^13 - 429881071729752134763778*zeta1280^12 + 2533644029049790041455129*zeta1280^11 + 258414888967057871397446*zeta1280^10 - 985769426015678192342018*zeta1280^9 - 100792924475518106238832*zeta1280^8 + 229662454242100806590311*zeta1280^7 + 23526719373273264108474*zeta1280^6 - 27847921707112376241503*zeta1280^5 - 2856416535680139555816*zeta1280^4 + 1323714816988549264238*zeta1280^3 + 135871497938828491146*zeta1280^2 - 10362137493026264546*zeta1280 - 1063759814089207676]
[ 20341475868748923024*zeta1280^31 + 1951592806605672059*zeta1280^30 - 571077856238540660223*zeta1280^29 - 55295108822895822100*zeta1280^28 + 7196660823407758697940*zeta1280^27 + 702767505055806319373*zeta1280^26 - 53765538108198903629865*zeta1280^25 - 5291465115995185819391*zeta1280^24 + 264923914164486524534940*zeta1280^23 + 26259460104538909856351*zeta1280^22 - 906245929378464841397589*zeta1280^21 - 90407733444980990175230*zeta1280^20 + 2206203021548855068437216*zeta1280^19 + 221362527558328990088394*zeta1280^18 - 3854747120114205378128966*zeta1280^17 - 388741549981824734924651*zeta1280^16 + 4813958179921541651869291*zeta1280^15 + 487624064406937426541122*zeta1280^14 - 4227967989627007169290270*zeta1280^13 - 429881071535409363510562*zeta1280^12 + 2533644028996862789742983*zeta1280^11 + 258414888850769755761022*zeta1280^10 - 985769425995015280773042*zeta1280^9 - 100792924430327984016820*zeta1280^8 + 229662454237274355481855*zeta1280^7 + 23526719362754563876958*zeta1280^6 - 27847921706526106032793*zeta1280^5 - 2856416534405482802868*zeta1280^4 + 1323714816960654684330*zeta1280^3 + 135871497878260136072*zeta1280^2 - 10362137492807861970*zeta1280 - 1063759813615106246 9479323308436728508*zeta1280^31 - 70533314534492196905*zeta1280^30 - 265607299367708827120*zeta1280^29 + 1979831130978284462763*zeta1280^28 + 3341016926189694874654*zeta1280^27 - 24945362580283401746400*zeta1280^26 - 24917836206749318963324*zeta1280^25 + 186334783002684698789191*zeta1280^24 + 122586899086925146656452*zeta1280^23 - 918010464058548571922420*zeta1280^22 - 418740666342845811245466*zeta1280^21 + 3139891285412312572660856*zeta1280^20 + 1018079424800570481061316*zeta1280^19 - 7642962939237553667703665*zeta1280^18 - 1776769434047526922198020*zeta1280^17 + 13352594357905645275206924*zeta1280^16 + 2216659229139532144217196*zeta1280^15 - 16673676083116486323107226*zeta1280^14 - 1945146801270513519057908*zeta1280^13 + 14642857602023675891482054*zeta1280^12 + 1164803955054950478252698*zeta1280^11 - 8774263024173173761196618*zeta1280^10 - 452930026846689350220538*zeta1280^9 + 3413634778211777313137122*zeta1280^8 + 105476445981425991518928*zeta1280^7 - 795268995785895547201508*zeta1280^6 - 12785805943160598272756*zeta1280^5 + 96428355297618433095990*zeta1280^4 + 607656823442098385608*zeta1280^3 - 4583526826365167751127*zeta1280^2 - 4756628030961100976*zeta1280 + 35880078853642750464]

这个题目看了一眼我就被劝退了,又是分圆域又是hermite矩阵的,看着就头大。

然后@yolbby一语点醒梦中人,定位到这里:

1
2
3
skey = Matrix([[a, _c], [b, _d]])
pkey = (skey + identity_matrix(2)) * (skey.T - identity_matrix(2))
return (skey, pkey)

可以看出skey和pkey都是2x2的矩阵,并且还存在关系如下:

而其实这相当于四个方程四个未知数,只是元素在一个陌生一点的域中而已,sage依然是可以正常处理的。也就是说,用这四个方程去resultant就可以把skey完全解出来。又由于flag是根据skey生成的,所以解出来之后就可以拿到flag。

exp(from yolbby):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from hashlib import sha256
f = open(r'C:\Users\chax\Desktop\rehawk_updated\pubkey_update','rb')

C = CyclotomicField(128)
F, PHI = C.maximal_totally_real_subfield()
d = F.degree()
D = [[0 for _ in range(d)] for _ in range(d)]
for i in range(d):
for j in range(i + 1):
if (i - j) % 2 == 0: D[i][j] = binomial(i, (i - j) >> 1)
D = Matrix(D)
O = [F(_) for _ in D.inverse()]
a = f.readline().replace(b' ',b',').replace(b'^',b'**')
zeta1280 = F.gen()
a = matrix(F,eval(a))
b = [20341475868748923024*zeta1280**31 + 1951592806605672059*zeta1280**30 - 571077856238540660223*zeta1280**29 - 55295108822895822100*zeta1280**28 + 7196660823407758697940*zeta1280**27 + 702767505055806319373*zeta1280**26 - 53765538108198903629865*zeta1280**25 - 5291465115995185819391*zeta1280**24 + 264923914164486524534940*zeta1280**23 + 26259460104538909856351*zeta1280**22 - 906245929378464841397589*zeta1280**21 - 90407733444980990175230*zeta1280**20 + 2206203021548855068437216*zeta1280**19 + 221362527558328990088394*zeta1280**18 - 3854747120114205378128966*zeta1280**17 - 388741549981824734924651*zeta1280**16 + 4813958179921541651869291*zeta1280**15 + 487624064406937426541122*zeta1280**14 - 4227967989627007169290270*zeta1280**13 - 429881071535409363510562*zeta1280**12 + 2533644028996862789742983*zeta1280**11 + 258414888850769755761022*zeta1280**10 - 985769425995015280773042*zeta1280**9 - 100792924430327984016820*zeta1280**8 + 229662454237274355481855*zeta1280**7 + 23526719362754563876958*zeta1280**6 - 27847921706526106032793*zeta1280**5 - 2856416534405482802868*zeta1280**4 + 1323714816960654684330*zeta1280**3 + 135871497878260136072*zeta1280**2 - 10362137492807861970*zeta1280 - 1063759813615106246,9479323308436728508*zeta1280**31 - 70533314534492196905*zeta1280**30 - 265607299367708827120*zeta1280**29 + 1979831130978284462763*zeta1280**28 + 3341016926189694874654*zeta1280**27 - 24945362580283401746400*zeta1280**26 - 24917836206749318963324*zeta1280**25 + 186334783002684698789191*zeta1280**24 + 122586899086925146656452*zeta1280**23 - 918010464058548571922420*zeta1280**22 - 418740666342845811245466*zeta1280**21 + 3139891285412312572660856*zeta1280**20 + 1018079424800570481061316*zeta1280**19 - 7642962939237553667703665*zeta1280**18 - 1776769434047526922198020*zeta1280**17 + 13352594357905645275206924*zeta1280**16 + 2216659229139532144217196*zeta1280**15 - 16673676083116486323107226*zeta1280**14 - 1945146801270513519057908*zeta1280**13 + 14642857602023675891482054*zeta1280**12 + 1164803955054950478252698*zeta1280**11 - 8774263024173173761196618*zeta1280**10 - 452930026846689350220538*zeta1280**9 + 3413634778211777313137122*zeta1280**8 + 105476445981425991518928*zeta1280**7 - 795268995785895547201508*zeta1280**6 - 12785805943160598272756*zeta1280**5 + 96428355297618433095990*zeta1280**4 + 607656823442098385608*zeta1280**3 - 4583526826365167751127*zeta1280**2 - 4756628030961100976*zeta1280 + 35880078853642750464]
b = matrix(F,b)
pkey = block_matrix([[a],[b]])
a2_c_2 = pkey[0,0]+1
b_c = ((pkey[0,1]-pkey[1,0])/2)

ab_cd = pkey[0,1]-b_c
b2_d_2 = pkey[1,1]+1

print(a2_c_2)
print(ab_cd)
print(b2_d_2)

R.<a,b,c,d> = PolynomialRing(F)
f1 = a^2+c^2-a2_c_2
f2 = a*b+c*d-ab_cd
f3 = b^2+d^2-b2_d_2
f4 = b-c-b_c

h1 = f1.sylvester_matrix(f4, b).det()
h2 = f2.sylvester_matrix(f4, b).det()
h3 = f3.sylvester_matrix(f4, b).det()
g1 = h1.sylvester_matrix(h2, c).det()
g2 = h1.sylvester_matrix(h3, c).det()
t = g1.sylvester_matrix(g2, d).det()

R.<a> = PolynomialRing(F)
t = R(t)
#print(t.roots())

a = -8*zeta1280^31 + 6*zeta1280^30 + 11*zeta1280^29 - 11*zeta1280^28 - 2*zeta1280^27 - 30*zeta1280^26 + 12*zeta1280^25 - 3*zeta1280^24 + 6*zeta1280^23 + 18*zeta1280^22 - 15*zeta1280^21 + 32*zeta1280^20 - 22*zeta1280^19 + 26*zeta1280^18 + 4*zeta1280^17 + 22*zeta1280^16 - 13*zeta1280^15 + 29*zeta1280^14 - 6*zeta1280^13 - 24*zeta1280^12 - 3*zeta1280^11 - 27*zeta1280^10 + 30*zeta1280^9 + 20*zeta1280^8 + 26*zeta1280^7 + 29*zeta1280^6 - 13*zeta1280^5 - 12*zeta1280^4 + 10*zeta1280^3 - 15*zeta1280^2 + 21*zeta1280 + 17
R.<c> = PolynomialRing(F)
f1 = a^2+c^2-a2_c_2
#print(f1.roots())

c = 196363659*zeta1280^31 + 479447260*zeta1280^30 - 5574890698*zeta1280^29 - 13410452103*zeta1280^28 + 70999923425*zeta1280^27 + 168403887120*zeta1280^26 - 535696998441*zeta1280^25 - 1253973849729*zeta1280^24 + 2663843670086*zeta1280^23 + 6159795499952*zeta1280^22 - 9189122836929*zeta1280^21 - 21011422148145*zeta1280^20 + 22540780354795*zeta1280^19 + 51018594206807*zeta1280^18 - 39651534571929*zeta1280^17 - 88933831209400*zeta1280^16 + 49813309503893*zeta1280^15 + 110835841742009*zeta1280^14 - 43973223346903*zeta1280^13 - 97171385626612*zeta1280^12 + 26463625856054*zeta1280^11 + 58144057818179*zeta1280^10 - 10331455784462*zeta1280^9 - 22595061111030*zeta1280^8 + 2413225554250*zeta1280^7 + 5259350115786*zeta1280^6 - 293135104364*zeta1280^5 - 637328376483*zeta1280^4 + 13947289953*zeta1280^3 + 30284177528*zeta1280^2 - 109201303*zeta1280 - 237050725
c = -c
b = c+b_c
print(b)

R.<d> = PolynomialRing(F)
f3 = b^2+d^2-b2_d_2
print(f3.roots())
d = 266010789*zeta1280^31 + 482737266*zeta1280^30 - 7361323736*zeta1280^29 - 13829433097*zeta1280^28 + 91483219711*zeta1280^27 + 177606388336*zeta1280^26 - 674398006293*zeta1280^25 - 1350408311994*zeta1280^24 + 3281208330570*zeta1280^23 + 6762561063913*zeta1280^22 - 11091831601900*zeta1280^21 - 23477085414453*zeta1280^20 + 26707622486418*zeta1280^19 + 57918642171854*zeta1280^18 - 46200520267420*zeta1280^17 - 102400447660711*zeta1280^16 + 57184654198715*zeta1280^15 + 129209740105463*zeta1280^14 - 49834602378781*zeta1280^13 - 114489362577117*zeta1280^12 + 29667951387175*zeta1280^11 + 69115665617763*zeta1280^10 - 11481488829941*zeta1280^9 - 27050018004836*zeta1280^8 + 2664050718278*zeta1280^7 + 6330200656934*zeta1280^6 - 322126775377*zeta1280^5 - 769914149547*zeta1280^4 + 15288262361*zeta1280^3 + 36657928391*zeta1280^2 - 119641092*zeta1280 - 287055447

print(a*b+c*d == ab_cd)
skey = matrix([[a,c],[b,d]])

P = (skey + identity_matrix(2)) * (skey.T - identity_matrix(2))
print(P[0,0] == pkey[0,0])
print(P[0,1] == pkey[0,1])
print(P[1,0] == pkey[1,0])
print(P[1,1] == pkey[1,1])
flag = str(sum(skey.coefficients()).polynomial()(2^128))
flag = 'CCTF{' + sha256(flag.encode()).hexdigest() + '}'
print(flag)


Vorop(7 solves , 334 pts)

题目描述:

1
Vorop is a post-modern cryptosystem with diverse applications in finite fields and matrices - thoroughly analyze it to attempt breaking it.

题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env sage

import sys
from flag import flag

def die(*args):
pr(*args)
quit()

def pr(*args):
s = " ".join(map(str, args))
sys.stdout.write(s + "\n")
sys.stdout.flush()

def sc():
return sys.stdin.buffer.readline()

def rand_utm(F, n):
M = matrix(F, n, n)
for i in range(n):
for j in range(i, n):
M[i,j] = F.random_element()
return M

def upper(F, M):
r, c = M.nrows(), M.ncols()
_U = matrix(F, r, c)
for i in range(r):
_U[i, i] = M[i, i]
for j in range(i + 1, c):
_U[i, j] = M[i, j] + M[j, i]
return _U

def genkey(q, m, d, u):
n = m + d + u
F = GF(q)
O = random_matrix(F, u, m + d)
c = O.ncols()
I = identity_matrix(F, c)
OI = block_matrix([[O], [I]])
P = []
for _ in range(m):
A = rand_utm(F, u)
B = random_matrix(F, u, m + d)
C = O.transpose() * A * O + O.transpose() * B
C = upper(F, C)
D = matrix(F, m + d, u)
Q = block_matrix([[A, B], [D, C]])
P.append(Q)
return (O, OI), P

def iszero(M):
F, r, c = M.base_ring(), M.nrows(), M.ncols()
return matrix(F, r, c) == M

def n2F(n):
z = GF(256).gen()
G, g = bin(n)[2:].zfill(8), GF(256)(0)
for i in range(8):
g += int(G[i]) * z ^ (7 - i)
return g

def compress(M):
r, c = M.nrows(), M.ncols()
_M = matrix(r, c)
for i in range(r):
for j in range(c):
_M[i, j] = M[i][j].to_integer()
return _M

def oracle(O) :
F = O.base_ring()
r, c = O.nrows(), O.ncols()
n = r + c
I = identity_matrix(F, c)
OI = block_matrix([[O], [I]])
U = matrix(F, n, 1)
while(iszero(U)):
V = random_matrix(F, c, 1)
U = OI * V
return U

def isequal(F, A, B):
l, k = len(A), len(B)
if l != k: return False
n = A[0].nrows()
for _ in range(l):
V = random_matrix(F, n, 1)
for _ in range(l):
r = V.transpose() * A[_] * V
s = V.transpose() * B[_] * V
if r != s: return False
return True

def verify(P, OI):
r, c, F = P[0].nrows(), OI.ncols(), OI.base_ring()
B, Z = [], []
for _ in P:
Z.append(matrix(F, c, c))
B.append(OI.transpose() * _ * OI)
return isequal(F, B, Z)

def main():
border = "┃"
pr( "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
pr(border, " .: Welcome to VOROP oracle! Try to break this cryptosystem :. ", border)
pr( "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")
q, m, d, u = 2**8, 38, 8, 86
params = (q, m, d, u)
F = GF(q)
(O, OI), P = genkey(q, m, d, u)
U = oracle(O)

while verify(P, OI):
pr("| Options: \n|\t[G]et the parameters \n|\t[O]racle output \n|\t[P]ublic key \n|\t[V]erify \n|\t[Q]uit")
ans = sc().decode().lower().strip()
if ans == 'g':
pr(border, f"The parameters are q, m, d, u = {q, m, d, u}")
elif ans == 'o':
pr(border, f"The oracle output U = {compress(U.transpose())}")
elif ans == 'p':
pr(border, f"Public key P is very large matrix, I will print row by row!")
for p in P:
_p = compress(p)
for _r in _p:
pr(border, f"{_r}")
pr(border, "━" * 40)
pr(border, "Done!")
elif ans == 'v':
pr(border, f"You should send a matrix with the appropriate size!")
pr(border, f"So please send it row by row separated by comma ','")
B = []
for _ in range(m + d):
pr(border, f"Send the row {_}:")
_r = sc().decode().split(',')
try:
_r = [n2F(int(_)) for _ in _r]
B.append(_r)
except:
die(border, f"The input you provided is is not valid!")
_b, B = False, matrix(F, B)
if B.ncols() == m + d + u:
_b = verify(P, B.transpose()) and not B.is_zero()
if _b:
die(border, f'Congrats, you got the flag: {flag}')
else:
die(border, f'The provided matrix is not correct! Bye!!')
elif ans == 'q':
die(border, 'Quitting...')
else:
die(border, f"Your input does not meet the requirements!!!")

if __name__ == '__main__':
main()

知识盲区,按@hash-hash的说法是基于多变量的一道题目,之前稍微看过一点多变量,但除了还记得油变量和醋变量之外,其他的完全不懂了。

直接写attack好像很折磨,hashhash就发动群友一起找现成的attack,然后@leukocyte找到了这篇:

https://eprint.iacr.org/2024/279.pdf

里面有实现:

https://github.com/River-Moreira-Ferreira/prov-attack

hashhash稍微调调就能用了,但我确实是一点都整不明白,直接躺好。而且hashhash好像还发现了个非预期。

然而奇怪的事情又发生了——交互总是会卡在第33轮,开始怀疑是网的问题,所以大家都跑了一遍,都卡在33轮。leukocyte最后发现他远端下发的参数和题目附件的不一样,对应改一改就好了。

但还是完全不懂多变量,有空学一学。



总结

虽然比赛槽点很多,但是总而言之,一群喜欢密码的师傅聚一块儿打比赛本来就是很开心的事情,明年继续加油。

还有就是比赛结束第二天去CTFtime建战队的时候发现cyberCryer这个战队名被 抢 注 了,真是离了大谱。给CTFtime发feedback过后恢复了正常,然而排名似乎不太好改了,发feedback、发邮件甚至去推上at都没有人理,伤心。