Created
April 14, 2018 15:23
-
-
Save RichardEllicott/2f896905e14ec7090e9e476d166095a8 to your computer and use it in GitHub Desktop.
very simple RSA example using the pycrypto module
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
''' | |
pip install pycrypto | |
''' | |
from Crypto.PublicKey import RSA | |
from Crypto import Random | |
import ast | |
random_generator = Random.new().read | |
key = RSA.generate(2048, random_generator) # generate pub and priv key | |
publickey = key.publickey() # pub key export for exchange | |
encrypted = publickey.encrypt('encrypt this message', 32) | |
# message to encrypt is in the above line 'encrypt this message' | |
print ('encrypted message:', encrypted) # ciphertext | |
f = open('encryption.txt', 'w') | |
f.write(str(encrypted)) # write ciphertext to file | |
f.close() | |
# decrypted code below | |
f = open('encryption.txt', 'r') | |
message = f.read() | |
decrypted = key.decrypt(ast.literal_eval(str(encrypted))) | |
print ('decrypted', decrypted) | |
f = open('encryption.txt', 'w') | |
f.write(str(message)) | |
f.write(str(decrypted)) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment