import java.io.*;
class caesercipher
{
String str; int key;
public void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A STRING");
str=br.readLine();
System.out.println("ENTER A SHIFT");
key=Integer.parseInt(br.readLine());
String encrypted=encrypt(str,key);
System.out.println(encrypted);
String decrypted=decrypt(encrypted,key);
System.out.println(decrypted);
}
public String encrypt(String str,int key)
{
String encrypted="";
int i;
for(i=0;i<str.length();i++)
{
int c=str.charAt(i);
if(Character.isUpperCase(c))
{
c+=(key%26);
if(c>'Z')
c=c-26;
}
else if(Character.isLowerCase(c))
{
c=c+(key%26);
if(c<'z')
c=c-26;
}
encrypted+=(char)c;
}return encrypted;}
public String decrypt(String str,int key)
{
String decrypted="";
int i;
for(i=0;i<str.length();i++)
{
int c=str.charAt(i);
if(Character.isUpperCase(c))
{
c=c-(key%26);
if(c<'A')
c+=26;
}
else if(Character.isLowerCase(c))
{
c=c-(key%26);
if(c<'a')
c+=26;
}
decrypted+=(char)c;
}
return decrypted;
}
}
for input
str=i am boy
key=3
output is
r jv kxi
o gs hui
but this doesnot satisfy caeser cipher pls help