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
| import gmpy2 from Crypto.Util.number import *
def broadcast_attack(data): def extended_gcd(a,b): x,y = 0,1 lastx,lasty = 1,0 while b: a,(q,b) = b,divmod(a,b) x,lastx = lastx-q*x,x y,lasty = lasty-q*y,y return (lastx,lasty,a) def chinese_remaindor_theorem(items): N = 1 for a,n in items: N *= n result = 0 for a,n in items: m = N//n r,s,d = extended_gcd(n,m) if d != 1: N = N//n continue result += a*s*m return result%N ,N x, n = chinese_remaindor_theorem(data) return x
a = 156853895847604116708242664263151514811095704969640303272039451331791888050995073274981545693518063639560286348739938318495685137088495867703518198511200409009953879436648706837731243061114851474801565873584183542649886358523850682697732574913523360866915083642887238043256280849100274825940626065115676325169 c = 3459715117165130065996389169943285249501133832272446001239391765859259811270526185228996906338576254353123756173289118671028939933226544773197852424767051933844004667155191851195814295922794480300237399956789038592856532530692732011427288405114650955620859282144504446058845961744702163836107847961388150810
tmp = int(gmpy2.iroot(a, 2)[0]) q = gmpy2.next_prime(tmp) p = a // q assert p*q == a and p < q and isPrime(p) e = 65537 d = gmpy2.invert(e, (p-1)*(q-1)) m = pow(c, d, a) assert pow(m, e, a) == c mp = (-m) % p mq = (-m) % q for i in range(p, q): mq = (mq*i) % q
data = [(mp,p),(mq,q)] f = broadcast_attack(data) print(long_to_bytes(f)) '''code 2 from gmpy2 import invert from Crypto.Util.number import long_to_bytes from functools import reduce a = 156853895847604116708242664263151514811095704969640303272039451331791888050995073274981545693518063639560286348739938318495685137088495867703518198511200409009953879436648706837731243061114851474801565873584183542649886358523850682697732574913523360866915083642887238043256280849100274825940626065115676325169 c = 3459715117165130065996389169943285249501133832272446001239391765859259811270526185228996906338576254353123756173289118671028939933226544773197852424767051933844004667155191851195814295922794480300237399956789038592856532530692732011427288405114650955620859282144504446058845961744702163836107847961388150810 p = 12524132538727147976454683542869473176723933298740764667643646868771435739017779964538584767004546975861773724104399289331497224456192089483187179727966053 q = 12524132538727147976454683542869473176723933298740764667643646868771435739017779964538584767004546975861773724104399289331497224456192089483187179727966173 e = 65537 assert p < q m = pow(c,int(invert(e,(p-1)*(q-1))),a) flag1 = -m % p flag2 = -m*reduce(lambda x,y:x*y,range(p,q)) % q flag = (flag1*q*int(invert(q,p)) + flag2*p*invert(p,q)) % a print(long_to_bytes(flag)) '''
|