Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ashutosh1206
GitHub Repository: ashutosh1206/crypton
Path: blob/master/RSA-encryption/Intro-Challenges/Challenge-2/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
p = getPrime(512)
7
q = getPrime(512)
8
n = p*q
9
# size() from Crypto.Util.number tells the size of the number (in bits)
10
size1 = size(n)
11
12
e = 3
13
assert GCD(e, (p-1)*(q-1)) == 1
14
15
m = bytes_to_long(flag)
16
ciphertext = long_to_bytes(pow(m, e, n))
17
ciphertext = ciphertext.encode("hex")
18
19
obj1 = open("ciphertext.txt",'w')
20
obj1.write(ciphertext)
21
22
publickey = RSA.construct((n, long(e)))
23
# Read this documentation on how to construct and import PEM files using pycrypto
24
# Public Key encryption: https://www.dlitz.net/software/pycrypto/api/2.6/toc-Crypto.PublicKey.RSA-module.html
25
# Construct PEM files: https://www.dlitz.net/software/pycrypto/api/2.6/Crypto.PublicKey.RSA-module.html#construct
26
# Import PEM files: https://www.dlitz.net/software/pycrypto/api/2.6/Crypto.PublicKey.RSA-module.html#importKey
27
f = open("publickey.pem",'w')
28
f.write(publickey.exportKey("PEM"))
29