07.07.2023 Views

Implementing-cryptography-using-python

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8 ■ Cryptographic Applications and PKI 229

print ("\nDecrypting message with your personal private key ",

private ," . . .")

print ("\nYour message is: {}\n\n".format(decrypt(private,

encrypted_msg)))

To produce the output in Figure 8.1, enter 11 as your first prime and 17 as

your second.

Figure 8.1: Understanding RSA

Generating RSA Certificates

For our next example, we will utilize the PyCrypto library and import the RSA

module. In the next code listing, you will generate a new RSA key and then

export the private and public aspects of the key into variables that are then

saved as certificates to disk. The keys here are much larger than used in the

previous example, which makes them much harder to crack. You may notice

that this program is also much slower due to the increased time it takes to find

the coprime key pair as the key size increases:

#ch8_Generate_RSA_Certs.py

from Crypto.PublicKey import RSA

#Generate a public/private key pair using 4096 bits key length (512

bytes)

new_key = RSA.generate(4096, e=65537)

#The private key in PEM format

private_key = new_key.exportKey("PEM")

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!