Crypto Algs (Part 1.0): The Caesar Cipher

algorithm
caesarcipher
encryption

(oaktree) #1

Hey guys! I thought I would start a little series on cryptography algorithms. I’m going to run out this last hour before Mr. Robot’s season 2 airs.

With that said, let’s get down to business. The Caesar Cipher is named for its alleged use by the Roman Emperor Julius Caesar. In order to “ensure” the confidentiality of certain military-related messages, Caesar would perform an alphabetical shift on his messages.

Here’s an example.

C H E E S E

F K H H V H

Can you guess what the key is? It is… three (3)!

Okay. So all we did was shift each letter over to the “right” by three. Easy enough. What if the letters end up shifting past the alphabet?

X R A Y

A S B Z

All we must do is wrap around – and there is an easy way to tell a computer to do this.

Let’s develop an expression for the shift. Let’s say that the index of “A” is 0, and the index of “X” is 25. Our key, or shift-value, is k.

Our expression would look like this:

character = ( index(character) + k ) % 26 + "A"

Let’s shift “B” over by 3 as an example. The index of “B” is 1.

1 + 3 = 4
4 % 26 = 4
4 + "A" = "E"

The Code

#include <vector>
#include <string>
#include <iostream>

int main() {
    std::string s; std::getline(std::cin, s);
    int k; std::cin >> k;

    for (auto& c : s) {
        if (c >= 'a' && c <= 'z') {

            c = ((c - 'a' + k) % 26) + 'a';

        } else if (c >= 'A' && c <= 'Z') {

            c = ((c - 'A' + k) % 26) + 'A';

        }
    }

    std::cout << s << std::endl;
    return 0;
}

You’ll notice that, rather than using some index() method, I subtract c from 'a' to get an index. This is because your computer really just treats characters as special unsigned, one-byte (usually), ints.

In the Caesar Cipher, we ignore non-alphabetical characters because we won’t shift those. What happens if you shift a period?

Next, I’ll be doing the vigenere cipher.

Later…
@oaktree

P.S. Nxs’y ymj hfjxfw hnumjw httq?


Crypto Algs (Part 1.1): Cracking Caesar
Crypto Algs (Part 2.0): Vigenere
#2

Great post mate! I remember when I used the Caesar cipher years ago to make confusing puzzles on reddit! It may be basic but it sure as hell gets the job done if used properly. I’ll look forward to anything else you have in store! Quick question, are you going to make a series with the basics, or are you going to move on to more advanced things quickly? If you plan on making this a series with notable basics I’d suggest a look at the Atbash cipher which was originally in the Hebrew alphabet. It’s one of the basic ciphers where you replace letters with other letters. Say, replace a e with an n. A bit harder to crack than the Caesar, but as said above can still get the job done if correctly used with other ciphers! Kudos!


(pico) #3

Pa’z wylaaf jvvs thal!


#4

@oaktree Great article ! Did you plan to write more about modern encryption schemes like CBC, ECB, … ? Upstream, I think that it would be relevant to add a section highlighting weaknesses, used to break the cipher ! What do you think ?

Umpqr clapwnrgml qafckc ctcp :stuck_out_tongue_closed_eyes:

Best,
Nitrax


(oaktree) #5

@Nitrax : I’ll do a Part 1.1 about breaking the Caesar Cipher. Next up is vigenere. I may or may not get to more advanced stuff like AES+CBC and what not, depending on how comfortable I am with them when the time comes.


(Security Architect & Founder) #6

Nice post! I’m a little confused with the code, since it is in C. I will however make an effort to read into it a little deeper.

When I was making one in ruby, I just made an array of the whole alphabet alphabet = “abcdefghijklmnopqrstuvwxyz”.split(""), and then just referenced the values from their indexes in the array. alphabet[0] would equal a and so on. If I wanted to increment it, I would just do alphabet[alphabet.index(“C”) + rot]


(pico) #7

Love it @oaktree!

It will be great a series of cryptographic posts covering increasingly complex algorithms and techniques.

As discussed, you may want to consider chaotic modulations/masking for a later post. This is a bit of a different view, more from the point of view of securing the signals and not directly the data (spread spectrum stuff and similar). It is pretty interesting the discussion about chaotic subsystem synchronization and how it can be use to mask signals into other signals… so, is is maybe closer to stenography than classical crypto. The maths are cools anyway :slight_smile:

As a side note I will drop some related reading… not directly cryptography but I found it very interesting specially as an example on how to bring together things that may look completely unrelated

http://lcamtuf.coredump.cx/oldtcp/tcpseq.html

It is pretty useless nowadays but it is an interesting reading specially with regards to randomness that is most related to cryptology.

Congrat mate!


(oaktree) #8

@pry0cc: you could do that, but it’s easier in C or C++ to just use character subtraction, since characters are really just unsigned integers, like I mentioned.


(oaktree) #9

This topic was automatically closed after 30 days. New replies are no longer allowed.