Skip to content

Instantly share code, notes, and snippets.

@lkdocs
Last active October 22, 2018 05:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lkdocs/6519270 to your computer and use it in GitHub Desktop.
Save lkdocs/6519270 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 Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
key = open(public_key_loc, "r").read()
rsakey = RSA.importKey(key)
rsakey = PKCS1_OAEP.new(rsakey)
encrypted = rsakey.encrypt(message)
return encrypted.encode('base64')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment