SHANNON W. BRAY CASP, Security +, CISM is the Virtual CISO at Secured Systems providing clients guidance on establishing and maintaining information security plans, policies, procedures, standards, and guidelines using accepted frameworks. He has worked in this roles and others in corporations and federal agencies. Shannon has an MS in Cybersecurity from the University of Delaware. Prior to transitioning to cybersecurity, he was a well known IT expert with several successful books about Microsoft SharePoint.
Introduction to Cryptography and Python
Cryptography is one of the most important tools we have at our disposal as information security professionals. It provides us with the ability to protect sensitive information from unauthorized disclosure through encryption. Cryptography is the use of mathematical algorithms that can be used to transform data either into an encrypted form (ciphertext) or into its decrypted form (plaintext). The purpose of these algorithms can be quite complicated. The goal of this book is to help simplify the use of cryptography using the available libraries in Python, so you will begin your journey into cryptography by setting up a Python environment. You'll also get a review of using Python, and then you'll write your first cipher using Python. Specifically, you'll do the following:
- Gain an understanding of algorithms
- Explore various Python installations
- Set up Python 3 on various machines
- Explore the basics of the Python language
- Write your first cipher using Python
The algorithms you will explore in this book can be quite sophisticated, but most of the logic is encapsulated into little black boxes that allow you to interface with the algorithms using functions. In the programming or mathematical world, a function is merely a way to enter values and receive output. When using algorithms in cryptography, we generally have two inputs for encryption and two inputs for decryption:
- The encryption process will take the plaintext message (P) along with an encryption key (K) and then run the plaintext through encryption algorithms, which will return ciphertext (C).
- On the decryption side, the ciphertext (C) will be supplied along with the encryption key (K), which will produce the plaintext (P) message.
As you study various encryption themes throughout this book, you will find that they are described by three algorithms: GEN for key generation, ENC for the encryption algorithm, and DEC for the decryption algorithm. You'll revisit this concept at the end of this chapter when you learn how to create a Python function.
Why Use Python?
Using Python for cryptography is simpler than using languages such as C or C++; while free libraries such as OpenSSL are available, their use can be quite complex. Python removes these complexities with many built-in libraries that aid in cryptography scripting. It is also a great choice because Python is free in terms of license. Python can be described as an open-source, general-purpose language that is object oriented, functional, and procedural, and it allows for the interface with C/ObjC/Java/Fortran and even .NET. A number of versions are available, from 2.5.x through 3.x. Currently, 3.x is becoming more popular as new libraries are introduced. Python comes preinstalled with Linux and macOS, and you can install it on Windows as well. You will learn how to install Python in each of these environments later in this chapter.
Because Python is available for Windows, Linux/Unix, Mac, and Chromebooks, among others, the lessons you learn here will be portable to several environments. Python has proven to be quite powerful in information security and can be used to quickly script solutions to help you become a better security practitioner. If you are using Ubuntu Linux, every command you see in this book will work. If you are on a Mac, you can use the Terminal app found inside the Utilities folder. On a Windows machine, you have several options as well: you can install a number of tools such as Cygwin, Visual Studio, or PyCharm; use the Windows 10 Linux subsystem; or just install Ubuntu Linux from the Microsoft Store. If you are new to Linux, I recommend practicing your command-line skills a bit to help get you comfortable.
When you start the Python shell (by typing
python3 at the command line), you will see the version and the date associated with the version. For instance, if you install and run Python in a Linux environment, you should see something similar to the following:
Python 3.7.4+ (default, Sep 4 2019, 08:03:05)
[GCC 9.2.1 20190827] on linux
Type "help", "copyright", "credits" or "license" for more information.
The majority of Python interpreters use
>>> as a prompt to accept user input; one notable exception is IPython, which is discussed briefly in a moment. Once you are in a Python shell, exit by pressing CTRL+D or typing
exit(). Python files use a
.py extension, and we start their execution in Linux environments by typing
python3 filename.py; (keep in mind that you still need to
chmod +x the file).
NOTE In Unix-like operating systems, the
chmod command sets the permissions of files or directories.
As you will learn later in this chapter, a large number of modules are available for Python, many of which we will use along your journey of learning cryptography. One of the more notable modules you will learn more about is NumPy. NumPy offers numerical operations to Python, including fast multidimensional array operations, random number generation, and linear algebra. Another module that you will be introduced to is Matplotlib, which is an excellent library for plotting. In Chapter 6, you will learn more about PyFITS, which is a module that provides access to Flexible Image Transport System (FITS) files. FITS is a portable file standard that is widely used throughout the astronomy community to store images and tables. We will be combining it with our cryptography techniques to encrypt and decrypt image files.
Downloading and Installing Python
You can download the Python interpreter from
www.python.org/python, where you'll find versions for Ubuntu, macOS, and Windows. Be sure to download version 3.7 or higher, since Python 2 is now unsupported.
Installing on Ubuntu
If you are running Ubuntu, install Python from the Ubuntu Software Center using these steps:
- Open the Ubuntu Software Center.
- In the search box in the top-right corner of the window, type Python.
- Select IDLE.
- Click Install.
Installing on macOS
If you are using macOS, download the
.dmg file for your version of macOS and execute it. When the DMG package opens, do the following:
- Double-click the Python package file (
- Click Continue through the Welcome section and click Agree to accept the license.
- Select HD Macintosh and click Install.
Installing on Windows
If you are using Windows, download and run the Python installer; it should have an
.msi extension. Then follow these steps:
- Click Install Now to begin the installation.
- When the installation completes, click Close.
Notice the mention of upgrading. You can upgrade a package by clearing out the Search PyPI and Installed Packages search box. This will show you all of the installed packages. Any package that has an update will have an up arrow next to it along with the newer version number (see Figure 1.1). You may have to elevate your privileges.
Installing on a Chromebook
New Windows and Chrome OS computers give you the ability to install Linux. The Chrome OS, which is becoming more popular, enables you to install a Linux container that you can interact with but also keeps the rest of the environment isolated so that you do not have to worry about messing up your system.
Figure 1.1: Upgrading packages
To get started with Python 3 on a Chromebook, click the time in the lower-right corner. If you have the appropriate permissions on the Chromebook, you should see a Settings button that looks like a flower with a dot in the middle of it. Once you are in Settings, you can scroll down until you find Linux (Beta) and then enable it by clicking Turn On. It may take several minutes, but once it is complete, you will be able to run Linux tools, editors, and IDEs on your Chromebook. To continue with the Chromebook installation, see the next section for instructions on how to install NumPy and Matplotlib.
Installing Additional Packages
Many of the coding recipes that you will find in this book require additional packages or modules to help reduce the amount of code you need to write to ensure message security. Most Python interpreters use a command-line approach of loading the packages. Integrated development environments (IDEs) may have unique ways of loading packages for each interpreter available.
Installing Pip, NumPy, and Matplotlib
Ensure you are in a command shell environment with a prompt. In the following example installation, you will get an update to the packages that come with your Python version.
Pip, or Pip3 in this case, is the package management system for Python; it is used to install and manage software packages written in Python. Pip is similar to tools like Bundle, NPM, and Composer in other programming languages. NumPy, which stands for Numerical Python, is a Python package that is the core library for scientific computing. It contains a powerful n-dimensional array object, and...