Skip to content

Instantly share code, notes, and snippets.

@MrSimsek
Created October 7, 2017 07:29
Show Gist options
  • Save MrSimsek/b24ae7723be498f92a1d662e06506e97 to your computer and use it in GitHub Desktop.
Save MrSimsek/b24ae7723be498f92a1d662e06506e97 to your computer and use it in GitHub Desktop.
A Public Key Cryptography Story
from Crypto.PublicKey import RSA
# Ross created a public-private key pair himself
ross_key = RSA.generate(2048)
# He seperated his public key from the pair
ross_pubkey = ross_key.publickey()
# A key pair for Rachel, NORMALY THIS MUST BE CREATED BY HER!
rachel_key = RSA.generate(2048)
# Rachel's public key
rachel_pubkey = rachel_key.publickey()
# We will assume Ross only knows "rachel_pubkey", he does not know "rachel_key" (Rachel's Key Pair)
# Because "rachel_key" object has the private key information, you can confirm it like this;
# rachel_key.has_private() --> True
# rachel_pubkey.has_private() --> False
# Ross's message
message = "I love you Rachel!"
# He encrypts it with Rachel's public key
encrypted_message = rachel_pubkey.encrypt(message, '')
print "How everybody sees the message: ", encrypted_message
# Rachel can decrypt the message using her private key,
# Remember: "rachel_key" object has the private key information
decrypted_message = rachel_key.decrypt(encrypted_message)
print "How Rachel sees the message: ", decrypted_message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment