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!