m2skills

CAESAR JAVA DEC

Aug 24th, 2017
1,718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. /**
  2.  * Created by MOHIT on 07-11-2016.
  3.  */
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.util.ArrayList;
  9. import java.util.Scanner;
  10. import java.util.Random;
  11. import java.io.FileWriter;
  12.  
  13. public class ceasarDecipher {
  14.  
  15.     public static void main(String arg[]){
  16.         Scanner sc = new Scanner(System.in);
  17.  
  18.         //System.out.print("Enter the String to be decrypted : ");
  19.         //String inString = sc.nextLine();
  20.  
  21.         System.out.print("\nEnter the Shift value : ");
  22.         int shift_value = sc.nextInt();
  23.  
  24.         // String finalString = decrypt_(inString,cipher);
  25.         decrypt_file(shift_value);
  26.         // System.out.print("\nThe decrypted String is : " + finalString);
  27.     }
  28.  
  29.     static String decrypt(String original_msg, int shift_value){
  30.  
  31.         String allChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
  32.         int all_chars_length = allChars.length();
  33.         String decrypted_msg = "";
  34.         for(int i=0; i< original_msg.length(); i++){
  35.             Character letter = original_msg.charAt(i);
  36.             if(allChars.indexOf(letter) >= 0) {
  37.                 int position = allChars.indexOf(letter);
  38.                 position -= shift_value;
  39.                 if(position < 0){
  40.                     position = all_chars_length + position;
  41.                 }
  42.                 position %= all_chars_length;
  43.                 Character e_letter = allChars.charAt(position);
  44.                 decrypted_msg += e_letter;
  45.  
  46.             }else{
  47.                 decrypted_msg += letter;
  48.             }
  49.         }
  50.  
  51.  
  52.         return decrypted_msg;
  53.     }
  54.  
  55.     static void decrypt_file(int shift_value){
  56.         FileInputStream fin;
  57.         Scanner sc = new Scanner(System.in);
  58.  
  59.         System.out.print("Enter the path of the file : ");
  60.         String file_name = sc.nextLine();
  61.         String fullFile = "";
  62.  
  63.         try {
  64.             fin = new FileInputStream(file_name);
  65.             Scanner fp = new Scanner(fin);
  66.             int size = 0;
  67.  
  68.             while (fp.hasNextLine()) {
  69.                 String line = fp.nextLine();
  70.                 fullFile += line + "\n";
  71.                 size += line.length();
  72.             }
  73.             System.out.println("File read successfully!!");
  74.             fullFile = decrypt(fullFile, shift_value);
  75.             System.out.println("Data decrypted successfully!!");
  76.             System.out.println(fullFile);
  77.         }catch (Exception e){
  78.             e.printStackTrace();
  79.         }
  80.  
  81.  
  82.         try {
  83.  
  84.             File newTextFile = new File("C:\\Users\\MOHIT\\Desktop\\decrypted_file.txt");
  85.             FileWriter f1 = new FileWriter(newTextFile);
  86.             f1.write(fullFile);
  87.             f1.close();
  88.             System.out.println("decrypted file stored on Desktop");
  89.         } catch (IOException ex) {
  90.             ex.printStackTrace();
  91.         }
  92.     }
  93.  
  94.  
  95. }
  96.  
  97. /*
  98. Output for decrypting a file
  99.  
  100. Enter the Shift value : 3
  101. Enter File name :F:/python_3.4/sample.txt
  102. File read successufully!!
  103. File data decrypted successfully!!
  104. decrypted file stored on Desktop!!
  105.  
  106.  
  107.  */
Add Comment
Please, Sign In to add comment