from random import randint from gmpy2 import * from Crypto.Util.number import *
def getprime(bits): while 1: n = 1 while n.bit_length() < bits: n *= next_prime(randint(1,1000)) if isPrime(n - 1): return n - 1
m = bytes_to_long(b'flag{************************************}')
p = getprime(505) q = getPrime(512) r = getPrime(512) assert m < q
n = p * q * r e = 0x10001 d = invert(q ** 2, p ** 2) c = pow(m, 2, r) cipher = pow(c, e, n)
print(n) print(d) print(cipher)
使用factordb分解得到的数值
c = pow(m, 2, r)已知 求
求
的解的方法:
1 2
from sympy.ntheory.residue_ntheory import nthroot_mod x=nthroot_mod(a,n,p)
exp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from Crypto.Util.number import long_to_bytes import gmpy2 import math
n = d = m = e = 0x10001 p = 102634610559478918970860957918259981057327949366949344137104804864768237961662136189827166317524151288799657758536256924609797810164397005081733039415393 q = 7534810196420932552168708937019691994681052660068275906973480617604535381306041583841106383688654426129050931519275383386503174076258645141589911492908993 r = 10269028767754306217563721664976261924407940883784193817786660413744866184645984238866463711873380072803747092361041245422348883639933712733051005791543841
d1 = gmpy2.invert( e , (p-1)*(q-1)*(r-1) ) c1 = pow( m , d1 , n )
from sympy.ntheory.residue_ntheory import nthroot_mod c = nthroot_mod( c1 , 2 , r ) print( c ) print( long_to_bytes( c ) )
def tonelli(n, p): assert legendre(n, p) == 1 q = p - 1 s = 0 while q % 2 == 0: q //= 2 s += 1 if s == 1: return pow(n, (p + 1) // 4, p) for z in range(2, 10000): # for z in range(2, p): if p - 1 == legendre(z, p): break c = pow(z, q, p) r = pow(n, (q + 1) // 2, p) t = pow(n, q, p) m = s t2 = 0 while (t - 1) % p != 0: t2 = (t * t) % p for i in range(1, m): if (t2 - 1) % p == 0: break t2 = (t2 * t2) % p b = pow(c, 1 << (m - i - 1), p) r = (r * b) % p c = (b * b) % p t = (t * c) % p m = i return r n=7941371739956577280160664419383740967516918938781306610817149744988379280561359039016508679365806108722198157199058807892703837558280678711420411242914059658055366348123106473335186505617418956630780649894945233345985279471106888635177256011468979083320605103256178446993230320443790240285158260236926519042413378204298514714890725325831769281505530787739922007367026883959544239568886349070557272869042275528961483412544495589811933856131557221673534170105409 d=gmpy2.mpz(7515987842794170949444517202158067021118454558360145030399453487603693522695746732547224100845570119375977629070702308991221388721952258969752305904378724402002545947182529859604584400048983091861594720299791743887521228492714135449584003054386457751933095902983841246048952155097668245322664318518861440) cipher=1618155233923718966393124032999431934705026408748451436388483012584983753140040289666712916510617403356206112730613485227084128314043665913357106301736817062412927135716281544348612150328867226515184078966397180771624148797528036548243343316501503364783092550480439749404301122277056732857399413805293899249313045684662146333448668209567898831091274930053147799756622844119463942087160062353526056879436998061803187343431081504474584816590199768034450005448200
e = 46531 n = 16278524034278364842964386062476113517067911891699789991355982121084973951738324063305190630865511554888330215827724887964565979607808294168282995825864982603759381323048907814961279012375346497781046417204954101076457350988751188332353062731641153547102721113593787978587135707313755661153376485647168543680503160420091693269984008764444291289486805840439906620313162344057956594836197521501755378387944609246120662335790110901623740990451586621846212047950084207251595169141015645449217847180683357626383565631317253913942886396494396189837432429078251573229378917400841832190737518763297323901586866664595327850603 c = 14992132140996160330967307558503117255626925777426611978518339050671013041490724616892634911030918360867974894371539160853827180596100892180735770688723270765387697604426715670445270819626709364566478781273676115921657967761494619448095207169386364541164659123273236874649888236433399127407801843412677293516986398190165291102109310458304626261648346825196743539220198199366711858135271877662410355585767124059539217274691606825103355310348607611233052725805236763220343249873849646219850954945346791015858261715967952461021650307307454434510851869862964236227932964442289459508441345652423088404453536608812799355469 hint=int(binascii.hexlify(hint),16) assert(q1p*q1q==n) assert(q1p<q1q) assert(c==pow(hint,e,n))
def CRT( r , p ): M = 1 for i in p: M *= i m = [] for i in p: m.append( M // i ) ans = 0 for i in range( len(p) ): ans += r[i] * m[i] * invert( m[i] , p[i] ) ans %= M return ans
#part1 共膜攻击 def part1(): n = c = p4 = CRT( c , n ) p = iroot( p4 , 4 )[0] #print( p ) return p
对于第二部分:使用低密度指数攻击
很小,所以可以直接爆破
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import gmpy2 k = 0 while True: if gmpy2.iroot( ce1 + n*k , ee1 )[1] == True: e1 = gmpy2.iroot( ce1 + n*k , ee1 )[0] break k += 1 print( e1 ) k = 0 while True: if gmpy2.iroot( ce2 + n*k , ee2 )[1] == True: e2 = gmpy2.iroot( ce2 + n*k , ee2 )[0] - tmp break k += 1 print( e2 )
n = [ q1, q2 ] a = gmpy2.gcd(e1,(p-1)*(q1-1)) b = gmpy2.gcd(e2,(p-1)*(q2-1)) c = [ gmpy2.powmod(c1,gmpy2.invert(e1//a,(p-1)*(q1-1)),q1) , gmpy2.powmod(c2,gmpy2.invert(e2//b,(p-1)*(q2-1)),q2)] M = n[0] * n[1] m = [0]*2 inv = [0]*2 x = 0 for i in range(2): m[i]=M//n[i] inv[i]=gmpy2.invert(m[i],n[i]) x+=(c[i]*inv[i]*m[i]) x = x % M e=7 d=gmpy2.invert(e,(q1-1)*(q2-1)) flag=gmpy2.iroot(gmpy2.powmod(x,d,q1*q2),2)[0] print(long_to_bytes(flag))
flag:de1ctf{9b10a98b-71bb-4bdf-a6ff-f319943de21f}
[DASCTF Sept X
浙江工业大学秋季挑战赛]签到
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/env python # -*- coding: utf-8 -*- from Crypto.Util.number import * import random flag=b'flag{******************}' n = 2 ** 256 flaglong=bytes_to_long(flag) m = random.randint(2, n-1) | 1 c = pow(m, flaglong, n) print('m = ' + str(m)) print('c = ' + str(c))
# m = 73964803637492582853353338913523546944627084372081477892312545091623069227301 # c = 21572244511100216966799370397791432119463715616349800194229377843045443048821
discrete_log()使用示例:
1 2 3
>>> from sympy.ntheory import discrete_log >>> discrete_log(41, 15, 7) 3
即
1 2 3 4 5 6 7
import sympy import binascii m = 73964803637492582853353338913523546944627084372081477892312545091623069227301 c = 21572244511100216966799370397791432119463715616349800194229377843045443048821 n = 2 ** 256 flag=sympy.discrete_log(n,c,m) print(binascii.unhexlify(hex(flag)[2:]))