+353-1-416-8900REST OF WORLD
+44-20-3973-8888REST OF WORLD
1-917-300-0470EAST COAST U.S
1-800-526-8630U.S. (TOLL FREE)

Implementing Cryptography Using Python. Edition No. 1

  • Book

  • 304 Pages
  • September 2020
  • John Wiley and Sons Ltd
  • ID: 5837488

Learn to deploy proven cryptographic tools in your applications and services

Cryptography is, quite simply, what makes security and privacy in the digital world possible. Tech professionals, including programmers, IT admins, and security analysts, need to understand how cryptography works to protect users, data, and assets. Implementing Cryptography Using Python will teach you the essentials, so you can apply proven cryptographic tools to secure your applications and systems. Because this book uses Python, an easily accessible language that has become one of the standards for cryptography implementation, you’ll be able to quickly learn how to secure applications and data of all kinds.

In this easy-to-read guide, well-known cybersecurity expert Shannon Bray walks you through creating secure communications in public channels using public-key cryptography. You’ll also explore methods of authenticating messages to ensure that they haven’t been tampered with in transit. Finally, you’ll learn how to use digital signatures to let others verify the messages sent through your services.

  • Learn how to implement proven cryptographic tools, using easy-to-understand examples written in Python
  • Discover the history of cryptography and understand its critical importance in today’s digital communication systems
  • Work through real-world examples to understand the pros and cons of various authentication methods
  • Protect your end-users and ensure that your applications and systems are using up-to-date cryptography

Table of Contents

Introduction xvii

Chapter 1 Introduction to Cryptography and Python 1

Exploring Algorithms 2

Why Use Python? 2

Downloading and Installing Python 3

Installing on Ubuntu 4

Installing on macOS 4

Installing on Windows 4

Installing on a Chromebook 4

Installing Additional Packages 5

Installing Pip, NumPy, and Matplotlib 6

Installing the Cryptography Package 7

Installing Additional Packages 8

Testing Your Install 9

Diving into Python Basics 9

Using Variables 10

Using Strings 11

Introducing Operators 11

Understanding Arithmetic Operators 11

Understanding Comparison Operators 13

Understanding Logical Operators 13

Understanding Assignment Operators 14

Understanding Bitwise Operators 15

Understanding Membership Operators 15

Understanding Identity Operators 16

Using Conditionals 16

Using Loops 17

for 17

while 18

continue 18

break 18

else 18

Using Files 19

Understanding Python Semantics 20

Sequence Types 20

Introducing Custom Functions 26

Downloading Files Using Python 27

Introducing Python Modules 28

Creating a Reverse Cipher 29

Summary 30

Chapter 2 Cryptographic Protocols and Perfect Secrecy 31

The Study of Cryptology 32

Understanding Cryptography 32

Cryptography’s Famous Family: Alice and Bob 33

Diffie-Hellman 34

Data Origin Authentication 34

Entity Authentication 35

Symmetric Algorithms 36

Asymmetric Algorithms 36

The Needham-Schroeder Protocols 36

The Otway-Rees Protocol 38

Kerberos 39

Multiple-Domain Kerberos 40

X.509 41

Formal Validation of Cryptographic Protocols 46

Configuring Your First Cryptographic Library 47

Understanding Cryptanalysis 47

Brute-Force Attacks 47

Side-Channel Attacks 48

Social Engineering 48

Analytical Attacks 48

Frequency Analysis 48

Attack Models 49

Shannon’s Theorem 50

One-Time Pad 51

XOR, AND, and OR 51

One-Time Pad Function 56

One-Way Hashes 58

Cryptographic One-Way Hashes 59

Message Authentication Codes 60

Perfect Forward Secrecy 60

Published and Proprietary Encryption Algorithms 61

Summary 62

References 62

Chapter 3 Classical Cryptography 65

Password Best Practices 66

Password Storage 66

Hashing Passwords 67

Salting Passwords 67

Stretching Passwords 68

Password Tools 68

Obfuscating Data 69

ASCII Encoding 70

Base64 Encoding Text 70

Binary Data 72

Decoding 72

Historical Ciphers 72

Scytale of Sparta 73

Substitution Ciphers 73

Caesar Cipher 74

ROT-13 76

Atbash Cipher 77

Vigenère Cipher 77

Playfair 79

Hill 2x2 83

Column Transposition 87

Affine Cipher 90

Summary 93

Chapter 4 Cryptographic Math and Frequency Analysis 95

Modular Arithmetic and the Greatest Common Devisor 96

Prime Numbers 97

Prime Number Theorem 98

School Primality Test 98

Fermat’s Little Theorem 100

Miller-Rabin Primality Test 100

Generate Large Prime Numbers 104

Basic Group Theory 106

Orders of Elements 107

Modular Inverses 109

Fermat’s Little Theorem to Find the Inverse 110

Extending the GCD 111

Euler’s Theorem 111

Pseudorandomness 115

Breaking C’s rand() Function 116

Solving Systems of Linear Equations 117

Frequency Analysis 120

Cryptanalysis with Python 123

Using an Online Word List 125

Determining the Frequency 126

Breaking the Vigenère Cipher 129

Summary 138

Chapter 5 Stream Ciphers and Block Ciphers 139

Convert between Hexdigest and Plaintext 140

Use Stream Ciphers 141

ARC4 147

Vernam Cipher 148

Salsa20 Cipher 149

ChaCha Cipher 151

Use Block Ciphers 156

Block Modes of Operations 158

ECB Mode 158

CBC Mode 159

CFB Mode 160

OFB Mode 162

CTR Mode 163

Tricks with Stream Modes 164

DIY Block Cipher Using Feistel Networks 165

Advanced Encryption Standard (AES) 167

Using AES with Python 167

File Encryption Using AES 169

File Decryption Using AES 169

Summary 169

Chapter 6 Using Cryptography with Images 171

Simple Image Cryptography 171

Images and Cryptography Libraries 174

Understanding the Cryptography Library 174

Understanding the Cryptosteganography Library 175

Image Cryptography 175

File Cryptography Using Fernet 176

Image Cryptography Using Fernet 179

AES and Block Modes of Operations 180

Exploring a Simple ECB Mode Example 181

Exploring a Simple CBC Mode Example 185

Applying the Examples 186

Steganography 187

Storing a Message Inside an Image 188

Storing a Binary File Inside an Image 192

Working with large images 195

Summary 197

Chapter 7 Message Integrity 199

Message Authentication Codes 200

Hash-based Message Authentication Code 201

Using HMAC to Sign Message 202

Message Digest with SHA 203

Binary Digests 204

NIST Compliance 205

CBC-MAC 206

Birthday Attacks 207

Crafting Forgeries 209

The Length Extension Attack 209

Setting Up a Secure Channel 210

Communication Channels 211

Sending Secure Messages over IP Networks 212

Create a Server Socket 212

Create a Client Socket 213

Create a Threaded Server with TCP 214

Adding Symmetric Encryption 215

Concatenate Message and MAC 218

Summary 221

References 222

Chapter 8 Cryptographic Applications and PKI 223

The Public-Key Transformation 224

Exploring the Basics of RSA 226

Generating RSA Certificates 229

Constructing Simple Text Encryption and Decryption with RSA Certificates 231

Constructing BLOB Encryption and

Decryption with RSA Certificates 232

The El-Gamal Cryptosystem 235

Elliptic Curve Cryptography 238

Generating ECC Keys 240

Key Lengths and Curves 241

Diffie-Hellman Key Exchange 242

Summary 245

Chapter 9 Mastering Cryptography Using Python 247

Constructing a Plaintext Communications Application 248

Creating a Server 248

Creating the Client 250

Creating the Helper File 251

Execution 252

Installing and Testing Wireshark 253

Implementing PKI in the Application Using RSA Certificates 255

Modifying the Server 256

Modifying the Client 257

Modifying the Helper File 258

Execution 259

Implementing Diffie-Hellman Key Exchange 261

Modifying the Server File 262

Modifying the Client File 264

Modifying the Helper File 266

Creating the Diffie-Hellman Class File 270

Execution 275

Wrapping Up 276

Index 277

Authors

Shannon W. Bray