Caesar Cipher > Java Program
System Security
Caesar Cipher > Java Program
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<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<n;i++)
{
System.out.print(Caesar_Cipher[i]);
}
}
}
/*
Caesar Cipher program output:
Enter plain text : cipher program
Enter Caesar Cipher shift key : 2
Caesar Cipher text: : ekrjgtVrtqitco
*/
Comments
Post a Comment