Caesar Shift Cipher
- Yahvin Gali
- Mar 19, 2021
- 4 min read
Updated: Sep 12, 2021
To encrypt/decrypt an English prompt (user input) based upon a user inputted 'phase shift', or a randomly generated one.
History
The concept of a substitution cipher was first used by Julius Caesar (around 50 BCE) during the Gaelic Wars. By substituting Greek for Roman, and even using a substitution cipher, Caesar managed to successfully send a coded message to Cicero (who was about to surrender). That cipher became known as the Caesar Shift. The concept of an encryption cipher evolved over time, and went on to be used in World Wars and the Cold War, as seen in the image above, which showcases a cipher machine used by the Axis Powers.
Caesar Shift encryption works by replacing each letter in the plaintext with another letter some number of positions down or up in the alphabet.
Introduction
This post is a combination of two projects: Caesar Shift Cipher and Random Cipher. Both are available in this Github Repository.

The Caesar Shift Cipher uses a variety of methods in order to encrypt and decrypt a user inputted English phrase via Caesar Shift value specified by the user. The Random Cipher takes this a step further, randomizing the Caesar Shift value for each phrase to be translated, which is once again provided by the user. Both programs work as shown in the image above: whatever number is provided as the shift value is the the exact number of positions that the alphabet is shifted. For example, if the shift value is -3, meaning that the alphabet shifts up 3 units (just like in the image above), the word "bee" would become "ybb".
Caesar Shift - encrypt(), decrypt(), and Output()
Both methods use Unicode values to encrypt and decrypt an English phrase. In this case, the shift is implemented directly in the Unicode value of each character of the English phrase, which is represented as a string. The code for encryption is shown below:
if (Character.isUpperCase(text.charAt(i))){
char ch = (char)(((int)text.charAt(i) + shift - 65) % 26 + 65);
result += ch;
}
else{
char ch = (char)(((int)text.charAt(i) + shift - 97) % 26 + 97);
result += ch;
}
For decryption, the exact opposite must be done. The Unicode value of the to-be-decrypted String must "lose" the Caesar Shift value. This is done as:
if (Character.isUpperCase(text.charAt(i))){
char ch = (char)(((int)text.charAt(i) - shift - 65) % 26 + 65);
result += ch;
}
else{
char ch = (char)(((int)text.charAt(i) - shift - 97) % 26 + 97);
result += ch;
}
Lastly, the method Output() asks for the User to provide both a phrase and the shift value, all in a while loop that continues to run until the user asks to quit. An example run is shown below (user input is bolded):
Enter 1 to Encrypt
Enter 2 to Decrypt
1
Begin? (Y/N):
Y
What would you like to Encrypt?
hello
What is the Phase Shift?
5
Encryption:
mjqqt
Continue? (Y/N)
N
Have a Nice day.
Random Cipher - createCipher(), encrypt(), decrypt(), and cipherResults()
The createCipher() method is central to this program, as it generates the random shift value for each letter of the alphabet. This is done via a for-loop, the Random class, and the .nextInt() method. The code is shown below:
public static char[] createCipher(){
char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z'};
Random generate = new Random();
for (int i = 0; i <alphabet.length; i++ ){
int i2 = generate.nextInt(alphabet.length);
char thing = alphabet[i];
alphabet[i] = alphabet[i2];
alphabet[i2] = thing;
}
return alphabet;
}
In essence, the letter at a random chosen index is switched with the letter at the target index, thus generating a random shift effect. The returned alphabet is the basis of the encrypt() and decrypt() methods, just like with the Caesar Shift program. However, the code for encryption and decryption for the random cipher is must simpler in comparison to the Caesar Shift cipher. The code for both the encrypt() and decrypt() methods is virtually the same, and is shown below:
encrypt():
String cipher = "";
for(int i = 0; i<message.length(); i++){
for(int j = 0; j<cipherAlphabet.length; j++){
if(message.charAt(i) == alphabet[j]){
cipher += cipherAlphabet[j];
}
}
}
return cipher;
decrypt():
String english = "";
for(int i = 0; i<cipher.length(); i++){
for(int j = 0; j<cipherAlphabet.length; j++){
if(cipher.charAt(i) == cipherAlphabet[j]){
english += alphabet[j];
}
}
}
return english;
where cipherAlphabet the alphabet generated by the createCipher() method.
Lastly, the cipherResults() method accepts a user inputted message, and displays both the encrypted message, and the message after decryption (which should be identical to the original message). An example run is shown below (user input is bolded):
Please enter a message (no punctuation): hello
Encrypted cipher: vnjjk
Original message: hello
Final Thoughts
These two projects were a more advanced version of the Morse Code converter, as they involved both encryption and decryption. Not to mention, the random cipher used a randomly generated number as the basis of the code, which is a step even higher than a basic Caesar Shift cipher. I mainly had difficulty in the manipulating the Unicode Values in the Caesar Shift project, as it took repeated testing and further research into the nature of Unicode to better understand how to implement the shift value. I am glad that both projects turned out successful in the end.
Comments