Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ashutosh1206
GitHub Repository: ashutosh1206/crypton
Path: blob/master/RSA-encryption/Intro-Challenges/C4-reloaded/encrypt.py
1402 views
1
from Crypto.Util.number import *
2
from Crypto.PublicKey import RSA
3
import gmpy2
4
from secret import flag
5
6
7
p = getPrime(512)
8
9
i = 10000
10
while True:
11
q = p + i
12
if isPrime(q):
13
break
14
i += 1
15
print "i: ", i
16
n = p*q
17
e = 65537
18
phin = (p-1)*(q-1)
19
assert GCD(e, phin) == 1
20
21
d = inverse(e, phin)
22
23
# Message/Flag to be encrypted
24
m = bytes_to_long(flag)
25
ciphertext = long_to_bytes(pow(m, e, n))
26
ciphertext = ciphertext.encode("hex")
27
28
obj1 = open("ciphertext.txt",'w')
29
obj1.write(ciphertext)
30
31
key = RSA.construct((n, long(e)))
32
f = open("publickey.pem",'w')
33
f.write(key.exportKey("PEM"))
34