caesar cipher Program in java

In cryptography Caesar cipher is one of the simple and most widely used Encryption algorithm.Caesar cipher is special case of shift cipher.Caesar cipher is substitution cipher where letter in plain text is replaced by some other letter.If shift is 3 then A letter is replaced by D,B is replaced by E and so on.


import java.io. * ;
import java.lang. * ;
class CaesarCipher {
	public static void main(String args[]) throws IOException {
		String plaintext;
		int n;
		int z;
		int ascii;
		DataInputStream in =new DataInputStream(System. in );
		System.out.print("\nEnter plain text\t");
		plaintext = in.readLine();
		char ptext[] = plaintext.toCharArray();
		n = plaintext.length();
		char Caesar_Cipher[] = new char[n];
		System.out.print("\n\nEnter Caesar Cipher shift key\t\t");
		z = Integer.parseInt( in .readLine());
		for (int i = 0; i & lt; n; i++) {
			ascii = (int) ptext[i];
			ascii = ascii - 97;
			ascii = ((ascii + z) % 26);
			ascii = ascii + 97;
			Caesar_Cipher[i] = (char) ascii;
		}
		System.out.print("\n\nCaesar Cipher text:\t\t");
		for (int i = 0; i & lt; n; i++) {
			System.out.print(Caesar_Cipher[i]);
		}
	}
}

/*

caesar cipher in java output:

Enter plain text : cipher program

Enter Caesar Cipher shift key : 2

Caesar Cipher text: : ekrjgtVrtqitco

*/

You may also like...

Leave a Reply

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