AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Java Program for Caesar Cipher Encryption

In cryptography, a Caesar cipher, also known as the shift cipher. It is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plain text is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, X would replace by A and so on. Write a JAVA program to implement Caesar cipher Algorithm. Input Specification: First line will contain an integer 1 or 2.(1 means encryption and 2 means Decryption) Second line will contain a key N. Third line will contain a string(a single word containing only Alphabets) which is either to be encrypted or decrypted based on user input on line 1. Output Specification: Contains a single line encrypted or decrypted String. Sample Input: 1 3 Ignite Sample Output. Ljqlwh Sample Input: 2 3 Ljqlwh Sample Output. Ignite


corejava x 352
programming x 168
Posted On : 2016-12-22 10:50:19.0
profile Navin Kumar - anyforum.in Navin Kumar
1000
up-rate
5
down-rate

Answers


import java.util.Scanner;

public class CaesarCipher {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int operation=input.nextInt();
int key=input.nextInt();
input.nextLine();
String inputString=input.nextLine();
if(operation==1){
System.out.println(encrypt(key,inputString));
}else if(operation==2){
System.out.println(decrypt(key, inputString));
}else{
System.out.println("Invalid input!!");
}
}

public static String encrypt(int key, String input){
StringBuilder sb=new StringBuilder();
int charCode=0;
for(int i=0;i<input.length();i++){
charCode=input.charAt(i)+key;
sb=sb.append(Character.toString((char)charCode));
charCode=0;
}
return sb.toString();
}

public static String decrypt(int key, String input){
StringBuilder sb=new StringBuilder();
int charCode=0;
for(int i=0;i<input.length();i++){
charCode=input.charAt(i)-key;
sb=sb.append(Character.toString((char)charCode));
charCode=0;
}
return sb.toString();
}

}

Posted On : 2016-12-22 23:58:49
Satisfied : 3 Yes  1 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188221961
Reply This Thread
up-rate
3
down-rate
Comments
the code is not giving the output
profile Navin Kumar - anyforum.in Navin Kumar
6  0  0
Posted On :2016-12-23 10:40:16.0
Leave a Comment
Hi Navin,
I think you are trying by giving input in single line. Please try to give the inputs as specifies in question´s description:
e.g.
Input:1
1
3
Ignite

Input:2
2
3
Ljqlwh


You´ll definitely get correct output. It´s already tested.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1882  21961
Posted On :2016-12-23 22:31:36.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in