Posts

Showing posts from April, 2020

RSA encryption in python 🐍

Image
RSA Encryption in Python 3.8 First install the dependencies ie pycryptodome library [pycryptodome 3.9.7]: pip3 install pycryptodome from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP import binascii keyPair = RSA.generate(3072) pubKey = keyPair.publickey() pubKey = pubKey.exportKey() pubKey = pubKey.decode('utf-8') privKey = keyPair.exportKey() privKey = privKey.decode('utf-8') key = RSA.importKey(pubKey) msg = b'A message for encryption' encryptor = PKCS1_OAEP.new(key) encrypted = encryptor.encrypt(msg) encrypted = binascii.hexlify(encrypted) encrypted = encrypted.decode('utf-8') print(encrypted) key = RSA.importKey(privKey) decryptor = PKCS1_OAEP.new(key) encrypted = encrypted.encode('utf-8') encrypted = binascii.unhexlify(encrypted) decrypted = decryptor.decrypt(encrypted) decrypted = decrypted.decode('utf-8') print('Decrypted:', decrypted)