Bookmark and Share

Friday, September 15, 2017

Working with public/private certificates using Python

If you need to sign a message using the public/private key technology, Python is your best friend.

Here is in few lines the creation of public/private keys, a sign of a message using a private key and the check using the public key.

First of all install pycryptodomex library using pip.

Next here is the code:


from Cryptodome.PublicKey import RSA
from Cryptodome.Signature import PKCS1_v1_5
from Cryptodome.Hash import SHA256

privatekey = RSA.generate(1024)
privatesigner = PKCS1_v1_5.new(privatekey) 
publicsigner = PKCS1_v1_5.new(privatekey.publickey()) 
message = b'Python rocks!'
private_message_sign = privatesigner.sign(SHA256.new(message))
self.assertTrue(publicsigner.verify(SHA256.new(message),private_message_sign))