Code Cryptography & Ciphers in Python

In this 6 module course, we will explore some basic programming concepts in Python to build ciphers, encode and decode secret messages, and learn about how cryptography works! If you enjoy the content, please drop me a line and let me know.

Let's get started!

Data Wrangling for Youth Program Evaluation

Module 1

Module 1 - Working with Strings


Hi everyone! Welcome to Python Programming for Beginners: Code Cryptography and Ciphers - 6 Week Flex course! In this class we are going to learn all about encoding and decoding text in Python, make cyphers, and hopefully decode as well as encode our secret messages!


This week we worked with strings of different kinds, emojis, and lots of different kinds of text. We worked on some code to swap letters from one form to another, and build a substitution code using different strings, even turning fancy letters into emojis!


In this course, we are going to use https://replit.com/~ Its easy to get started and sign up. When you click on the code in any of the modules, they will pop up and allow you to fork or copy the code into your account. If you made a mistake, you can just go back and create another copy. You can click the share button to get a link and share your code.


Today we worked on strings

https://replit.com/@SidneyShapiro/DefiniteWryLocks


Fancy multiline ASCII text

http://patorjk.com/software/taag/#p=display&f=Small&t=Secret!


Replacing letters with each other, a list and a loop

https://replit.com/@SidneyShapiro/FaithfulVainLint


Differences between variables and lists

https://replit.com/@SidneyShapiro/DodgerblueRemorsefulNetworking


Emoji secret messages

https://replit.com/@SidneyShapiro/UniformLightcyanAdaware


Emoji cheat sheet

https://www.webfx.com/tools/emoji-cheat-sheet/


The emoji package

https://pypi.org/project/emoji/


Setting up a translation substitution

https://replit.com/@SidneyShapiro/SafeImaginativeClient


Module video

Click to play YouTube video


Module slides

https://ln.sync.com/dl/fa67eee50/fj6mt6wb-2v42ciyd-d83ncvif-68zmabhn


Homework

For this week, get set up on repl.it and work on encoding and decoding. For some of the code we looked at in this module, you will need to build the decoder to get the text back to its original form. Make up your own secret message and send it into the encoding program and see what you can come up with. Make sure you can also decode it using the program. When you are done, share it back in the classroom to share with the class. If you have any questions or get stuck, send me a message in the teachers section or the classroom. Looking forward to next week!


Can you decode this weeks secret message?

hee blf mecg deek fli mldfle 2# all ghih galk ablfg kiaaa amd i am kieggb hfmgib fli hlme kiaaa#

Module 2

Module 2 - Hiding Messages in Text


Welcome to Module 2! More codes this week and we are going to have a lot of fun hiding text. We are going to use some huge text like books and look at strings, lists, index positions and dictionaries. But before we get to all of that, we are going to look at some really neat code to recap our string conversion from last week and look at different ideas to expand it. The focus this week is on frequency, or how often a word or letter shows up inside of a string. This module has two videos, with the second one going over the last bit of code and a recap. It may be a good idea to take a break and practice coding between watching them :) Slides for this module are attached.


We started with an example of string replace

https://replit.com/@SidneyShapiro/ImpossibleStudiousGreyware


We looked at strings, lists, and index positions, we even printed out a number of index positions using a slice

https://replit.com/@SidneyShapiro/TautFancyAnalyst


We looked at the idea of using row number and index position to code our secret message

https://replit.com/@SidneyShapiro/UltimateVibrantSignature


Then we expanded that idea to build a substring finder, which will tell us where the index positions are

https://replit.com/@SidneyShapiro/WorthwhileAliveInitialization


Building on letter index positions, we did the same thing, but this time we looked at entire words and got the index position of where they stop and start

https://replit.com/@SidneyShapiro/ShimmeringImperturbableAccounting


We started counting how many times a word appears, this will be really useful later as we analyze frequencies

https://replit.com/@SidneyShapiro/CommonBurlySupport


Using re or regular expressions

https://replit.com/@SidneyShapiro/AcademicVitalObjectcode


Dictionaries, and lists in loops, remember that dictionaries have key/value pairs and lists have index positions

https://replit.com/@SidneyShapiro/DarkkhakiUnwillingExponent


Coding with much bigger data using the Hounds of the Baskervilles, and include some pretty neat frequency code

https://replit.com/@SidneyShapiro/IntentRedLevels


Module video

Click to play YouTube video

Click to play YouTube video


Module slides

https://ln.sync.com/dl/d2c4a6db0/dx8b5xme-4avjr9zd-x2iksshu-fsrajkku


Homework: For this week, practice with lists and dictionaries. Review the code from this module, and use one of the methods we looked at to encode and decode a string. Send it to the classroom to share with everyone or send it to me in the teacher section. If you run into trouble, just send me a message and I can help (hopefully) :)


This weeks puzzle code (using the first example we looked at): why4is4my4ca34s3ill4sl11ping?4i43hink4sh14is43ir1d4from4h1r4nap

Module 3

Module 3 - Shift Cyphers and Brute Force Decoding


In Module 3 we look at some exciting new ideas about encoding using index positions, looking at the numbers behind each key (attached is a table of the ASCII number codes), and building programs to encrypt the text and decrypt it using the brute force method (which means it tries every possibility to find the answer). For a short bit of text, it should not take very long to find the answer, for longer encoded text, it could take a very long time to run through all the possibilities.


Formatting text and finding the ASCII codes for numbers

https://replit.com/@SidneyShapiro/FlusteredCriticalServices


Shift encoder

https://replit.com/@SidneyShapiro/GiganticWirelessBuckets


Brute force decryption 1

https://replit.com/@SidneyShapiro/DistortedPossibleGlueware


Brute force decryption 2 - this version makes a list at the end

https://replit.com/@SidneyShapiro/LoyalDarkvioletMultitasking


Substitution matrix - can you build the decoder?

https://replit.com/@SidneyShapiro/SaltySuperbRelationaldatabase


Index position finder 1

https://replit.com/@SidneyShapiro/CrimsonInsidiousMaps


Index position finder 2

https://replit.com/@SidneyShapiro/ParallelFrankAmpersand


Module video

Click to play YouTube video


Module slides

https://ln.sync.com/dl/66ad4fa90/gkfcnsqc-nnp67u7s-nizjfhd6-ymjmyq53


Homework: This week work on building a decoder for the substitution matrix, and then send some code through the shift encoder and see if you can find the clear decoded text using the brute force program. You can also print out the manual encoder wheel and see if you can first come up with a code on paper, and then confirm your message using the encoding program. Ask for help when doing crafts and be safe.


This weeks puzzle code (using the encoder with a different shift pattern): VHwLJCwRBwWJVNMwBQJRWJwMXwHXDwQJENwJwYNCb

Module 4

Module 4 - Vigenère and Rail Fence Cipher


In module 4 we look at the Vigenère cypher, which is a polyalphabetic cypher. This means it uses more than one shift sequence to encode and decode, making it super hard to break with a small amount of text. In fact, this code was thought to be unbreakable for almost 400 years!


We started with our apollo text

https://replit.com/@SidneyShapiro/BurlyWideeyedSpyware


Alice in Wonderland text

https://replit.com/@SidneyShapiro/AngryNiftyMatter


Project Gutenberg free books

https://www.gutenberg.org/ebooks/11


Decrypt program 1

https://replit.com/@SidneyShapiro/GuiltyLazyCosmos


Decrypt program 2

https://replit.com/@SidneyShapiro/LightblueHardtofindSolidstatedrive


Railfence

https://replit.com/@SidneyShapiro/RundownApprehensiveColdfusion


Tabs to spaces

https://tabstospaces.com/


Module video

Click to play YouTube video


Module slides

https://ln.sync.com/dl/9bb709140/pyw6ru8a-bktszqap-ebuifen6-ah2u3zur


Homework

For this week, work on a polyalphabetic cypher on paper and see if you can encode and decode the text using the programs we looked at. With shorter text it will be a lot harder for the computer to work out the sequence of your key.


Code using the 1,2,3 key:

ujlt ydt c yftb gwq xghl ob dcw tcbt vkbv hwgq ujrvik tjh icg gwq tjh tvlmn qfggt vr ocs cgipth eqloi doa ppth dqgjpj

Module 5

Module 5 - Encryption and Decryption in Python


In this module we are going to look at more complex encryption using base64 passwords and the security standard called AES/Fernet. In this module we are focused on an approach called symmetric encryption, which is when a key is used to encrypt and decrypt a message, so whoever encrypted it can decrypt it. The only way to decrypt the message is to know what was used to encrypt it; kind of like a password.

We started with a shift cypher program for a file

https://replit.com/@SidneyShapiro/RoyalUnlawfulMicrokernel


Key generator 1

https://replit.com/@SidneyShapiro/RaggedCompetentIntegers


Key generator 2

https://replit.com/@SidneyShapiro/AquaGraciousCodec


Encode and decode strings using fernet

https://replit.com/@SidneyShapiro/DeadEagerWifi


Encode and decode files using fernet

https://replit.com/@SidneyShapiro/UntidyNonstopAbility


Module video

Click to play YouTube video


Module slides

https://ln3.sync.com/dl/0f256e600/se8jubbm-qakrfys7-b25wc8ea-cdw6qpt8


Homework

This week, create a base 64 key in bytes and encode some text, either in a string or a file and then decrypt it. See if you can work with the encoded text using one of the decryption methods we used (this is pretty much impossible)


Code using Fernet:

==========Encrypting==========

b'gAAAAABglrfZv66JX6RufsNr5id3tL7_odg43H4ryHUTVJ84a1V5IsMu5zT0dZS2cW8iUNhqsa1OEtwb9Sag1kRnV8YR7P5CjWc0AeXlyE9bBzrtUMbO2sw='

using key key = b'fWn9BDrXryrtcxjXhaO3BR9Oc_bS_zk1k4b6aL_0rbI='

Module 6

Module 6 - This week we worked on some new code ideas including has functions and steganography, which is hiding code inside of images


We started with a program that builds hashes for a string

https://replit.com/@SidneyShapiro/EverlastingRawChapters


We expanded this to compare the hash of a password a user enters against the has we stored

https://replit.com/@SidneyShapiro/DefinitiveMessyConfiguration


We then looked at that idea but added to it with a list of passwords and a loop inside of a loop, allowing us to check for even more options

https://replit.com/@SidneyShapiro/OffshoreCrowdedSmalltalk


Finally, we looked at steganography, hiding code inside of an image. This is not the same as encryption, but does let us change the bytes of an image to hide a string

https://replit.com/@SidneyShapiro/KlutzyGreedyMosaic


Color mixer

https://www.w3schools.com/html/html_colors_rgb.asp


Decimal to binary

https://www.rapidtables.com/convert/number/decimal-to-binary.html


ASCII to binary

https://www.rapidtables.com/convert/number/ascii-hex-bin-dec-converter.html


Module slides

https://ln3.sync.com/dl/e4f0ae0e0/fcumfxzn-9v78zpzd-vbm49bih-jtbgbxjz


Module video

Click to play YouTube video


Homework

Upload your own image to the steganography program and encode a string, see if you can encode and decode a message inside of an image. Then, work on a program that checks the hash of your password against the hash of another word by expanding the examples we worked on.


I hope you had fun in the course and learned a lot about codes in Python, have an amazing week!