Last active
October 7, 2017 09:28
-
-
Save MrSimsek/53f44413179d28be5b4b55c033dadc3a to your computer and use it in GitHub Desktop.
The PKC Story Continues
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
from Crypto.PublicKey import RSA | |
from Crypto.Hash import SHA256 | |
ross_key = RSA.generate(2048) | |
ross_pubkey = ross_key.publickey() | |
# NORMALY THIS MUST BE CREATED ON THE RECIPIENT SIDE! | |
# Because it involves PRIVATE KEY INFORMATION! | |
rachel_key = RSA.generate(2048) | |
# But PUBLIC KEY can be known by everbody | |
rachel_pubkey = rachel_key.publickey() | |
# Ross's message | |
message = "I love you Rachel!" | |
# First Requirement: Only Rachel can see it | |
encrypted_message = rachel_pubkey.encrypt(message, '') | |
print ("\nHow everybody sees the message: " + repr(encrypted_message) + "\n") | |
decrypted_message = rachel_key.decrypt(encrypted_message) | |
print ("How Rachel sees the message: " + decrypted_message + "\n") | |
# Second Requirement: Prove that it is sent from Ross | |
# Ross will disturb the message | |
ross_message_hash = SHA256.new(message).hexdigest() | |
print "Ross's hashed message: ", ross_message_hash | |
# Then he will sign the message with his private key | |
ross_signature = ross_key.sign(ross_message_hash, '') | |
# When Rachel decrypted the message, she will hash it like Ross did | |
rachel_message_hash = SHA256.new(decrypted_message).hexdigest() | |
print "Rachel's hashed message: ", rachel_message_hash | |
# Then she will check the signature with her hashed message | |
if (ross_pubkey.verify(rachel_message_hash, ross_signature)): | |
print "\nHash values match. It is from Ross." | |
else: | |
print "\nHash values not match. It is not from Ross." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment