Cracking Codes with Python: An Introduction to Building and Breaking Ciphers
introduCtion I couldn’t help but overhear probably because I was eavesdropping Anonymous If you could travel back to the early 1990s with this book the contents of Chapter 23 that implement part of the RSA cipher would be illegal to export out of the United States Because messages encrypted with RSA are impossible to hack the export of encryption software like RSA was deemed a matter of national security and required State Department approval In fact strong cryptography was regulated at the same level as tanks missiles and flamethrowers In 1990 Daniel J Bernstein a student at the University of California Berkeley wanted to publish an academic paper that featured source code of his Snuffle encryption system The US government informed him that he would need to become a licensed arms dealer before he could post his source code on the internet The government also told him that it would deny him an export license if he applied for one because his technology was too secure
The Electronic Frontier Foundation a young digital civil liberties orga nization represented Bernstein in Bernstein v United States For the first time ever the courts ruled that written software code was speech protected by the First Amendment and that the export control laws on encryption vio lated Bernstein’s First Amendment rights Now strong cryptography is at the foundation of a large part of the global economy safeguarding businesses and e-commerce sites used by millions of internet shoppers every day The intelligence community’s pre dictions that encryption software would become a grave national security threat were unfounded But as recently as the 1990s spreading this knowledge freely as this book does would have landed you in prison for arms trafficking For a more detailed history of the legal battle for freedom of cryptography read Steven Levy’s book Crypto How the Code Rebels Beat the Government Saving Privacy in the Digital Age Penguin 2001 who should read this Book Many books teach beginners how to write secret messages using ciphers A couple of books teach beginners how to hack ciphers But no books teach beginners how to program computers to hack ciphers This book fills that gap This book is for those who are curious about encryption hacking or cryptography The ciphers in this book except for the public key cipher in Chapters 23 and 24 are all centuries old but any laptop has the compu tational power to hack them No modern organizations or individuals use these ciphers anymore but by learning them you’ll learn the foundations cryptography was built on and how hackers can break weak encryption no t e The ciphers you’ll learn in this book are fun to play with but they don’t provide true security Don’t use any of the encryption programs in this book to secure your actual files As a general rule you shouldn’t trust the ciphers that you create Real-world ciphers are subject to years of analysis by professional cryptographers before being put into use This book is also for people who have never programmed before It teaches basic programming concepts using the Python programming lan guage which is one of the best languages for beginners It has a gentle learning curve that novices of all ages can master yet it’s also a power ful language used by professional software developers Python runs on Windows macOS Linux and even the Raspberry Pi and it’s free to down load and use See Downloading and Installing Python on page xxv for instructions In this book I’ll use the term hacker often The word has two defini tions A hacker can be a person who studies a system such as the rules of a cipher or a piece of software to understand it so well that they’re not limited by that system’s original rules and can modify it in creative ways xxii Introduction
A hacker can also be a criminal who breaks into computer systems violates people’s privacy and causes damage This book uses the term in the first sense Hackers are cool Criminals are just people who think they’re being clever by breaking stuff what’s in this Book The first few chapters introduce basic Python and cryptography concepts Thereafter chapters generally alternate between explaining a program for a cipher and then explaining a program that hacks that cipher Each chap ter also includes practice questions to help you review what you’ve learned Chapter 1 Making Paper Cryptography Tools covers some simple paper tools showing how encryption was done before computers Chapter 2 Programming in the Interactive Shell explains how to use Python’s interactive shell to play around with code one line at a time Chapter 3 Strings and Writing Programs covers writing full programs and introduces the string data type used in all programs in this book Chapter 4 The Reverse Cipher explains how to write a simple pro gram for your first cipher Chapter 5 The Caesar Cipher covers a basic cipher first invented thou sands of years ago Chapter 6 Hacking the Caesar Cipher with Brute-Force explains the brute-force hacking technique and how to use it to decrypt messages without the encryption key Chapter 7 Encrypting with the Transposition Cipher introduces the transposition cipher and a program that encrypts messages with it Chapter 8 Decrypting with the Transposition Cipher covers the sec ond half of the transposition cipher being able to decrypt messages with a key Chapter 9 Programming a Program to Test Your Program introduces the programming technique of testing programs with other programs Chapter 10 Encrypting and Decrypting Files explains how to write programs that read files from and write files to the hard drive Chapter 11 Detecting English Programmatically describes how to make the computer detect English sentences Chapter 12 Hacking the Transposition Cipher combines the concepts from previous chapters to hack the transposition cipher Chapter 13 A Modular Arithmetic Module for the Affine Cipher explains the math concepts behind the affine cipher Chapter 14 Programming the Affine Cipher covers writing an affine cipher encryption program Chapter 15 Hacking the Affine Cipher explains how to write a pro gram to hack the affine cipher Introduction xxiii