2 from Crypto.Util.number import *
7 r = random.randint(0, nbit-1)
15 rbit += (R[i+1] - R[i] - 1) * '0' + '1'
16 rbit += (nbit - R[-1] - 1) * '0'
20 n = len(bin(p)[2:]) # n = 229
21 f, skey = gen_rand(n, l), gen_rand(n, l)
22 pkey = f * inverse(skey, p) % p # Return the inverse of u mod v.
23 return (p, pkey), skey
25 def encrypt(msg, pkey):
27 msg, enc, n = bytes_to_long(msg), [], len(bin(p)[2:])
28 for b in bin(msg)[2:]:
29 s, t = gen_rand(n, l), gen_rand(n, l)
38 pkey, skey = genkey(p, l)
39 enc = encrypt(long_to_bytes(0b1010100001111), pkey)
42 s = ((check - t) * inverse(g, p)) % p
45 p = 862718293348820473429344482784628181556388621521298319395315527974911