Caesar Cipher

I enjoy and have been working with cryptography for a couple decades or so. When I saw in HackerRank the challenged named Caesar Cipher I jumped in to solve it.

The cipher is very old and by itself it is completely insecure. One can write a very short program to convert cipher text into plain text which would execute in milliseconds. That said; it was fun to work on the solution.

If interested first take a look at the following URL in HackerRank:  https://www.hackerrank.com/challenges/caesar-cipher-1?utm_campaign=challenge-recommendation&utm_medium=email&utm_source=24-hour-campaign

It seems like the input format specifies the length of the plain text string in addition to the actual string in order to be able to generate the cipher text directly without having to read the entire string. I decided to pass on such offer.

I also noted that the key is in the range 0 <=  key <= 100. It seems that a key which does not involve an effective rotation should be excluded. On the other hand, for the challenge any key % 26 == 0 key (e.g., 0, 26) should return the plain text as cipher text.

From the console of the Eclipse IDE:

11

middle-Outz <== plain text

26

middle-Outz <== cipher text

The test case follows:

11

middle-Outz

2

okffng-Qwvb

It seems to match the expected output.

My solution in Java follows:

import java.util.Scanner;

public class Solution {

// **** ****

final static int letterCount      = 26;

/**

* Encrypt the specified plain text.

* The key may be 0 <= key <= 100.

* A key == 0 or key % 26 == 0 would NOT encrypt the plain text :o(

*/

static String encrypt(String plainText, int key) {

StringBuilder cipherText = new StringBuilder(plainText.length());

for (int i = 0; i < plainText.length(); i++) {

char c;

char p               = plainText.charAt(i);

int charType = Character.getType(p);

int j;

// **** lower case letter ****

if (charType == Character.LOWERCASE_LETTER) {

j                    = p – ‘a’;

j                    = (j + key) % letterCount;

c                    = (char)((int)’a’ + j);

cipherText.append(c);

}

// **** upper case letter ****

else if (charType == Character.UPPERCASE_LETTER) {

j                    = p – ‘A’;

j                    = (j + key) % letterCount;

c                    = (char)((int)’A’ + j);

cipherText.append(c);

}

// **** not a letter ****

else {

cipherText.append(plainText.substring(i, i + 1));

}

}

return cipherText.toString();

}

/**

* Test code.

* @param args

*/

public static void main(String[] args) {

Scanner sc           = new Scanner(System.in);

int n                = sc.nextInt();

String plainText     = sc.next();

int key              = sc.nextInt();

sc.close();

System.out.println(encrypt(plainText, key));

}

}

If you have comments or questions regarding this post or any other one in this blog, please do not hesitate and send me an email message. I will not use your name unless you explicitly allow me to do so.

John

john.canessa@gmail.com

Follow me on Twitter: @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

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