Skip to content

Instantly share code, notes, and snippets.

@strategicpause
Created November 8, 2017 14:17
Show Gist options
  • Save strategicpause/bf2bfd93602a60427cd3e28f47d273e6 to your computer and use it in GitHub Desktop.
Save strategicpause/bf2bfd93602a60427cd3e28f47d273e6 to your computer and use it in GitHub Desktop.
from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from base64 import b64encode
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
plaintext = "Hello".encode("UTF8")
print("Plaintext Message:")
print(plaintext)
cipher = PKCS1_OAEP.new(key)
ciphertext = cipher.encrypt(plaintext)
print("\nCiphertext Message:")
print(b64encode(ciphertext))
digest = SHA256.new(ciphertext)
signer = PKCS1_v1_5.new(key)
signature = signer.sign(digest)
print("\nSignature:")
print(b64encode(signature))
print("\nSignature Verified?")
print(signer.verify(digest, signature))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment