100% found this document useful (1 vote)
23 views47 pages

Cracking Codes With Python Al Sweigart Download

Cracking Codes with Python by Al Sweigart is an educational resource that teaches programming through the creation and decryption of ciphers. The book covers various classical ciphers and includes practical exercises, making it suitable for beginners interested in programming and cryptography. It features detailed explanations and code examples to facilitate learning Python while engaging with real-world applications of encryption.

Uploaded by

zxfinds435
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
23 views47 pages

Cracking Codes With Python Al Sweigart Download

Cracking Codes with Python by Al Sweigart is an educational resource that teaches programming through the creation and decryption of ciphers. The book covers various classical ciphers and includes practical exercises, making it suitable for beginners interested in programming and cryptography. It features detailed explanations and code examples to facilitate learning Python while engaging with real-world applications of encryption.

Uploaded by

zxfinds435
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Cracking Codes with Python Al Sweigart - PDF

Download (2025)

https://ebookultra.com/download/cracking-codes-with-python-al-
sweigart/

Visit ebookultra.com today to download the complete set of


ebooks or textbooks
We have selected some products that you may be interested in
Click the link to download now or visit ebookultra.com
for more options!.

Invent Your Own Computer Games with Python Early Access Al


Sweigart

https://ebookultra.com/download/invent-your-own-computer-games-with-
python-early-access-al-sweigart/

Automate the Boring Stuff with Python Practical


Programming for Total Beginners 1st Edition Al Sweigart

https://ebookultra.com/download/automate-the-boring-stuff-with-python-
practical-programming-for-total-beginners-1st-edition-al-sweigart/

Coding with Minecraft 1st Edition Al Sweigart

https://ebookultra.com/download/coding-with-minecraft-1st-edition-al-
sweigart/

Pavement Cracking Mechanisms Modeling Detection Testing


and Case Histories 1st Edition Imad L. Al-Qadi

https://ebookultra.com/download/pavement-cracking-mechanisms-modeling-
detection-testing-and-case-histories-1st-edition-imad-l-al-qadi/
Data Driven SEO with Python Solve SEO Challenges with Data
Science Using Python 4th Edition Andreas Voniatis

https://ebookultra.com/download/data-driven-seo-with-python-solve-seo-
challenges-with-data-science-using-python-4th-edition-andreas-
voniatis/

Cracking the GRE with 4 Practice Tests 2014 Edition


Princeton Review

https://ebookultra.com/download/cracking-the-gre-with-4-practice-
tests-2014-edition-princeton-review/

Parallel Programming with Python First Edition Jan Palach

https://ebookultra.com/download/parallel-programming-with-python-
first-edition-jan-palach/

Maya Programming with Python Cookbook 1st Edition Herbez

https://ebookultra.com/download/maya-programming-with-python-
cookbook-1st-edition-herbez/

Starting out with Python 5th Edition Tony Gaddis

https://ebookultra.com/download/starting-out-with-python-5th-edition-
tony-gaddis/
Cracking Codes with Python Al Sweigart Digital Instant
Download
Author(s): Al Sweigart
ISBN(s): 9781593278229, 1593278225
Edition: Illustrated
File Details: PDF, 8.17 MB
Year: 2018
Language: english
LEARN PYTHON

CR ACKING CODES
COVERS
BY HACKING
PYTHON 3
SECRET CIPHERS

WITH PY THON

CR ACKING CODES W ITH PY THON


A N I N T R O D U C T I O N T O
Learn how to program in Python while making and • Create test programs to make sure that your code
breaking ciphers—algorithms used to create and send encrypts and decrypts correctly B U I L D I N G A N D B R E A K I N G C I P H E R S
secret messages!
• Code (and hack!) a working example of the affine
After a crash course in Python programming basics, cipher, which uses modular arithmetic to encrypt a
you’ll learn to make, test, and hack programs that message
AL SWEIGART
encrypt text with classical ciphers like the transposition
• Break ciphers with techniques such as brute-force and
cipher and Vigenère cipher. You’ll begin with simple
frequency analysis
programs for the reverse and Caesar ciphers and then
work your way up to public key cryptography, the type There’s no better way to learn to code than to play with
of encryption used to secure today’s online transactions, real programs. Cracking Codes with Python makes the
including digital signatures, email, and Bitcoin. learning fun!

Each program includes the full code and a line-by-line ABOUT THE AUTHOR
explanation of how things work. By the end of the book,
Al Sweigart is a professional software developer
you’ll have learned how to code in Python and you’ll
who teaches programming to kids and adults. He is
have the clever programs to prove it!
the author of Automate the Boring Stuff with Python,
You’ll also learn how to: Invent Your Own Computer Games with Python,
and Scratch Programming Playground, also from
• Combine loops, variables, and flow control
No Starch Press. His programming tutorials can
statements into real working programs
be found at inventwithpython.com.
• Use dictionary files to instantly detect whether
decrypted messages are valid English or gibberish

SWEIGART
T H E F I N E ST I N G E E K E N T E RTA I N M E N T ™
w w w.nostarch.com
$29.95 ($39.95 CDN)
LANGUAGES/PYTHON
SHELVE IN: PROGRAMMING

FSC FPO “ I L I E F L AT .”
This book uses a durable binding that won’t snap shut.
PRAISE FOR
CRACKING CODES WITH PYTHON

“Definitely worth the read even as an experienced Python developer.


I learned more about cryptography and even a few new Python tricks.”
—Ray Doyle, The Ethical
Hacker Network

“I recommend this book for anyone who wants to learn Python/


programming and is interested in security or puzzles.”
—Jeanne Boyarsky, Code Ranch

“A fantastic programming and cryptography course for any high


school-aged child.”
—Kirsten West, The Old Schoolhouse
Magazine

“This book is an excellent resource for the complete beginner who


wants to learn something about programming and wants to learn
something about ciphers.”
—David Lowry-Duda, Senior
Research Scientist at ICERM

“If learning Python while studying basic cryptography is of interest, you


will find this book useful and well worth your time.”
—Matthew Helmke, Senior
Technical Content Developer
at Grafana
Cracking
Codes with
Python
An Introduction to Building
and Breaking Ciphers

b y Al S w e i g a r t

San Francisco
CRACKING CODES WITH PYTHON. Copyright © 2018 by Al Sweigart.

Some rights reserved. This work is licensed under the Creative Commons Attribution-NonCommercial-
ShareAlike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/
by-nc-sa/3.0/us/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
Seventh printing

27 26 25 24 23 7 8 9 10 11

ISBN-10: 1-59327-822-5
ISBN-13: 978-1-59327-822-9

Publisher: William Pollock


Production Editor: Riley Hoffman
Cover Illustration: Josh Ellingson
Interior Design: Octopod Studios
Developmental Editors: Jan Cash and Annie Choi
Technical Reviewers: Ari Lacenski and Jean-Philippe Aumasson
Copyeditor: Anne Marie Walker
Compositors: Riley Hoffman and Meg Sneeringer
Proofreader: Paula L. Fleming

For information on distribution, bulk sales, corporate sales, or translations, please contact No Starch Press,
Inc. directly at info@nostarch.com or:

No Starch Press, Inc.


245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900
www.nostarch.com

Library of Congress Cataloging-in-Publication Data

Names: Sweigart, Al, author.


Title: Cracking codes with Python : an introduction to building and breaking
ciphers / Al Sweigart.
Description: San Francisco : No Starch Press,Inc., [2018]
Identifiers: LCCN 2017035704 (print) | LCCN 2017047589 (ebook) | ISBN
9781593278694 (epub) | ISBN 1593278691 (epub) | ISBN 9781593278229 (pbk.)
| ISBN 1593278225 (pbk.)
Subjects: LCSH: Data encryption (Computer science) | Python (Computer program
language) | Computer security. | Hacking.
Classification: LCC QA76.9.A25 (ebook) | LCC QA76.9.A25 S9317 2018 (print) |
DDC 005.8/7--dc23
LC record available at https://lccn.loc.gov/2017035704

No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other
product and company names mentioned herein may be the trademarks of their respective owners. Rather
than use a trademark symbol with every occurrence of a trademarked name, we are using the names only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution
has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or
indirectly by the information contained in it.
Dedicated to Aaron Swartz, 1986–2013

“Aaron was part of an army of citizens that believes democracy only


works when the citizenry are informed, when we know about our
rights—and our obligations. An army that believes we must make
justice and knowledge available to all—not just the well born or
those that have grabbed the reins of power—so that we may govern
ourselves more wisely. When I see our army, I see Aaron Swartz and
my heart is broken. We have truly lost one of our better angels.”
—Carl Malamud
About the Author
Al Sweigart is a software developer and tech book author living in San
Francisco. Python is his favorite programming language, and he is the
developer of several open source modules for it. His other books are
freely available under a Creative Commons license on his website https://
inventwithpython.com/. His cat weighs 12 pounds.

About the Technical Reviewers


Ari Lacenski creates mobile apps and Python software. She lives in Seattle.

Jean-Philippe Aumasson (Chapters 22–24) is Principal Research Engineer at


Kudelski Security, Switzerland. He speaks regularly at information security
conferences such as Black Hat, DEF CON, Troopers, and Infiltrate. He is
the author of Serious Cryptography (No Starch Press, 2017).
Brief Contents

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

Chapter 1: Making Paper Cryptography Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Chapter 2: Programming in the Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Chapter 3: Strings and Writing Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Chapter 4: The Reverse Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Chapter 5: The Caesar Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

Chapter 6: Hacking the Caesar Cipher with Brute-Force . . . . . . . . . . . . . . . . . . . . . . . . . . 69

Chapter 7: Encrypting with the Transposition Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

Chapter 8: Decrypting with the Transposition Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

Chapter 9: Programming a Program to Test Your Program . . . . . . . . . . . . . . . . . . . . . . . . 113

Chapter 10: Encrypting and Decrypting Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

Chapter 11: Detecting English Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

Chapter 12: Hacking the Transposition Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

Chapter 13: A Modular Arithmetic Module for the Affine Cipher . . . . . . . . . . . . . . . . . . . 171

Chapter 14: Programming the Affine Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

Chapter 15: Hacking the Affine Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

Chapter 16: Programming the Simple Substitution Cipher . . . . . . . . . . . . . . . . . . . . . . . . 207

Chapter 17: Hacking the Simple Substitution Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

Chapter 18: Programming the Vigenère Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

Chapter 19: Frequency Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

Chapter 20: Hacking the Vigenère Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279


Chapter 21: The One-Time Pad Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315

Chapter 22: Finding and Generating Prime Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 321

Chapter 23: Generating Keys for the Public Key Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . 335

Chapter 24: Programming the Public Key Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349

Appendix: Debugging Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

viii   Brief Contents
Conte nt s in De ta il

Acknowledgments xix

Introduction xxi
Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
How to Use This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Typing Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Checking for Typos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Coding Conventions in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Downloading and Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Windows Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
macOS Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Ubuntu Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Downloading pyperclip.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Starting IDLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii

1
Making Paper Cryptography Tools 1
What Is Cryptography? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Codes vs. Ciphers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Caesar Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
The Cipher Wheel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Encrypting with the Cipher Wheel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Decrypting with the Cipher Wheel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Encrypting and Decrypting with Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Why Double Encryption Doesn’t Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2
Programming in the Interactive Shell 11
Some Simple Math Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Integers and Floating-Point Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Evaluating Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Storing Values with Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Overwriting Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3
Strings and Writing Programs 21
Working with Text Using String Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
String Concatenation with the + Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
String Replication with the * Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Getting Characters from Strings Using Indexes . . . . . . . . . . . . . . . . . . . . . . . . 24
Printing Values with the print() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Printing Escape Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Quotes and Double Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Writing Programs in IDLE’s File Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Source Code for the “Hello, World!” Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Checking Your Source Code with the Online Diff Tool . . . . . . . . . . . . . . . . . . . . . . . . . 31
Using IDLE to Access Your Program Later . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Saving Your Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Running Your Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Opening the Programs You’ve Saved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
How the “Hello, World!” Program Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Printing Directions to the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Taking a User’s Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Ending the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

4
The Reverse Cipher 39
Source Code for the Reverse Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Sample Run of the Reverse Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Setting Up Comments and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Finding the Length of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Introducing the while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The Boolean Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
The while Loop Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
“Growing” a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Improving the Program with an input() Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

5
The Caesar Cipher 53
Source Code for the Caesar Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Sample Run of the Caesar Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Importing Modules and Setting Up Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Constants and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
The for Loop Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
An Example for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
A while Loop Equivalent of a for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

x   Contents in Detail
The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
An Example if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
The else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
The elif Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
The in and not in Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
The find() String Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Encrypting and Decrypting Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Handling Wraparound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Handling Symbols Outside of the Symbol Set . . . . . . . . . . . . . . . . . . . . . . . . 65
Displaying and Copying the Translated String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Encrypting Other Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

6
Hacking the Caesar Cipher with Brute-Force 69
Source Code for the Caesar Cipher Hacker Program . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Sample Run of the Caesar Cipher Hacker Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Setting Up Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Looping with the range() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Decrypting the Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Using String Formatting to Display the Key and Decrypted Messages . . . . . . . . . . . . . . 75
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Practice Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

7
Encrypting with the Transposition Cipher 77
How the Transposition Cipher Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Encrypting a Message by Hand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Creating the Encryption Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Source Code for the Transposition Cipher Encryption Program . . . . . . . . . . . . . . . . . . . 81
Sample Run of the Transposition Cipher Encryption Program . . . . . . . . . . . . . . . . . . . . 82
Creating Your Own Functions with def Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Defining a Function that Takes Arguments with Parameters . . . . . . . . . . . . . . . 83
Changes to Parameters Exist Only Inside the Function . . . . . . . . . . . . . . . . . . . 84
Defining the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Passing the Key and Message As Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
The List Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Reassigning the Items in Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Lists of Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Using len() and the in Operator with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
List Concatenation and Replication with the + and * Operators . . . . . . . . . . . . 89
The Transposition Encryption Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Augmented Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Moving currentIndex Through the Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
The join() String Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Return Values and return Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
A return Statement Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Returning the Encrypted Ciphertext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
The __name__ Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

Contents in Detail   xi
8
Decrypting with the Transposition Cipher 99
How to Decrypt with the Transposition Cipher on Paper . . . . . . . . . . . . . . . . . . . . . . . 100
Source Code for the Transposition Cipher Decryption Program . . . . . . . . . . . . . . . . . . 101
Sample Run of the Transposition Cipher Decryption Program . . . . . . . . . . . . . . . . . . . 102
Importing Modules and Setting Up the main() Function . . . . . . . . . . . . . . . . . . . . . . . . 102
Decrypting the Message with the Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
The round(), math.ceil(), and math.floor() Functions . . . . . . . . . . . . . . . . . . . . 103
The decryptMessage() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Adjusting the column and row Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

9
Programming a Program to Test Your Program 113
Source Code for the Transposition Cipher Tester Program . . . . . . . . . . . . . . . . . . . . . 114
Sample Run of the Transposition Cipher Tester Program . . . . . . . . . . . . . . . . . . . . . . . 115
Importing the Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Creating Pseudorandom Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Creating a Random String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Duplicating a String a Random Number of Times . . . . . . . . . . . . . . . . . . . . . 118
List Variables Use References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Passing References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Using copy.deepcopy() to Duplicate a List . . . . . . . . . . . . . . . . . . . . . . . . . . 122
The random.shuffle() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Randomly Scrambling a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Testing Each Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Checking Whether the Cipher Worked and Ending the Program . . . . . . . . . . . . . . . . 124
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Testing the Test Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

10
Encrypting and Decrypting Files 127
Plain Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Source Code for the Transposition File Cipher Program . . . . . . . . . . . . . . . . . . . . . . . 128
Sample Run of the Transposition File Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . 130
Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Opening Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Writing to and Closing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Reading from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Setting Up the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Checking Whether a File Exists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
The os.path.exists() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Checking Whether the Input File Exists with the os.path.exists() Function . . . . . 134
Using String Methods to Make User Input More Flexible . . . . . . . . . . . . . . . . . . . . . . 134
The upper(), lower(), and title() String Methods . . . . . . . . . . . . . . . . . . . . . . . 134
The startswith() and endswith() String Methods . . . . . . . . . . . . . . . . . . . . . . . 135
Using These String Methods in the Program . . . . . . . . . . . . . . . . . . . . . . . . . 135

xii   Contents in Detail
Reading the Input File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Measuring the Time It Took to Encrypt or Decrypt . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
The time Module and time.time() Function . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Using the time.time() Function in the Program . . . . . . . . . . . . . . . . . . . . . . . . 137
Writing the Output File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

11
Detecting English Programmatically 141
How Can a Computer Understand English? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Source Code for the Detect English Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Sample Run of the Detect English Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Instructions and Setting Up Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
The Dictionary Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
The Difference Between Dictionaries and Lists . . . . . . . . . . . . . . . . . . . . . . . 147
Adding or Changing Items in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . 147
Using the len() Function with Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Using the in Operator with Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Finding Items Is Faster with Dictionaries than with Lists . . . . . . . . . . . . . . . . . 149
Using for Loops with Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Implementing the Dictionary File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
The split() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Splitting the Dictionary File into Individual Words . . . . . . . . . . . . . . . . . . . . 151
Returning the Dictionary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Counting the Number of English Words in message . . . . . . . . . . . . . . . . . . . . . . . . . 152
Divide-by-Zero Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Counting the English Word Matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
The float(), int(), and str() Functions and Integer Division . . . . . . . . . . . . . . . . 154
Finding the Ratio of English Words in the Message . . . . . . . . . . . . . . . . . . . 154
Removing Non-Letter Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
The append() List Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Creating a String of Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Detecting English Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Using Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Calculating Percentages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

12
Hacking the Transposition Cipher 161
Source Code of the Transposition Cipher Hacker Program . . . . . . . . . . . . . . . . . . . . . 162
Sample Run of the Transposition Cipher Hacker Program . . . . . . . . . . . . . . . . . . . . . . 163
Importing the Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Multiline Strings with Triple Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Displaying the Results of Hacking the Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Getting the Hacked Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
The strip() String Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Applying the strip() String Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Failing to Hack the Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

Contents in Detail   xiii
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

13
A Modular Arithmetic Module for the Affine Cipher 171
Modular Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
The Modulo Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Finding Factors to Calculate the Greatest Common Divisor . . . . . . . . . . . . . . . . . . . . . 173
Multiple Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Euclid’s Algorithm for Finding the GCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Understanding How the Multiplicative and Affine Ciphers Work . . . . . . . . . . . . . . . . . 177
Choosing Valid Multiplicative Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Encrypting with the Affine Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Decrypting with the Affine Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Finding Modular Inverses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
The Integer Division Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Source Code for the Cryptomath Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

14
Programming the Affine Cipher 185
Source Code for the Affine Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Sample Run of the Affine Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Setting Up Modules, Constants, and the main() Function . . . . . . . . . . . . . . . . . . . . . . 188
Calculating and Validating the Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
The Tuple Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Checking for Weak Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
How Many Keys Can the Affine Cipher Have? . . . . . . . . . . . . . . . . . . . . . . 191
Writing the Encryption Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Writing the Decryption Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Generating Random Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

15
Hacking the Affine Cipher 197
Source Code for the Affine Cipher Hacker Program . . . . . . . . . . . . . . . . . . . . . . . . . 198
Sample Run of the Affine Cipher Hacker Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Setting Up Modules, Constants, and the main() Function . . . . . . . . . . . . . . . . . . . . . . 200
The Affine Cipher Hacking Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
The Exponent Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Calculating the Total Number of Possible Keys . . . . . . . . . . . . . . . . . . . . . . . 201
The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Using continue to Skip Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

xiv   Contents in Detail
16
Programming the Simple Substitution Cipher 207
How the Simple Substitution Cipher Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Source Code for the Simple Substitution Cipher Program . . . . . . . . . . . . . . . . . . . . . . 209
Sample Run of the Simple Substitution Cipher Program . . . . . . . . . . . . . . . . . . . . . . . 210
Setting Up Modules, Constants, and the main() Function . . . . . . . . . . . . . . . . . . . . . . 211
The sort() List Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Wrapper Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
The translateMessage() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
The isupper() and islower() String Methods . . . . . . . . . . . . . . . . . . . . . . . . . 216
Preserving Cases with isupper() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Generating a Random Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

17
Hacking the Simple Substitution Cipher 221
Using Word Patterns to Decrypt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Finding Word Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Finding Potential Decryption Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Overview of the Hacking Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
The Word Pattern Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Source Code for the Simple Substitution Hacking Program . . . . . . . . . . . . . . . . . . . . . 226
Sample Run of the Simple Substitution Hacking Program . . . . . . . . . . . . . . . . . . . . . . 229
Setting Up Modules and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Finding Characters with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Setting Up the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Displaying Hacking Results to the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Creating a Cipherletter Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Creating a Blank Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Adding Letters to a Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Intersecting Two Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
How the Letter-Mapping Helper Functions Work . . . . . . . . . . . . . . . . . . . . . . 235
Identifying Solved Letters in Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Testing the removeSolvedLetterFromMapping() Function . . . . . . . . . . . . . . . . 240
The hackSimpleSub() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
The replace() String Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Decrypting the Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Decrypting in the Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

18
Programming the Vigenère Cipher 247
Using Multiple Letter Keys in the Vigenère Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Longer Vigenère Keys Are More Secure . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Choosing a Key That Prevents Dictionary Attacks . . . . . . . . . . . . . . . . . . . . . 250
Source Code for the Vigenère Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Sample Run of the Vigenère Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

Contents in Detail   xv
Setting Up Modules, Constants, and the main() Function . . . . . . . . . . . . . . . . . . . . . . 252
Building Strings with the List-Append-Join Process . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Encrypting and Decrypting the Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258

19
Frequency Analysis 259
Analyzing the Frequency of Letters in Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Matching Letter Frequencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Calculating the Frequency Match Score for the Simple Substitution Cipher . . . 262
Calculating the Frequency Match Score for the Transposition Cipher . . . . . . . 263
Using Frequency Analysis on the Vigenère Cipher . . . . . . . . . . . . . . . . . . . . 264
Source Code for Matching Letter Frequencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Storing the Letters in ETAOIN Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Counting the Letters in a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Getting the First Member of a Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Ordering the Letters in the Message by Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Counting the Letters with getLetterCount() . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Creating a Dictionary of Frequency Counts and Letter Lists . . . . . . . . . . . . . . 269
Sorting the Letter Lists in Reverse ETAOIN Order . . . . . . . . . . . . . . . . . . . . . 270
Sorting the Dictionary Lists by Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Creating a List of the Sorted Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Calculating the Frequency Match Score of the Message . . . . . . . . . . . . . . . . . . . . . . . 276
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

20
Hacking the Vigenère Cipher 279
Using a Dictionary Attack to Brute-Force the Vigenère Cipher . . . . . . . . . . . . . . . . . . . 280
Source Code for the Vigenère Dictionary Hacker Program . . . . . . . . . . . . . . . . . . . . . 280
Sample Run of the Vigenère Dictionary Hacker Program . . . . . . . . . . . . . . . . . . . . . . 281
About the Vigenère Dictionary Hacker Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Using Kasiski Examination to Find the Key’s Length . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Finding Repeated Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Getting Factors of Spacings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Getting Every Nth Letters from a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Using Frequency Analysis to Break Each Subkey . . . . . . . . . . . . . . . . . . . . . 285
Brute-Forcing Through the Possible Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Source Code for the Vigenère Hacking Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Sample Run of the Vigenère Hacking Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Importing Modules and Setting Up the main() Function . . . . . . . . . . . . . . . . . . . . . . . . 294
Finding Repeated Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Calculating the Factors of the Spacings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Removing Duplicates with the set() Function . . . . . . . . . . . . . . . . . . . . . . . . . 298
Removing Duplicate Factors and Sorting the List . . . . . . . . . . . . . . . . . . . . . . 298
Finding the Most Common Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Finding the Most Likely Key Lengths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
The extend() List Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Extending the repeatedSeqSpacings Dictionary . . . . . . . . . . . . . . . . . . . . . . 301
Getting the Factors from factorsByCount . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

xvi   Contents in Detail
Getting Letters Encrypted with the Same Subkey . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Attempting Decryption with a Likely Key Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
The end Keyword Argument for print() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Running the Program in Silent Mode or Printing Information to the User . . . . . 306
Finding Possible Combinations of Subkeys . . . . . . . . . . . . . . . . . . . . . . . . . 306
Printing the Decrypted Text with the Correct Casing . . . . . . . . . . . . . . . . . . . 310
Returning the Hacked Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Breaking Out of the Loop When a Potential Key Is Found . . . . . . . . . . . . . . . 311
Brute-Forcing All Other Key Lengths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Modifying the Constants of the Hacking Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314

21
The One-Time Pad Cipher 315
The Unbreakable One-Time Pad Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Making Key Length Equal Message Length . . . . . . . . . . . . . . . . . . . . . . . . . 316
Making the Key Truly Random . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Avoiding the Two-Time Pad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Why the Two-Time Pad Is the Vigenère Cipher . . . . . . . . . . . . . . . . . . . . . . . 319
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320

22
Finding and Generating Prime Numbers 321
What Is a Prime Number? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Source Code for the Prime Numbers Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Sample Run of the Prime Numbers Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
How the Trial Division Algorithm Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Implementing the Trial Division Algorithm Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
The Sieve of Eratosthenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Generating Prime Numbers with the Sieve of Eratosthenes . . . . . . . . . . . . . . . . . . . . . 330
The Rabin-Miller Primality Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Finding Large Prime Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Generating Large Prime Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334

23
Generating Keys for the Public Key Cipher 335
Public Key Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
The Problem with Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Digital Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Beware the MITM Attack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Steps for Generating Public and Private Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Source Code for the Public Key Generation Program . . . . . . . . . . . . . . . . . . . . . . . . . 340
Sample Run of the Public Key Generation Program . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Creating the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343

Contents in Detail   xvii
Generating Keys with the generateKey() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Calculating an e Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Calculating a d Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Returning the Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Creating Key Files with the makeKeyFiles() Function . . . . . . . . . . . . . . . . . . . . . . . . . 345
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Hybrid Cryptosystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348

24
Programming the Public Key Cipher 349
How the Public Key Cipher Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Creating Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Converting a String into a Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
The Mathematics of Public Key Cipher Encryption and Decryption . . . . . . . . . 353
Converting a Block to a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Why We Can’t Hack the Public Key Cipher . . . . . . . . . . . . . . . . . . . . . . . . 355
Source Code for the Public Key Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Sample Run of the Public Key Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Setting Up the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
How the Program Determines Whether to Encrypt or Decrypt . . . . . . . . . . . . . . . . . . . 362
Converting Strings to Blocks with getBlocksFromText() . . . . . . . . . . . . . . . . . . . . . . . . 363
The min() and max() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Storing Blocks in blockInt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Using getTextFromBlocks() to Decrypt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Using the insert() List Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Merging the Message List into One String . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Writing the encryptMessage() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Writing the decryptMessage() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Reading in the Public and Private Keys from Their Key Files . . . . . . . . . . . . . . . . . . . . 369
Writing the Encryption to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Decrypting from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Calling the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

Appendix
Debugging Python Code 375
How the Debugger Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Debugging the Reverse Cipher Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Setting Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380

Index 381

xviii   Contents in Detail
Other documents randomly have
different content
Their torches were burning now, and a little ahead of us a pack of
the huge green-grey mimes were leaping.
Then Tahg came toward me. "Good-bye," he said. "You look more
good to me when I see you next time. The gods prepare you now."

He turned and was lost in the darkness. My ankles had been fettered
with a two-foot length of rope; my wrists were crossed and lashed
behind me. No one was with me now but my two captors who urged
me forward, impatient at my little jerky steps. The village and its
jabbering turmoil and lights was in a moment hidden by a rise of the
rocky ground. Then I saw before me a fairly large, square building of
stone, flat-roofed, with a cone-shaped stone-pile on top like a crude
church spire.
An Org temple. It was windowless; some twenty feet high from
ground to its roof. A narrow, rectangular slit of doorway was in front,
where two huge torches, like braziers one on either side, were
burning. An Org stood between them, with the torchlight painting
him—an aged savage in a long, white skin drape which was
fantastically ornamented. He was thin and bent, his round brown
skull almost hairless, his body shriveled, parched with age. His
skinny arms were upraised, outstretched to welcome me.
But my startled gaze turned from him, for on the ground just at the
edge of the swaying torchlight, I saw that two figures were lying.
Two men, roped and tied into inert bundles.
They were Jan and Torrence!

V
There was a time when, roped and tied like Jan and Torrence, I was
laid beside them while in the torchlight, alone with his pagan gods,
the ancient Org priest stood intoning his prayers and incantations. It
was then that Jan was able to tell me what had happened to him.
He was lying between Torrence and me. I had little chance to talk to
Torrence. Nor any great desire, for I considered him then merely a
craven fellow who had deserted us at the very first of the weird
attacks.
Human emotions work strangely. It was obvious now, as we lay
there in the darkness, with the aged savage in the torchlight near us
—obvious enough that we were doomed to something horrible which
at best would end in our death. Yet Jan and I—each having
considered the other dead—were for a brief time at least, pleased
that we were here. No one yet alive, can normally quite give up
hope of escaping death. I recall that in the darkness I was furtively
trying to loosen my bonds, twisting and squirming.
"You needn't bother," Torrence muttered. "I've tried all that. And
those two damned Orgs who carried you here—they're still watching
us."
"Going to take us inside, I guess," Jan whispered. "Inside this
temple to—to—"
His shuddering imagination supplied no words. But his idea was
right, for presently the old priest was finished with his incantations.
His cracked voice called a command and the two savages who had
brought me here came from nearby. One by one, they picked us up
and carried us inside.
I was the last to go in. The place was a single stone square room. It
was lurid with a swaying torchlight. Carved gargoyle images, crude
and hideously ugly—grotesque personification of the pagan Vulcan
gods—where ranged along the walls. The old priest was standing
now on a little dais, between the two interior torches. His arms were
upraised toward me as I was carried in; behind him there was a
quick stone altar, with a line of smaller images on it. His voice rose,
quavering, as I was slowly carried past him; and his hands over me
might have been purifying me for the coming rite.
In the center of the room, raised some five feet above the floor,
there was a broad stone slab, with a big, grinning, pot-bellied stone
image mounted up there. Then I saw that the slab had a broad,
cradle-like depression in front of the image. Still bound, lying there
side by side, with the belly of the huge image projecting partly over
them, were Jan and Torrence. And now the two savages hoisted me
up and rolled me among them.
The sacrificial altar. Heaven knows, I could not miss the realization
now. There was a weird, acrid, nauseous smell clinging here from
former ceremonies. And as I was hoisted up, I saw that the smooth
sides of the altar were seared, blackened by the heat of flames
which so many times before must have been here.
And the heat—the fire? Within a moment after I was rolled into the
saucer-like depression of the alter—with Torrence muttering
despairing curses and Jan pallid and grim beside me—outside the
temple there sounded a weird gibbering chorus of baying. Ghastly,
familiar sound! The mimes—the giant fire-males! Released at the
temple doorway, they came bounding in—blobs of leaping red-green
flame! A dozen or more of the weird creatures, all of these much
larger than the male Jan had killed near the Roberts' spaceship. Fire-
males trained for this ceremony. Enveloped in their lurid flames they
rushed at the altar, circling it, swiftly running one behind the other
so that we were encircled with a ring of leaping flames.
I heard Torrence mutter, "To roast us! Just to roast us slowly—"

The shoulders and heads of the running, circling fire-mimes were


nearly as high as the altar slab on which we were lying. The flames
of them swirled two or three feet higher—blobs of fire which merged
one with the other. A circular curtain of mounting flame walling us
in. Through it the temple interior was blurred, distorted. Vaguely the
figure of the aged priest was visible. He was now on his knees,
turned partly away from us as he faced his little row of god-images,
supplicating them.
Curtain of swirling fire. Within a moment the heat of it was searing
us. Heat slowly intensifying. It was bearable now; but the confined
circle of air here was mounting in temperature; the big gargoyle
image over us, the metallic-rock slab beneath us both were slowly
heating. The smoke and the swirling gas-fumes would choke us into
unconsciousness very quickly, I knew. And then the mounting heat
would at last make this a sizzling griddle, on which we would lie,
slowly roasting....
A chaos of confused phantasmagoria blurred my mind in those first
horrible moments.... I saw the old priest, so solemnly, humbly
supplicating his gods as he officiated at this gruesome pagan
ceremony ... then I could envisage us being carried off, back to the
Org village where the people, not worthy of being here in the sacred
temple, were so eagerly awaiting us ... then the orgy—sacred feast,
endowing its participants with what future virtues and panaceas they
conceived their gods would give them....
The end, for us.... Already Jan was pitifully coughing.... But what
was this? I felt a shape stir beside me; a small, slender figure with
dangling hair; I felt trembling fingers fumbling at my bonds.
Ama! She had crept from a little recess under the giant bulging
statue of the gargoyle god, here on the altar. Ama, who had found a
chance to slip away from the wooing Tahg, and had preceded us
here—hiding up here so that she might try and release us....
But it was too late now. So obviously too late! She had accomplished
nothing, save to immolate herself here with us!
Into my ear her terrified voice was whispering, "I thought that the
fire-males would not come so soon."
In the blurring, blasting heat and smoke, she had untied us, but of
what use? "No—no chance to try and jump," she stammered. "As we
fell they would leap upon us—kill us in a moment—"
The sizzling, crackling of the flames—the gibbering baying of the
fire-mimes mingling with the incantations of the old priest—it was all
a blurred chaos.... Then suddenly I was aware that Jan, coughing,
choking, had struggled half erect on the slab. There was just an
instant when I saw his contorted face, painted lurid by the flames.
Wild despairing desperation was stamped there. But there was
something else. An exaltation....
"You—run—" he gasped.
And then he jumped. A wild, desperate leap, upward and outward....
It carried him through the curtain of flame and out some ten feet to
the temple floor. The thud of his crashing body mingled with the
gibbering yelps of the fire-mimes as they whirled and pounced upon
him—all of them in a second, merged into a great blob of flame out
there on the temple floor where they fought, scrambling over him,
ripping—tearing—
Gruesome horror.... I knew in that second that already Jan was
dead.... And then I was aware that the other side of the altar, behind
the gargoyle image, was momentarily completely dark. All the
flaming creatures were fighting over Jan's body. Torrence, too, had
realized it. I saw him stagger up and jump into the darkness. I
shoved at Ama; rolled and tumbled her off the slab. We fell in a
heap and scrambled erect. The pawing, snarling group of fire-
mimes, twenty feet away with the big altar slab intervening, intent
upon their scattering fragments, for that moment did not heed us.
On his little dais by the wall, the old priest had turned and was
standing numbed, confused. There was no one else in the sacred
temple. The single doorway was a vertical slit of darkness. Already
Torrence was running for it. I clutched at Ama and we ran.
Out into the rocky blackness. I recall that I had the wits to turn us
away from where the Org village lay nearby, behind the hillock....
Then, suddenly, from behind a crag, a dark figure rose up. Tahg!
Tahg, who had been crouching here, evidently impatient for his feast
so that he would be the first to see us as we were brought from the
temple....
He stood gasping, startled; and in that same second I was upon
him, my fist crashing into his face so that he went backward and
down. With desperate haste I caught up a rock from the ground—
pounded it on his head—wildly pounding until his skull smashed....
Then I was up, clutching Ama. Torrence already was ten or twenty
feet ahead of us in the darkness. We ran after him; he heard us
coming and waited.
"Which way?" he gasped. "She ought to know. Our spaceship—that
would be best—"
At the door of the temple the old priest now was standing
screaming. From behind the little hill, answering shouts were
responding....
"Is it closer to your village, or to our ship?" I demanded of Ama.
"Why—why to your ship, I think."
"You know the way?"
"Yes—yes, I think so. Not to where you landed—that I do not know.
But to the Roberts' ship—"
And the Orgs doubtless would consider that we would head into the
Senza country. The forests in that direction would be full of roaming
Orgs hunting us....
She and I and Torrence ran, plunging wildly forward in the rocky
darkness, with the lights and the turmoil behind us presently fading
away into the heavy blank silence of the Vulcan night....
I think that there is little I need add. It was a long, arduous journey,
but we reached our little spaceship safely. And in a moment, with
the rocket-streams shoving downward and with the lower-hull
gravity plates in neutral, slowly we were rising into the cloudy
darkness.
"You will take me to my people?" Ama said anxiously. "You did
promise me—"
"Yes, of course, Ama—we'll land you near your village—"
Queerly enough, it was not until that moment after all the
tumultuous events which had engulfed us, that suddenly I
remembered the deposits of allurite which we had hoped to locate
upon Vulcan. If I could take back samples of the ore—to my
sponsors that doubtless would be considered the major success—the
only success indeed—of my expedition.... It occurred to me then
that we could land at the Senza village, and for a little time, prospect
from there....
But even that plan was doomed to frustration. I mentioned it to
Torrence. "We should head for Earth," he said dogmatically. "I have
had enough of this."
It was then, before we had gone far toward the Senza country, that
I noticed the rocket streams were acting queerly. A seeming lack of
power.... Torrence had gone down into the hull; he came back
presently to the turret.
"The Pelletier rotators are slowing," I said. "What's the matter?"
He shook his head. "I noticed it," he said. "Haven't found out yet.
You want to come and look?"
I locked the controls, left Ama and went down into the hull with
Torrence. In the dim mechanism cubby, as I bent over the Pelletier
mechanisms, suddenly Torrence leaped on me! It came as quickly,
unexpectedly as that. The culmination of his brooding, murderous,
cowardly plans. His heavy face was contorted, his eyes blazing. In
his hand he held a sliver of metal arrow. It was bent, doubled over,
so that all this time he had been able to keep it hidden in his
clothes. The arrow he had taken from Roberts' body, as it lay there
near the bow of the wrecked spaceship! The little light in the
mechanism cubby gleamed on it now; glistened on the green and
red spots of the sleek, sand-colored metal. Allurite! The precious
substance—not an alloy, not a low-grade allurium ore, but allurite in
its pure state! On Earth this single bent little arrow could be worth a
fortune!
And the frenzied Torrence was gloating: "See it, you damn fool—
your allurite—right under your nose all the time! And now it's mine
—" In that second he would have plunged the needle-sharp arrow-
point like a stilletto into my heart. But his own frenzied, murderous
hysteria defeated him. My fist struck his wrist, knocked his stab-
thrust away, with the arrow clattering to the floor. And then I had
him by the throat, strangling him until he yielded and I tied him
up....
As you who read this, of course, already know from the news
reports, I dropped Ama near the edge of the Senza village. I recall
now how she stood in the Vulcan night, in the torchlight with the
excited crowd of her people behind her; the last I saw of Vulcan was
the little figure of her waving at me as I rose into the leaden sky and
headed back for Earth.... Maybe—just maybe—I'll return someday to
that land where Jan gave his life that his friends might live.
*** END OF THE PROJECT GUTENBERG EBOOK THE FLAME
BREATHERS ***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying
copyright royalties. Special rules, set forth in the General Terms of
Use part of this license, apply to copying and distributing Project
Gutenberg™ electronic works to protect the PROJECT GUTENBERG™
concept and trademark. Project Gutenberg is a registered trademark,
and may not be used if you charge for an eBook, except by following
the terms of the trademark license, including paying royalties for use
of the Project Gutenberg trademark. If you do not charge anything
for copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund
from the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law
in the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name associated
with the work. You can easily comply with the terms of this
agreement by keeping this work in the same format with its attached
full Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears,
or with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning
of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the Project
Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except for


the “Right of Replacement or Refund” described in paragraph 1.F.3,
the Project Gutenberg Literary Archive Foundation, the owner of the
Project Gutenberg™ trademark, and any other party distributing a
Project Gutenberg™ electronic work under this agreement, disclaim
all liability to you for damages, costs and expenses, including legal
fees. YOU AGREE THAT YOU HAVE NO REMEDIES FOR
NEGLIGENCE, STRICT LIABILITY, BREACH OF WARRANTY OR
BREACH OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH
1.F.3. YOU AGREE THAT THE FOUNDATION, THE TRADEMARK
OWNER, AND ANY DISTRIBUTOR UNDER THIS AGREEMENT WILL
NOT BE LIABLE TO YOU FOR ACTUAL, DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE OR INCIDENTAL DAMAGES EVEN IF
YOU GIVE NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving
it, you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or
entity that provided you with the defective work may elect to provide
a replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation,


the trademark owner, any agent or employee of the Foundation,
anyone providing copies of Project Gutenberg™ electronic works in
accordance with this agreement, and any volunteers associated with
the production, promotion and distribution of Project Gutenberg™
electronic works, harmless from all liability, costs and expenses,
including legal fees, that arise directly or indirectly from any of the
following which you do or cause to occur: (a) distribution of this or
any Project Gutenberg™ work, (b) alteration, modification, or
additions or deletions to any Project Gutenberg™ work, and (c) any
Defect you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many
small donations ($1 to $5,000) are particularly important to
maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

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.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookultra.com

You might also like