Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ashutosh1206
GitHub Repository: ashutosh1206/crypton
Path: blob/master/RSA-encryption/Factorisation-Pollard's_p-1/Challenges/handicraft_rsa/handicraft_rsa.py
1402 views
1
#!/usr/bin/python
2
3
from Crypto.Util.number import *
4
from Crypto.PublicKey import RSA
5
from secret import s, FLAG
6
7
def gen_prime(s):
8
while True:
9
r = getPrime(s)
10
R = [r]
11
t = int(5 * s / 2) + 1
12
for i in range(0, t):
13
R.append(r + getRandomRange(0, 4 * s ** 2))
14
p = reduce(lambda a, b: a * b, R, 2) + 1
15
if isPrime(p):
16
if len(bin(p)[2:]) == 1024:
17
return p
18
19
while True:
20
p = gen_prime(s)
21
q = gen_prime(s)
22
n = p * q
23
e = 65537
24
d = inverse(e, (p-1)*(q-1))
25
if len(bin(n)[2:]) == 2048:
26
break
27
28
msg = FLAG
29
key = RSA.construct((long(n), long(e), long(d), long(p), long(p)))
30
for _ in xrange(s):
31
enc = key.encrypt(msg, 0)[0]
32
msg = enc
33
34
print key.publickey().exportKey()
35
print '-' * 76
36
print enc.encode('base64')
37
print '-' * 76
38
39