Skip to content

Instantly share code, notes, and snippets.

@lkdocs
Last active March 28, 2018 07:19
Show Gist options
  • Save lkdocs/6519328 to your computer and use it in GitHub Desktop.
Save lkdocs/6519328 to your computer and use it in GitHub Desktop.
def encrypt_RSA(public_key_loc, message):
'''
param: public_key_loc Path to public key
param: message String to be encrypted
return base64 encoded encrypted string
'''
from M2Crypto import RSA, BIO
key = open(public_key_loc, "r").read()
pubkey = str(key).encode('utf8')
bio = BIO.MemoryBuffer(pubkey)
rsa = RSA.load_pub_key_bio(bio)
encrypted = rsa.public_encrypt(message, RSA.pkcs1_oaep_padding)
return encrypted.encode('base64')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment