e = nextprime(secret) p = getPrime(1024) q = getPrime(1024)
_lambda = ((p-1)*(q-1)) / gcd(p-1,q-1)
flag = bytes_to_long(flag)
print(_lambda) print(p*q) print(pow(flag,e,p*q)) ''' # data '''
analyses
条件全给的LCG,直接往下推就好了。 RSA, 直接求逆就是私钥了
exp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
from sympy import nextprime from Crypto.Util.number import * from gmpy2 import invert
deflcg(seed, params): (m, c, n) = params s = seed % n return (m * s + c) % n
m, c, n = (315926151576125492949520250047736865439, 204423972944032372640132172728460755543, 375402477603617093440157245062608289367) seed = 287868671713011127830359814204794790287 e = nextprime(lcg(seed, (m, c, n))) _lambda = d = invert(e, lam) n = c = print(long_to_bytes(pow(c, d, n)))
0x01 FeedBack
流密码的路子,再熟悉不过了
problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from secret import flag from string import hexdigits import random from functools import reduce
c = [...] A = [] for i inrange(27): v = c[i:i+27] A.append(v) s = vector(c[27:]) A = Matrix(A) a = A.solve_left(s)
c = c[:27] m = [] for _ inrange(27): for i inrange(0xff): res = cycle([i] + c[:-1],a) if res == c[-1]: c = [i] + c[:-1] m.append(chr(i)) break ''.join(m[::-1])
#! /bin/bash/env python3 from random import randrange from Crypto.Util.number import * from gmpy2 import invert defgcd(a,b): while b: a,b = b,a%b return a
defgenerate(): p = getPrime(1024) whileTrue: f = randrange(1,(p//2)**(0.5)) g = randrange((p//4)**(0.5),(p//2)**(0.5)) if gcd(f,p)==1and gcd(f,g)==1: break h = (invert(f,p)*g)%p return h,p,f,g
defencrypt(m,h,p): assert m<(p//4)**(0.5) r = randrange(1,(p//2)**(0.5)) c = (r*h+m)%p return c
h,p,f,g = generate()
from flag import flag c = encrypt(bytes_to_long(flag),h,p) print("h = {}".format(h)) print("p = {}".format(p)) print("c = {}".format(c)) # h = 70851272226599856513658616506718804769182611213413854493145253337330709939355936692154199813179587933065165812259913249917314725765898812249062834111179900151466610356207921771928832591335738750053453046857602342378475278876652263044722419918958361163645152112020971804267503129035439011008349349624213734004 # p = 125796773654949906956757901514929172896506715196511121353157781851652093811702246079116208920427110231653664239838444378725001877052652056537732732266407477191221775698956008368755461680533430353707546171814962217736494341129233572423073286387554056407408816555382448824610216634458550949715062229816683685469 # c = 4691517945653877981376957637565364382959972087952249273292897076221178958350355396910942555879426136128610896883898318646711419768716904972164508407035668258209226498292327845169861395205212789741065517685193351416871631112431257858097798333893494180621728198734264288028849543413123321402664789239712408700
from Crypto.Util.number import long_to_bytes as l2b h = 70851272226599856513658616506718804769182611213413854493145253337330709939355936692154199813179587933065165812259913249917314725765898812249062834111179900151466610356207921771928832591335738750053453046857602342378475278876652263044722419918958361163645152112020971804267503129035439011008349349624213734004 p = 125796773654949906956757901514929172896506715196511121353157781851652093811702246079116208920427110231653664239838444378725001877052652056537732732266407477191221775698956008368755461680533430353707546171814962217736494341129233572423073286387554056407408816555382448824610216634458550949715062229816683685469 c = 4691517945653877981376957637565364382959972087952249273292897076221178958350355396910942555879426136128610896883898318646711419768716904972164508407035668258209226498292327845169861395205212789741065517685193351416871631112431257858097798333893494180621728198734264288028849543413123321402664789239712408700
v1 = vector([1,h]) v2 = vector([0,p]) f,g = Matrix([v1,v2]).LLL()[0] f,g = abs(f),abs(g) d = int(inverse_mod(f,g)) x = int((c * f) % p %g ) l2b((x * d)%g)
0x03 threshold
真正的NTRU,我太菜了不知道多项式要怎么处理,去dawn_wispher爷爷的Blog学习一下,学会了再来更新 3/29 俺回来了, 俺学会了, 不过这边只扔一波脚本, 具体原理可以去看我的另外一篇Translation of LatticeHacks
defbalancedmod(f,N,q): g = list(((f[i] + q//2) %q) - q//2for i inrange(N)) return Zx(g)
defconvolution(f,g,N): return (f*g) % (x^N-1)
defrandomdpoly(d,N): assert d <= N result = N*[0] for j inrange(d): whileTrue: r = randrange(N) ifnot result[r]: break result[r] = 1 - 2*randrange(2) return Zx(result)
definvertmodprime(f,N,p): T = Zx.change_ring(Integers(p)).quotient(x^N-1) return Zx(lift(1 / T(f)))
definvertmodpowerof2(f,N,q): assert q.is_power_of(2) g = invertmodprime(f,2,N) whileTrue: r = balancedmod(convolution(g,f),q) if r == 1: return g g = balancedmod(convolution(g,2 - r),q) defrandommessage(N,p): result = list(randrange(p) - 1for j inrange(N)) return Zx(result) defkeypair(d,N,q,p): whileTrue: try: f = randomdpoly(d,N) fp = invertmodprime(f,N,p) fq = invertmodpowerof2(f,N,q) break except: pass g = randomdpoly() publickey = balancedmod(p * convolution(fq,g,N),N,q) secretkey = f,fp return publickey,secretkey
defencrypt(message,publickey,N,q): r = randomdpoly(randint(N)) return balancedmod(convolution(publickey,r) + message,N,q)
defdecrypt(ciphertext,secretkey,N,q,p): f,fp = secretkey a = balancedmod(convolution(ciphertext,f,N),N,q) return balancedmod(convolution(a,fp,N),N,p) defbalancedmod(f,N,q): g = list(((f[i] + q//2) %q) - q//2for i inrange(N)) return Zx(g)
definvertmodprime(f,N,p): T = Zx.change_ring(Integers(p)).quotient(x^N-1) return Zx(lift(1 / T(f)))
defconvolution(f,g,N): return (f*g) % (x^N-1)
N = 53 q = 28019 p = 257 B = Matrix([[0for _ inrange(2*N)] for _ inrange(2*N)]) ex = ... hx = ... H = hx.list()
sigma = 1 _lambda = 1
for i inrange(N): for j inrange(N): B[i+N,j] = int(H[j-i]) if i == j: B[i,j] = sigma * q B[N+i,N+j] = _lambda * 1 L = B.LLL()[1] fx = Zx(list(L[N:])) fp = invertmodprime(fx,N,p)
for i in decrypt(ex,(fx,fp),N,q,p).list(): print(chr(abs(i)),end='')