Implementation Of Additive/Caesar Cipher In Java


Additive cipher/ Caesar cipher is one of the most simplest encryption technique in Cryptography. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed place apart from it.

In cryptography, a Caesar cipher, also known as Caesar’s cipher, the shift cipherCaesar’s code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by Awould become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence. – Wikipedia

Additive Cipher uses following formulas

For encryption:

c = (p +k) mod 26
 

Where c = Cipher text, P = Plain Text and K = Key

For decryption:

c = (p -k) mod 26
 

Where c = Cipher text, P = Plain Text and K = Key

Here is implementation of the algorithm. You have to pass your plain text to encrypt or cipher text to decrypt. You can download the code from here.

import java.io.*;

class Additive
{
	public static void main(String args[]) throws IOException
	{
		//Getting a string
		String input;
		System.out.println("Enter a String");
		BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
		input = obj.readLine();
		int length;
		length = input.length();
		
		//Getting choice
		int ch;
		System.out.println("What do you want to perform?");
		System.out.println("1.Encryption");
		System.out.println("2.Decryption");
		ch = Integer.parseInt(obj.readLine());
		
		//Getting key
		int key;
		System.out.println("Enter key?");
		key = Integer.parseInt(obj.readLine());
		
		//Doing operation
		int temp, i;
		char curr_char;
		String output="";
		if(ch==1)
		{
			//perform encryption
			for(i=0;i<length;i++)
			{
				curr_char = input.charAt(i);
				
				if(Character.isLowerCase(curr_char))
				{
					curr_char = (char) ((int) curr_char - 97);
					output = output + (char) ((curr_char + key)%26+97);
				}
				else if(Character.isUpperCase(curr_char))
				{
					curr_char = (char) ((int) curr_char - 65);
					output = output + (char) ((curr_char + key)%26+65);
				}
				else
				{
					output = output + (char) (curr_char);
				}
				
			}
			System.out.println("Cipher text: "+output);
		}
		else if (ch==2)
		{
			//perform decryption
			for(i=0;i<length;i++)
			{
				curr_char = input.charAt(i);
				
				if(Character.isLowerCase(curr_char))
				{
					curr_char = (char) ((int) curr_char - 97);
					
					if(curr_char - key < 0)
					{
						curr_char += 26;
					}
					
					output = output + (char) ((curr_char - key)%26+97);
				}
				else if(Character.isUpperCase(curr_char))
				{
					curr_char = (char) ((int) curr_char - 65);
					
					if(curr_char - key < 0)
					{
						curr_char += 26;
					}
					
					output = output + (char) ((curr_char - key)%26+65);
				}
				else
				{
					output = output + (char) (curr_char);
				}
				
			}
			System.out.println("Plain text: "+output);
		}
		else
		{
			System.out.println("Invalid Choice");
		}
		
	}
}