Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Caesar_Cipher {
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- System.out.println("Enter the plaintext");
- String str=sc.nextLine();
- str.toLowerCase();
- System.out.println("Enter the key");
- int key=sc.nextInt();
- String ciph="";
- char chr[]=str.toCharArray();
- for(int i=0;i<str.length();i++){
- if(chr[i]==' '){
- ciph+=" ";
- continue;
- }
- int t=(int)chr[i]+(key%26);
- chr[i]=(char)t;
- ciph+=chr[i];
- }
- System.out.println("Cipher:"+ciph);
- chr=ciph.toCharArray();
- String deciph="";
- for(int i=0;i<ciph.length();i++){
- if(chr[i]==' '){
- deciph+=" ";
- continue;
- }
- int t=(int)chr[i]-(key%26);
- chr[i]=(char)t;
- deciph+=chr[i];
- }
- System.out.println("Plain Text:"+deciph);
- }
- }
- /*
- OUTPUT
- Enter the plaintext
- rendezvous at bravo
- Enter the key
- 4
- Cipher: virhi~zsyw ex fvezs
- Plain Text : rendezvous at bravo
- */
Add Comment
Please, Sign In to add comment