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
| from Crypto.Util.number import * import random p0 = 115792089210356248762697446949407573530086143415290314195533631308867097853951 a = 115792089210356248762697446949407573530086143415290314195533631308867097853948 b = 41058363725152142129326129780047268409114441015993725554835256314039467401291 E=EllipticCurve(GF(p0),[a,b]) P0=E(0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296,0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5) n=0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551 rlist1=[] for j in range(624): rlist1.append(random.getrandbits(32)) print(rlist1) s0=random.getrandbits(256) RMT=random.getrandbits(256) s0=s0%n p=getPrime(256) q=getPrime(256) assert p!=q x0=int(inverse_mod(q,n)) e1=(p*x0)%n
def GenRNG(): si=s0 p_point=p*P0 q_point=q*P0 Random_i=0 for j in range(8): si=int((p_point*si)[0]) ri=int((q_point*si)[0]) Random_i=ri&(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) r_point=si*p_point return hex(Random_i),r_point rand,point=GenRNG() print(int(rand,16)^^RMT)
def genPrime(): while True: a = random.getrandbits(256) b = random.getrandbits(256) if b % 3 == 0: continue p = int(a ** 2 + 3 * b ** 2) if p.bit_length() == 512 and p % 3 == 1 and isPrime(p): return p def add(P, Q, mod): m, n = P p, q = Q if p is None: return P if m is None: return Q if n is None and q is None: x = m * p % mod y = (m + p) % mod return (x, y) if n is None and q is not None: m, n, p, q = p, q, m, n if q is None: if (n + p) % mod != 0: x = (m * p + 2) * inverse(n + p, mod) % mod y = (m + n * p) * inverse(n + p, mod) % mod return (x, y) elif (m - n ** 2) % mod != 0: x = (m * p + 2) * inverse(m - n ** 2, mod) % mod return (x, None) else: return (None, None) else: if (m + p + n * q) % mod != 0: x = (m * p + (n + q) * 2) * inverse(m + p + n * q, mod) % mod y = (n * p + m * q + 2) * inverse(m + p + n * q, mod) % mod return (x, y) elif (n * p + m * q + 2) % mod != 0: x = (m * p + (n + q) * 2) * inverse(n * p + m * q + r, mod) % mod return (x, None) else: return (None, None) def power(P, a, mod): res = (None, None) t = P while a > 0: if a % 2: res = add(res, t, mod) t = add(t, t, mod) a >>= 1 return res def random_pad(msg, ln): pad = bytes([random.getrandbits(8) for _ in range(ln - len(msg))]) return msg + pad
p, q = genPrime(), genPrime() N = p * q phi = (p ** 2 + p + 1) * (q ** 2 + q + 1) print(f"N: {N}") d = getPrime(400) e2 = inverse(d, phi) k = (e * d - 1)/phi print("e2:",e2) to_enc=long_to_bytes(e1) ln = len(to_enc) print(f"Length: {ln}") pt1, pt2 = random_pad(to_enc[: ln // 2], 127), random_pad(to_enc[ln // 2 :], 127) M = (bytes_to_long(pt1), bytes_to_long(pt2)) E = power(M, e2, N) print(f"E: {E}")
flag=b"flag{**********}" m=bytes_to_long(flag) c=m^^int(point[0]) print("c:",c)
|