Created
May 2, 2017 10:15
-
-
Save andrisasuke/6cb289458b57e6ceb0e93c62bebd95d3 to your computer and use it in GitHub Desktop.
Python encryption-decryption RSA using M2Crypto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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