Sunday, March 22, 2020

BlueJ Program On Caesar Cipher



Caesar Cipher is an encryption technique which is implemented as ROT13 (‘rotate by 13 places’). 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. 

Example 1 

INPUT : Hello! How are you? 

OUTPUT : The cipher text is: Uryyb? Ubj ner lbh? 

Example 2 

INPUT : Encryption helps to secure data.

OUTPUT : The cipher text is: Rapelcgvba urycf gb frpher qngn. 

Example 3

INPUT : You

OUTPUT : INVALID LENGTH


import java.io.*; 
class Pat 
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
String s,str=""; 
int a; 
char ch;

void takeString() throws Exception 
{ 
System.out.print("\nEnter the sentence:"); 
s=br.readLine().trim();
 if(s.length() >100) 
{
System.out.print("\nInvalid Length of String:");
 return; 
}
for(int i = 0; i< s.length();i++) 
{
ch = s.charAt(i); 
if(Character.isLetter(ch)) 
{
a = ch + 13;
if((Character.toUpperCase(ch) > 90) ||(Character.toLowerCase(ch) > 122)) 
{ 
a = a - 26; 
} 
ch = (char)a; 
} 
str = str + ch;
}
 System.out.println("The cipher text is :"+str); 
} 
public static void main(String args[]) throws Exception 
{ 
Pat ob = new Pat(); ob.takeString();

} 

}

Back To 2017 ISC Practical Paper: Click Here

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner