Path: blob/master/RSA-encryption/Intro-Challenges/Challenge-2/encrypt.py
1402 views
from Crypto.Util.number import *1from Crypto.PublicKey import RSA2import gmpy23from secret import flag45p = getPrime(512)6q = getPrime(512)7n = p*q8# size() from Crypto.Util.number tells the size of the number (in bits)9size1 = size(n)1011e = 312assert GCD(e, (p-1)*(q-1)) == 11314m = bytes_to_long(flag)15ciphertext = long_to_bytes(pow(m, e, n))16ciphertext = ciphertext.encode("hex")1718obj1 = open("ciphertext.txt",'w')19obj1.write(ciphertext)2021publickey = RSA.construct((n, long(e)))22# Read this documentation on how to construct and import PEM files using pycrypto23# Public Key encryption: https://www.dlitz.net/software/pycrypto/api/2.6/toc-Crypto.PublicKey.RSA-module.html24# Construct PEM files: https://www.dlitz.net/software/pycrypto/api/2.6/Crypto.PublicKey.RSA-module.html#construct25# Import PEM files: https://www.dlitz.net/software/pycrypto/api/2.6/Crypto.PublicKey.RSA-module.html#importKey26f = open("publickey.pem",'w')27f.write(publickey.exportKey("PEM"))2829