Skip to content

Instantly share code, notes, and snippets.

@danemacaulay
Created June 18, 2018 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danemacaulay/4090cde8017ef11124dca1f7eb0b1789 to your computer and use it in GitHub Desktop.
Save danemacaulay/4090cde8017ef11124dca1f7eb0b1789 to your computer and use it in GitHub Desktop.
import ast
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
with open('cert/referral', 'rb') as f: key_text = f.read()
privkey = RSA.importKey(key_text)
publickey = privkey.publickey()
encryptor = PKCS1_OAEP.new(publickey)
decryptor = PKCS1_OAEP.new(privkey)
def encrypt(msg):
bit_msg = bytes(msg, encoding= 'utf-8')
encrypted = encryptor.encrypt(bit_msg)
return encrypted
def decrypt(encrypted):
decrypted = decryptor.decrypt(ast.literal_eval(str(encrypted)))
return decrypted.decode('ascii')
if __name__ == "__main__":
msg = 'hi'
en = encrypt(msg)
de = decrypt(en)
assert msg == de
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment