Week 1
签到题 InfantRSA
发现我之前网上抄的脚本不太好用了1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23from libnum import n2s
def inverse(u, v):
"""The inverse of :data:`u` *mod* :data:`v`."""
u3, v3 = u, v
u1, v1 = 1, 0
while v3 > 0:
q = u3 // v3
u1, v1 = v1, u1 - v1*q
u3, v3 = v3, u3 - v3*q
while u1<0:
u1 = u1 + v
return u1
p = 681782737450022065655472455411
q = 675274897132088253519831953441
e = 13
c = 275698465082361070145173688411496311542172902608559859019841
n=p*q
phi= (p-1)*(q-1)
d= inverse(e,phi)
m=pow(c,d,n)
s=n2s(m)
print (s) #hgame{t3Xt6O0k_R5A!!!}
Affine
题目:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import gmpy2
from secret import A, B, flag
assert flag.startswith('hgame{') and flag.endswith('}')
TABLE = 'zxcvbnmasdfghjklqwertyuiop1234567890QWERTYUIOPASDFGHJKLZXCVBNM'
MOD = len(TABLE)
cipher = ''
for b in flag:
i = TABLE.find(b)
if i == -1:
cipher += b
else:
ii = (A*i + B) % MOD
cipher += TABLE[ii]
print(cipher)
# A8I5z{xr1A_J7ha_vG_TpH410}
根据之前招新赛那道mod
对于r0 = (y * m) % n
由于我们知道r0,y,n的值,先通过求y模n的乘法逆元,然后可以求得m
两边同乘$y^{-1}$
修改:乍的一看应该是下面的形式
其实正确的换算应该是
类似地,题目的关键是ii = (Ai + B) % MOD
提取A,得ii = (A(i + B//A)) % MOD
把A看做y,把MOD看做n,把ii看做r0,i + B//A看做待求的m
接下来先求出A和B的值
A=13,B=14
1 | def inverse(u, v): |
有个坑,不知道之后有没有机会填掉,我不知道玩什么是-44才能得到正确答案,我一开始是-1,因为14//13=1