Skip to content

Instantly share code, notes, and snippets.

@andrisasuke
Created May 2, 2017 10:15
Show Gist options
  • Save andrisasuke/6cb289458b57e6ceb0e93c62bebd95d3 to your computer and use it in GitHub Desktop.
Save andrisasuke/6cb289458b57e6ceb0e93c62bebd95d3 to your computer and use it in GitHub Desktop.
Python encryption-decryption RSA using M2Crypto
import base64
from M2Crypto import RSA, BIO
key = open('/path/public_key.pem', 'r').read()
pubkey = str(key).encode('utf8')
bio = BIO.MemoryBuffer(pubkey)
rsa = RSA.load_pub_key_bio(bio)
encrypted = rsa.public_encrypt('hello', RSA.pkcs1_oaep_padding)
print 'encrypted text ', encrypted.encode('base64')
key = open('/path/private_key.pem', "r").read()
priv_key = BIO.MemoryBuffer(key.encode('utf8'))
key = RSA.load_key_bio(priv_key)
decrypted = key.private_decrypt(base64.b64decode(encrypted), RSA.pkcs1_oaep_padding)
print 'decrypted text ', decrypted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment