Caesar Cipher is an encryption technique that is implemented as ROT13. It is a simple letter substitution cipher that replaces a letter with the letter 13 places after it in the alphabets, with the other characters remaining unchanged.

Watch Java videos

Caesar Cipher

Write a program to accept a plain text of length L, where L must be greater than 3 and less than 100. Encrypt the text if valid as per the Caesar Cipher. Test your program with the sample data and some random data:
Example 1
INPUT : Hello! How are you?
OUTPUT: 
Uryyb? Ubj ner lbh?
Example 2
INPUT : Encryption helps to secure data.
OUTPUT :  Rapelcgvba urycf gb frpher qngn.
Example 3
INPUT : You
OUTPUT : INVALID LENGTH

import java.io.*;
public class caeser_cipher
{
    String str,str2="";
    int len;
    void input()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter the string");
        str=br.readLine();
        len=str.length();
    }
    void compute()
    {
        char ch,ch2;
        int l,m;
        for(int i=0;i<len;i++)
        {
            ch=str.charAt(i);
            if(Character.isLetter(ch)==true)
            {l=(int)ch;
            if(l==97||l<=109||l==65||(l<=77&&l<90))
            {
                m=l+13;
                ch2=(char)m;
                str2+=ch2;
            }
            else
            {
                m=l-13;
                ch2=(char)m;
                str2+=ch2;
            }
        }
        else
        str2+=ch;
    }
}
    void display()
    {
        System.out.println("the original string was:"+str);
        System.out.println("the cipher string is:"+str2);     
    }
    public static void main()throws IOException
    {
        caeser_cipher obj=new caeser_cipher();
        obj.input();
        obj.compute();
        
        obj.display();
    }
} 

Pronic Number

LEAVE A REPLY

Please enter your comment!
Please enter your name here