# Week 2: Cool Kids – Will

Square2Marketing.com

We all have secrets. Some of us are trying to trying to hide them from our friends, spouses, or even totalitarian governments. This week in CS50 we covered cryptography; the art of keeping secrets.  Cryptography is an incredibly immense and incredible science dating back to antiquity with the Greeks and the Arabs. Also one that is incredibly relevant to the privacy crisis we have going on today. Let’s dive into three examples of cryptography in this weeks PSet 2.

initials.c

Initial.c is not an encryption task, it is a string manipulation task. Our goal is simple, to receive a name from the user and return to them their initials.  To understand a little about why this task is interesting let’s take a short dive into how computers store text. Everything a computer deals with is binary, on’s and off’s, zeros and ones. However, Computer Science allows us to abstract away the pesky binary into constructions and syntax that makes up programming languages. In C, and most other modern languages,  characters are stored in a system called ascii. Ascii allows us to represent characters using a series of binary bits (lots of 1’s and 0’s) and thus make characters. Now how might we make words? We can make array’s of these characters (chars) to represent words. This means that each letter is at a certain numeric index (position) and it’s relatively easy to step through an array and look at each character.

With this knowledge, we can quickly design an algorithm to give us initials. First we’re going to automatically assume the first character in the name in an initial. Next we’re going to keep stepping through this array of characters until we find a position that contains a space, and has a valid character after it (ie. a letter, not another space), and we will call this our next initial. We will continue this second step until we reach the end of the array when we will print these initials.

Nice warmup, huh?

caesar.c

A Caesarian shift cipher is perhaps the most ubiquitous cipher every created. This simple algorithm goes back to Julius Caesar himself, who at least according to Suetonius in The Twelve Caesar’s, used it himself to protect important military messages. The cipher is simply, shift each letter in the message either forward or backwards some number of characters.

For example ‘Go Town! Beat George!’ shifted twenty one characters would be ‘Bj Ojri! Wzvo Bzjmbz!’ after shifting. While on paper this is pretty easy, it is a pretty non trivial programming challenge. It requires us to develop two algorithms that we derive from the ASCII table, one for upper case, and one for lower. While this builds on top of the string manipulation we saw in initials.c it is also a challenging forward. This is our first time deriving algorithms by ourself and would prove quick the challenge to the first time programmer.

vigenere.c

The Last 40 Lines of my implementation

The last problem of our second Pset involved us implementing a more complex form of cipher called a Vigenére cipher. The Vigenêre cipher works like a Caesarian shift, however we do not shift based on a constant, we shift based off of a key string. For example we’d have our key string ‘Beat George’ and our message ‘Go Town’. We’d offset each letter in ‘Go Town’ based on the position in the alphabet of the corresponding letter in ‘Beat George’ giving us ‘Hs Thcr’. This cipher, while still incredibly insecure by modern standards, was an incredible step forward compared to the Caesarian cipher  (which could be broken by trying all 26 possibilities).

This was yet another problem to teach us string manipulation and algorithm design. If you are interested into exactly how exactly we might implement this you can see the algorithm in the image above.

Postmortem

This was a difficult week in CS50; the second problem set was a major leap forward in difficulty. While the subject at hand, storing, modifying, and manipulating strings isn’t incredibly difficult it is certainly compounded in difficulty if you haven’t seen cryptography before. I can see a lot of students will struggle on this first real deep dive into C and I am happy I did not share their toil. This week was a lot of fun for me, cryptography is one of my passions, and my familiarity made implementing these ciphers trivial. If cryptography piqued your interest at all I highly recommend reading The Code Book: The Evolution Of Secrecy From Mary, Queen Of Scots To Quantum Cryptography by Simon Singh. It is a great nontechnical introduction to the history and science of Cryptography.

## 3 thoughts on “Week 2: Cool Kids – Will”

1. randyhimself

I think it’s pretty incredible that not only do you know the theory of encryption but also you are able to put it into practice on computers. Being able to execute ideas extremely important and something that I really need. Keep up with the good work. And maybe use “incredible” less in your first paragraph?

2. cleokelleher

This is really impressive coding! I’m impressed to see the whole thing presented directly in front of me (being that my coding experience goes no further than Java). I find it amazing that you’ve been able to pick up all of this in just a short week. Keep up the good work!

3. realrowo

Good work Will! It’s great learning from you how to code in such a clear and decent way. I especially want to lift up the pictures and video clips you use for your blog posts. They simply make your post more colorful and provides readers with a tool to understand what you are talking about. Great job!

This site uses Akismet to reduce spam. Learn how your comment data is processed.