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.

174 Chapter 6 ■ Using Cryptography with Images

Figure 6.3: Side by side of ch6_secret_image.jpg and d_ch6_secret_image.jpg

Images and Cryptography Libraries

Let’s change our focus to implementing libraries that will make our cryptography

lives a little easier. We will revisit the Cryptography library that we introduced

in Chapter 1, and we will introduce the Cryptosteganography library that will

give us the ability to explore steganography using Python later in this chapter.

Understanding the Cryptography Library

Cryptography is the name of a Python package that provides cryptographic

primitives and recipes. The library boasts to be your “cryptographic standard

library,” and it currently supports various versions in Python. To install the

library, perform a pip install cryptography.

The Cryptography library includes both high-level recipes and low-level

interfaces to common cryptographic algorithms such as symmetric ciphers,

message digests, and key derivation functions. The Fernet example shown next

highlights an example of a high-level interface. Check out the following quick

sample code to ensure that you have the Cryptography library installed:

from cryptography.fernet import Fernet

key = Fernet.generate_key()

f = Fernet(key)

print ("The key is %s", f)

print ()

ciphertext = f.encrypt(b"This is a secret message.")

print (ciphertext)

print ()

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

Saved successfully!

Ooh no, something went wrong!