07.07.2023 Views

Implementing-cryptography-using-python

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

8 Chapter 1 ■ Introduction to Cryptography and Python

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

interfaces to common cryptographic algorithms such as symmetric ciphers, message

digests, and key derivation functions. For example, to encrypt something

with Cryptography’s high-level symmetric encryption recipe, use this:

>>> from cryptography.fernet import Fernet

>>> # Put this somewhere safe!

>>> key = Fernet.generate_key()

>>> f = Fernet(key)

>>> token = f.encrypt(b"A really secret message. Not for prying eyes.")

>>> token

'...'

>>> f.decrypt(token)

'A really secret message. Not for prying eyes.'

Installing Additional Packages

IPython is available through many providers; it is an enhanced, interactive

version of Python. IPython offers a combination of convenient shell features,

special commands, and a history mechanism for both input and output. IPython

offers a vastly improved set of functionality and flexibility; it is a fully compatible

replacement for the standard Python interpreter. You can install IPython

by typing the following:

sudo apt install IPython3

apt-get install python3-IPython

To use IPython, type IPython3 -h at the system command line. You can start

the shell by typing this:

~$ IPython3

You should see something similar to the following:

Python 3.5.3 (default, Sep 27 2018, 17:25:39)

Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python

In [1]:

Please note that you will need to pay attention to the version of Python; this

example is using Python 3.5.3, but you may see something like Python 2.x if you

did not specify IPython3. When Python 3 first came out, many libraries didn’t

exist, so many people stayed with 2.x. Python 3 has caught up and is now a

great language. There are, however, a few differences between Python 2.x and

Python 3, so if you find that some of your scripts fail, try running them using

an alternate version.

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

Saved successfully!

Ooh no, something went wrong!