Hacking Secret Ciphers With Python

Transcription

Hacking SecretCiphers with PythonBy Al Sweigart

Copyright 2013 by Al SweigartSome Rights Reserved. “Hacking Secret Ciphers with Python” is licensed under a CreativeCommons Attribution-Noncommercial-Share Alike 3.0 United States License.You are free:To Share — to copy, distribute, display, and perform the workTo Remix — to make derivative worksUnder the following conditions:Attribution — You must attribute the work in the manner specified by the author orlicensor (but not in any way that suggests that they endorse you or your use of the work).(Visibly include the title and author's name in any excerpts of this work.)Noncommercial — You may not use this work for commercial purposes.Share Alike — If you alter, transform, or build upon this work, you may distributethe resulting work only under the same or similar license to this one.This summary is located here: s/ Your fair use and other rights arein no way affected by the above. There is a human-readable summary of the Legal Code (the full license), located 3.0/us/legalcodeBook Version 3Special thanks to Ari Lacenski. I can’t thank her enough. Without her efforts there’d be typos literally on every page.Thanks to Jason Kibbe. Cover lock photo by “walknboston” http://www.flickr.com/photos/walkn/3859852351/ Romeo& Juliet and other public domain texts from Project Gutenberg. Various image resources from Wikipedia. Wrinkledpaper texture by Pink Sherbet Photography 7/ Computer Usericon by Katzenbaer.If you've downloaded this book from a torrent, it’s probably out of date. Goto http://inventwithpython.com/hacking to download the latest version.ISBN 978-14826143741st Edition

Nedroid Picture Diary by Anthony Clark, http://nedroid.comMovies and TV shows always make hacking look exciting with furious typing and meaninglessones and zeros flying across the screen. They make hacking look like something that you have tobe super smart to learn. They make hacking look like magic.It’s not magic. It’s based on computers, and everything computers do have logicalprinciples behind them which can be learned and understood. Even when you don’tunderstand or when the computer does something frustrating or mysterious, there is always,always, always a reason why.And it’s not hard to learn. This book assumes you know nothing about cryptography orprogramming, and helps you learn, step by step, how to write programs that can hack encryptedmessages. Good luck and have fun!

100% of the profits from this book are donatedto the Electronic Frontier Foundation, the Creative Commons, and the Tor Project.

Dedicated to Aaron Swartz, 1986 – 2013“Aaron was part of an army of citizens that believes democracyonly works when the citizenry are informed, when we know aboutour rights—and our obligations. An army that believes we mustmake justice and knowledge available to all—not just the well bornor those that have grabbed the reins of power—so that we maygovern 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.”- C.M.

ABOUT THIS BOOKThere are many books that teach beginners how to write secret messages using ciphers. There area couple books that teach beginners how to hack ciphers. As far as I can tell, there are no books toteach beginners how to write programs to hack ciphers. This book fills that gap.This book is for complete beginners who do not know anything about encryption, hacking, orcryptography. The ciphers in this book (except for the RSA cipher in the last chapter) are allcenturies old, and modern computers now have the computational power to hack their encryptedmessages. No modern organization or individuals use these ciphers anymore. As such, there’s noreasonable context in which you could get into legal trouble for the information in this book.This book is for complete beginners who have never programmed before. This book teaches basicprogramming concepts with the Python programming language. Python is the best language forbeginners to learn programming: it is simple and readable yet also a powerful programminglanguage used by professional software developers. The Python software can be downloaded forfree from http://python.org and runs on Linux, Windows, OS X, and the Raspberry Pi.There are two definitions of “hacker”. A hacker is a person who studies a system (such as therules of a cipher or a piece of software) to understand it so well that they are not limited by theoriginal rules of that system and can creatively modify it to work in new ways. “Hacker” is alsoused to mean criminals who break into computer systems, violate people’s privacy, and causedamage. This book uses “hacker” in the first sense. Hackers are cool. Criminals are just peoplewho think they’re being clever by breaking stuff. Personally, my day job as a softwaredeveloper pays me way more for less work than writing a virus or doing an Internet scam would.On a side note, don’t use any of the encryption programs in this book for your actual files.They’re fun to play with but they don’t provide true security. And in general, you shouldn’t trustthe ciphers that you yourself make. As legendary cryptographer Bruce Schneier put it, “Anyone,from the most clueless amateur to the best cryptographer, can create an algorithm that he himselfcan’t break. It’s not even hard. What is hard is creating an algorithm that no one else can break,even after years of analysis. And the only way to prove that is to subject the algorithm to years ofanalysis by the best cryptographers around.”This book is released under a Creative Commons license and is free to copy and distribute (aslong as you don’t charge money for it). The book can be downloaded for free from its website athttp://inventwithpython.com/hacking. If you ever have questions about how these programs work,feel free to email me at al@inventwithpython.com.

TABLE OF CONTENTSAbout This Book . 6Table of Contents . 7Chapter 1 - Making Paper Cryptography Tools . 1What is Cryptography? . 2Codes vs. Ciphers . 2Making a Paper Cipher Wheel . 3A Virtual Cipher Wheel . 7How to Encrypt with the Cipher Wheel . 8How to Decrypt with the Cipher Wheel . 9A Different Cipher Tool: The St. Cyr Slide . 10Practice Exercises, Chapter 1, Set A . 11Doing Cryptography without Paper Tools . 11Practice Exercises, Chapter 1, Set B . 13Double-Strength Encryption?. 13Programming a Computer to do Encryption . 14Chapter 2 - Installing Python . 16Downloading and Installing Python . 17Downloading pyperclip.py . 18Starting IDLE . 18The Featured Programs . 19Line Numbers and Spaces . 20Text Wrapping in This Book . 20Tracing the Program Online . 21Checking Your Typed Code with the Online Diff Tool . 21Copying and Pasting Text . 21More Info Links . 22Programming and Cryptography . 22Chapter 3 - The Interactive Shell . 26Some Simple Math Stuff . 26Integers and Floating Point Values . 27

Expressions . 27Order of Operations . 28Evaluating Expressions . 29Errors are Okay!. 29Practice Exercises, Chapter 3, Set A . 30Every Value has a Data Type . 30Storing Values in Variables with Assignment Statements . 30Overwriting Variables . 32Using More Than One Variable . 33Variable Names. 34Practice Exercises, Chapter 3, Set B . 35Summary - But When Are We Going to Start Hacking?. 35Chapter 4 - Strings and Writing Programs . 36Strings . 36String Concatenation with the Operator . 38String Replication with the * Operator . 39Printing Values with the print() Function . 39Escape Characters . 40Quotes and Double Quotes . 41Practice Exercises, Chapter 4, Set A .

Personally, my day job as a software developer pays me way more for less work than writing a virus or doing an Internet scam would. On a side note, don’t use any of the encryption programs in this book for your actual files. They’re fun to play with but they don’t provide true security. And in general, you shouldn’t trust the ciphers that you yourself make. As legendary cryptographer .