Help Review Code for the Cryptography and Code Breaking Book

I have the source code written for my next book, "Hacking Secret Ciphers with Python" (formerly "Become a Code Breaker with Python"). (You can download the rough draft.)

The programs all implement classic cryptographic ciphers (Caesar cipher, simple substitution, etc.) as well as programs that can crack them. I'm posting it for public review so people can make any suggestions for edits. The code is loaded on github here:

These programs are for a book that teaches computer programming in Python 3 to complete beginners, as well as teaching amateur cryptography. My aim for the programs are:

  • Simple - Is the code arranged in a way that requires the least amount of prerequisite knowledge? Is there a chunk of code that should have more comments? Is there a comment that doesn't make sense?
  • Correctness - Is the cipher implemented correctly according to descriptions of the cipher?
  • Consistency - Is the naming and spacing of the code consistent with the other programs?

Things that are not goals for the source:

  • Security is not a primary concern. These are educational examples to teach basic cryptography concepts. Please withhold comments or criticisms that would be more appropriate for commercial encryption software. I understand that Python's random module is not suitable for crypto. I understand that the RSA block cipher should use CBC mode instead of ECB mode. These decisions have been made because the point of this book is to teach and be simple for the reader to understand without going into the arcana of cryptography.
  • Efficient and elegant code is also not a primary concern. For example, I don't use list comprehensions because it would add another programming topic to explain in the book. The code uses more lines than it probably needs too, but I'd rather have more-but-readable code than less-but-elegant code. If there's an order of magnitude improvement that I can make, I'll do it. Otherwise, a few extra seconds of processing time is worth having simpler code.
  • The code doesn't dogmatically pass PEP-8. "Meh."

Thanks to anyone who can offer their advice!

Read More

How Does Compression Work?

"Zip" programs that can compress multiple files into one smaller .zip file are fairly popular for downloads since the fewer bytes you have to download the faster it will download. But how do you compress files? Files are made up of ones and zeros, which can't be squished like clothes into a tight suitcase.

Think about file compression working like this: If someone asked you what the lyrics to "99 bottles of beer on the wall" were, you would tell them:

"The song goes '99 bottles of beer on the wall, 99 bottles of beer, take one down, pass it around, 98 bottles of beer on the wall', and then you repeat the lyrics with a number one less than the previous time."

The above sentence has 42 words in it. You wouldn't tell the person literally the entire song (which has 2,376 words in it). The reason you don't have to recite the full song is because of the song's repetition. Repetition makes compression possible. We've achieved a 98% compression rate (counting by the word amount) with the description of the lyrics over the literal lyrics (42 words vs. 2,376 words), even though both detail exactly the same song.

Read More

Source Code Makeover: Square Shooter, Part 2

UPDATE: Felix, the original author of Square Shooter, has made some comments in the comment section that are pretty insightful. I recommend you check them out.

This is a continuation from Part 1, where I go through the source code of Square Shooter, an Asteroids clone, and try to redesign the code to be more readable. (Part 3 is also up.)

All of the changes I make can be viewed on github. You can also download the original source code for Square Shooter.

Read More

Source Code Makeover: Square Shooter, Part 1

UPDATE: Felix, the original author of Square Shooter, has made some comments in the comment section that are pretty insightful. I recommend you check them out.

In this blog post, I'm taking a game off of and going through it to make it more readable and extend its functionality. You'll see an example of how to take code that works and changes to improve it's design (but more importantly, I explain why I make those changes). This is an intermediate level tutorial and assumes some familiarity with Python. This can be pretty helpful if you know programming basics but want to know, "How can I write better code?"

(Part 2 and Part 3 are now up as well.)

This blog post covers Square Shooter, made by Felix Pleșoianu. You can download the original source or view my changes on GitHub.

I only check in working code, so you can download the code at any point in history and see how it looks. I make a lot more check ins than I normally would, but I want you to see the gradual changes that I make.

(UPDATE: A note to everyone who mentions that I break PEP-8, see this link. Also, see this link.)

Let's play it. You'll need to have Python (a version 2, not version 3) and Pygame installed on your computer to run the file. It looks like a standard Asteroids clone. I notice that the circles dont wrap around the map until the center of the circle goes past the edge. Also, the bullets don't wrap around either, they just stop once they get to the edge.

Now take a look at the original source code. Try to get an idea of what each of the parts do, but you don't need to fully understand everything before continuing. (I didn't understand all the code when I started cleaning it up.)

First we'll do just a code clean up, and then we'll look at changing the the game to improve play and user experience.

The computer doesn't care what your code looks like. It will run the most unreadable code just fine. But the purpose of cleaning up code to be readable is so that it is easier to understand for humans so they can easily change code either for new features or bug fixes. This becomes especially important if multiple people are working on the same source code.


Read More