Cryptography in Programming pt 1 — Caesar Cipherby@hackernoon-archives

# Cryptography in Programming pt 1 — Caesar Cipher

July 7th, 2017

What is cryptography? Well the word cryptography stems from the Greek words <strong>kryptos</strong> which means ‘hidden’ or ‘secret’, <strong>graphein</strong> which means ‘writing’ and <strong>logia</strong>, ‘study’. Cryptography itself is the practice and study of techniques for secure communication.

### Companies Mentioned

What is cryptography? Well the word cryptography stems from the Greek words kryptos which means ‘hidden’ or ‘secret’, graphein which means ‘writing’ and logia, ‘study’. Cryptography itself is the practice and study of techniques for secure communication.

What is interesting is that cryptography exists at the intersection of of several disciplines such as computer science, mathematics, and electrical engineering.

secret-key crypto-system

Let’s dive right in and examine cryptography in general. One type of cryptography is secret-key cryptography in which a sender will encrypt a message so that only the recipient can decrypt it and anyone who somehow gets a hold of this message will be unable to extract any information. In this diagram above we have this black box that we will call our encryption algorithm.

The algorithm itself takes in some kind of input and a secret key. After that, this crypto-system will perform some kind of action using these inputs and spit out an output, which we will expect to be some sort of encrypted message. This algorithm can be written as a function that takes the input and the key and processes it based on the encryption technique, which is the heart of the algorithm itself and then returns the encrypted output.

Ok. So we understand that the we can write this as a function and that a crypto system is an algorithm, or a set of instructions that we can actually program based on encryption techniques. Let’s take a look at a very popular encryption technique known as the Caesar Cipher.

Shift Cipher Technique

The Caesar Cipher is a very popular encryption technique that is so simple, you may have at some point implemented it yourself. Perhaps in grade school, you passed around notes and you shifted every letter in the note up or down by a certain number so your teacher could not understand. This is pretty much how the caesar cipher works.

here we have plaintext and ciphertext

On the left we have some plaintext, or the unencrypted message. On the right we have the ciphertext or the output/return value once our input and secret key have been run through our encryption algorithm. In this case, our input would be the plaintext, and the number 1. To get from our plaintext to our ciphertext, all we have to do is shift each letter in the message up to the next letter. Pretty simple right? How would we do this in code? Here is an implementation of the caesar cipher I wrote in C.

Caesar Cipher in C

Let’s walk through this code together. Even if you are not familiar with C, it shouldn’t be too difficult to see what is happening here. Also, I have included some comments alongside the code.

Step 1 — check to see if we have the right number of arguments via command-line. When running this program, we must pass in the secret-key, a number, which will be used throughout the function. The way this is implemented isn’t too important to the overall algorithm, but at some point you need a key.

Step 2 — obtain plaintext using get_string function which will prompt user to type in a string and store it in variable s.

Step 3 — take the key that was passed in when the function was called at command-line and cast it into an integer. At this point the argument is a string, so we must change it to an actual number to continue to work with it as such.

Step 4 — initialize for loop and start to iterate through the plaintext. Note: strlen() is used to calculate the length of a string.

Step 5 — set type character variable c as the current letter we will process

Step 6 — check to see if current character is a letter and then check to see if letter is uppercase or lowercase. The formula we are using is case-sensitive as ASCII number representations for lowercase letters are different for uppercase letters.

Step 7 — use cipher formula c_i = ( (p_i) + key ) % 26.

For every i’th plaintext character, we add the key and mod it by 26. So that we wrap around correctly if were on, let’s say the letter z, and we have to shift up 1, it should wrap around to the letter a.

The caveat here is that we must first bring the each letter down to its alphabetical index which should be anywhere from 0 to 25, as there are 26 letters in the alphabet. Once we do that, we can apply the formula, and then bring it back out of the alphabetical index to yield the correct encrypted letter.

Since lowercase letters start at 97, we will take the letter, subtract 97 ( in C this is valid, as it will treat the letter as an integer value ) , add the key, mod 26, then add 97 back on. We do something very similar for the uppercase letters except instead of 97, we use 65.

And there we have it! Caesar Cipher algorithm in C.

In the next part of this blog, we’ll explore where and why programmers should draw the line on taking encryption into their own hands. More specifically how we as programmers handle authentication.

L O A D I N G
. . . comments & more!