JayBawankar

is ceaser

Aug 1st, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. import java.util.*;
  2. public class Caesar_Cipher {
  3.     public static void main(String[] args) {
  4.         Scanner sc=new Scanner(System.in);
  5.         System.out.println("Enter the plaintext");
  6.         String str=sc.nextLine();
  7.         str.toLowerCase();
  8.         System.out.println("Enter the key");
  9.         int key=sc.nextInt();
  10.         String ciph="";
  11.         char chr[]=str.toCharArray();
  12.         for(int i=0;i<str.length();i++){
  13.             if(chr[i]==' '){
  14.                 ciph+=" ";
  15.                 continue;
  16.             }
  17.             int t=(int)chr[i]+(key%26);
  18.            
  19.             chr[i]=(char)t;
  20.             ciph+=chr[i];
  21.         }
  22.         System.out.println("Cipher:"+ciph);
  23.         chr=ciph.toCharArray();
  24.         String deciph="";
  25.         for(int i=0;i<ciph.length();i++){
  26.              if(chr[i]==' '){
  27.                 deciph+=" ";
  28.                 continue;
  29.             }
  30.             int t=(int)chr[i]-(key%26);
  31.             chr[i]=(char)t;
  32.             deciph+=chr[i];
  33.         }
  34.         System.out.println("Plain Text:"+deciph);
  35.     }
  36. }
  37.  
  38. /*
  39. OUTPUT
  40.  
  41. Enter the plaintext
  42.  
  43. rendezvous at bravo
  44.  
  45. Enter the key
  46. 4
  47.  
  48. Cipher:    virhi~zsyw ex fvezs
  49. Plain Text :    rendezvous at bravo
  50. */
Add Comment
Please, Sign In to add comment