Subscribe via RSS Become a friend on Facebook Follow me on Twitter

Cymons Games

CaesarCipher

August 20th, 2010

The shift cipher is one of the most basic ways of encoding messages. It is simple enough that a child with a piece of paper, or perhaps a more complicated aid like a decoder wheel or code ring, is able to encrypt messages on their own. It consists of replacing each letter in your message with the letter some fixed distance further down the alphabet. So a shift cipher of 1 would replace all the As with B, B with C, and so on. A shift of 10 would replace A with K, B with L, and so on. When you reach Z you simply wrap the alphabet around to A again, so in the example where A is K, then P is Z, so Q is A, and R is B, etc. The shift cipher was used by Julius Caesar to communicate with his generals, hence it is also called the Caesar cipher.

In practice the Caesar cipher offers almost no communication security. There are only 25 possible ways for you to encode your message, so even without the key it’s possible to exhaust decode the message by exhaustively checking all keys. This program has been set up to do just that with the “break” option. Just type in your message and let it try all the possibilities for you.

To increase security with your shift cipher you can advance the key after every letter, changing the key and making it much more difficult to break using an exhaustive method. You will need to communicate to the receiver of the message what the initial shift is (by the value of what A equals initially) and how much to advance the key by every letter. This can be done by simply sending them through separate channels the letter and number they’ll need. So if you received the key “G5″ you’d have all you needed to decode the following message:

Zsu gtka nj Qhkscgwo Zozaoip ds dd ayrzw vwzwqjpzx unlj wnva kebh yw ywxua ljuow re’ft emwwlfhl trkspzjh tj.

Caesar Cipher is written by Joe Larson.

5 Responses to “CaesarCipher”

  1. Ratfink

    Nice little program! Although, I did see some problems with the source code. One is the use of the gets() function, which is very unsafe to use, since it doesn’t know how long the buffer is. It’s better to use fgets() instead. Trying to fix that, though, ended up making things even worse, so it will need quite a few changes I think. The other problem is quite simple to fix: replace things like:

    statement;
    while (condition)
    statement;

    with:

    do
    statement;
    while (condition);

    which is a lot cleaner, and means you don’t have to write the same code twice.


  2. Joe

    Excellent point Ratfink. I think you proved the reason for using gets(). It was mostly because there’s no better alternative. Well, there’s scanf(“%” xstr(n) “[^\n]%*[^\n]“, s), but that’s just confusing. I suppose I could just write a macro for that so I could use it in the future. Hmmm.

    So while C’s string handling routines leave something to be desired this is mostly academic, so it’s not that big a deal.

    Now as for the do while thing, yeah your right. There are a couple of places there that I could fix. I’ll get on that and update the code.


  3. Ratfink

    Another alternative to gets() is using fgets for all the input, including single-character input. Just use a very small buffer, and only check the first character. That might fix the input problem, but I’d have to try it to be sure.


  4. Jordi Fita

    Actually, the gets()’s man page states:

    “Never use gets().”

    Even if this is mostly academic, I would change the message input to this:

    do {
    fgets(message, sizeof(message), stdin);
    } while (!message[0] || message[0] == ‘\n’);

    From my point of view is a little clearer than using the fgets(…); while (…) fgets (…) as Ratfink pointed out, and it uses fgets instead of get, which is as easy as gets() but without the nasty possible buffer overflow.

    By the way, gcc also warns against the use of gets().


  5. Joe

    Tho I hardly do enough to that end, this is a site that is all about learning. To that end I have taken your snippet and integrated it into the code. Thanks for that.

    Yeah, I know gets is bad in general, but it’s easy when you’re learning, both to use and be understood what you’re doing. And since when you’re writing other applications stdio is rarely the I/O of choice.

    Also, nobody commented on the fact that the encoded message above was wrong, that I skipped a space and that messed things up. I’ve fixed it now, but shame on everyone for not trying out the code. It’s fixed now.

    key M3
    Qkwmw xpwr B vtnbp fl rhgm X/J J zkl bp ks xm bbag uuzw “IIFY!”


Leave a Reply

Cymons Games. All programs provided without guarantee or warranty. Maintained by Joseph Larson.
If you have any questions or notice something is wrong please contact me. Powered by WordPress.