Implementing Cryptography Using Python Shannon Bray - Quickly download the ebook in PDF format for unlimited reading
Implementing Cryptography Using Python Shannon Bray - Quickly download the ebook in PDF format for unlimited reading
https://ebookmass.com/product/bayesian-optimization-theory-and-
practice-using-python-1st-edition-peng-liu/
https://ebookmass.com/product/python-programming-using-problem-
solving-approach-1st-edition-reema-thareja/
https://ebookmass.com/product/machine-learning-on-geographical-data-
using-python-1st-edition-joos-korstanje/
https://ebookmass.com/product/data-driven-seo-with-python-solve-seo-
challenges-with-data-science-using-python-1st-edition-andreas-
voniatis/
Introduction To Computing And Problem Solving Using Python
1st Edition E. Balaguruswamy
https://ebookmass.com/product/introduction-to-computing-and-problem-
solving-using-python-1st-edition-e-balaguruswamy/
https://ebookmass.com/product/introduction-to-responsible-ai-
implement-ethical-ai-using-python-1st-edition-manure/
https://ebookmass.com/product/bayesian-optimization-theory-and-
practice-using-python-peng-liu/
https://ebookmass.com/product/etextbook-978-0134379760-the-practice-
of-computing-using-python-3rd-edition/
https://ebookmass.com/product/introduction-to-computation-and-
programming-using-python-third-edition-john-v-guttag/
Implementing
Cryptography Using
Python®
Shannon W. Bray
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
ISBN: 978-1-119-61220-9
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or
by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted
under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission
of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clear-
ance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the
Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111
River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/
go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or war-
ranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all
warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be
created or extended by sales or promotional materials. The advice and strategies contained herein may not
be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in
rendering legal, accounting, or other professional services. If professional assistance is required, the services
of a competent professional person should be sought. Neither the publisher nor the author shall be liable for
damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation
and/or a potential source of further information does not mean that the author or the publisher endorses
the information the organization or website may provide or recommendations it may make. Further, readers
should be aware that Internet websites listed in this work may have changed or disappeared between when
this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department
within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included
with standard print versions of this book may not be included in e-books or in print-on-demand. If this book
refers to media such as a CD or DVD that is not included in the version you purchased, you may down-
load this material at http://booksupport.wiley.com. For more information about Wiley products, visit
www.wiley.com.
Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc.
and/or its affiliates, in the United States and other countries, and may not be used without written permis-
sion. Python is a registered trademark of Python Software Foundation. All other trademarks are the property
of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned
in this book.
To Stephanie, Eden, Hayden, and Kenna,
with all my love, for making each and every day special.
About the Author
v
vi About the Author
book, Shannon is currently running for the U.S. Senate for the state of North
Carolina to help bring cybersecurity issues to the mainstream and to help people
understand the importance of end-to-end encryption.
Shannon can be contacted through LinkedIn at www.linkedin.com/in/
shannonbray.
Acknowledgments
While this is my third book, every book comes with its own set of challenges.
Completing a book is not always a fun project but does become a labor of love.
As soon as you start, life has a way of throwing changes at you, and it seems
that you never have the time that you thought you would have. Shortly after
starting this book, I started a PhD program with the Missouri University of
Science and Technology and transitioned through a number of government
contracts, ran for political office, and then became a work-from-home teacher
as the world battled a global pandemic. I knew that the editing team would be
earning their pay with whatever I put together. That team has been wonderful
with their feedback and responsiveness. Specifically, I’d like to thank the follow-
ing people on the Wiley team: Barath Kumar Rajasekaran, production editor;
Jim Minatel, acquisitions editor; Pete Gaughan, content enablement manager;
Brent Cook and James Langbridge, technical editors, and, most importantly,
Kim Wimpsett, the project editor. Kim did her best to keep me on schedule, but
I found new ways to miss a few deadlines.
vii
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Contents at a Glance
Introductionxvii
Chapter 1 Introduction to Cryptography and Python 1
Chapter 2 Cryptographic Protocols and Perfect Secrecy 31
Chapter 3 Classical Cryptography 65
Chapter 4 Cryptographic Math and Frequency Analysis 95
Chapter 5 Stream Ciphers and Block Ciphers 139
Chapter 6 Using Cryptography with Images 171
Chapter 7 Message Integrity 199
Chapter 8 Cryptographic Applications and PKI 223
Chapter 9 Mastering Cryptography Using Python 247
Index277
ix
Contents
Introductionxvii
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
xi
xii Contents
Using Conditionals 16
Using Loops 17
for17
while18
continue18
break18
else18
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
Summary30
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-Hellman34
Data Origin Authentication 34
Entity Authentication 35
Symmetric Algorithms 36
Asymmetric Algorithms 36
The Needham-Schroeder Protocols 36
The Otway-Rees Protocol 38
Kerberos39
Multiple-Domain Kerberos 40
X.50941
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
Contents xiii
Pseudorandomness115
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
Summary138
Chapter 5 Stream Ciphers and Block Ciphers 139
Convert between Hexdigest and Plaintext 140
Use Stream Ciphers 141
ARC4147
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
Summary169
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
Steganography187
Storing a Message Inside an Image 188
Contents xv
xvii
xviii Introduction
privacy advocates. The world is at war on what can be encrypted, what should be
encrypted, and who should have the keys to unlock someone else’s encryption.
Whether you agree or disagree with what powers a nation-state should have
over encrypted communications in their country, you should understand what
encryption is, how to apply it when needed, and how to ensure that the data
you are receiving is authentic and confidential. Over the course of this book,
you will get a basic understanding of how to cryptographically secure your
messages, files, or Internet traffic using easy-to-understand Python recipes that
have been created or updated to support Python 3.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookmass.com