m2skills

vignere decipher java

Sep 3rd, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. // program to implement vignere cipher decryption
  2. /**
  3.  * Created by MOHIT on 28-09-2017.
  4.  */
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11. import java.util.Random;
  12. import java.io.FileWriter;
  13.  
  14. public class vignereDecipher {
  15.  
  16.     public static void main(String arg[]){
  17.         Scanner sc = new Scanner(System.in);
  18.  
  19.         //System.out.print("Enter the String to be decrypted : ");
  20.         //String inString = sc.nextLine();
  21.  
  22.         System.out.print("\nEnter the Decription key : ");
  23.         String key = sc.nextLine();
  24.  
  25.         // String finalString = decrypt_(inString,cipher);
  26.         decrypt_file(key);
  27.         // System.out.print("\nThe decrypted String is : " + finalString);
  28.     }
  29.  
  30.     // method that removes all the numbers and punctuations
  31.     static String remove_ichars(String original_msg){
  32.         original_msg = original_msg.replaceAll("[^A-Z\n\r]", "");
  33.         return original_msg;
  34.     }
  35.  
  36.     // method that accepts encrypted string and returns decrypted string
  37.     static String decrypt(String original_msg, String key){
  38.  
  39. // allchars is our keyspace
  40.         String allChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  41.         int all_chars_length = allChars.length();
  42.         String decrypted_msg = "";
  43.        
  44.         // converting our string to uppercase
  45.         original_msg = original_msg.toUpperCase();
  46.         // removing all numbers, punctuations
  47.         original_msg = remove_ichars(original_msg);
  48.         // converting key to uppercase
  49.         key = key.toUpperCase();
  50.         int index = 0;
  51.  
  52.         // iterating over the original message character by character and decrypting it and appending it to the new string
  53.         for(int i=0; i< original_msg.length(); i++){
  54.             Character letter = original_msg.charAt(i);
  55.             if(allChars.indexOf(letter) >= 0) {
  56.                 int position = (allChars.indexOf(letter) - allChars.indexOf(key.charAt(index)) + 26) % all_chars_length;
  57.                 Character e_letter = allChars.charAt(position);
  58.                 decrypted_msg += e_letter;
  59.                 index++;
  60.                 // reset index if it is greater that key length
  61.                 if(index >= key.length()){
  62.                     index %= key.length();
  63.                 }
  64.             // case when \n or \r is encountered
  65.             }else{
  66.                 decrypted_msg += letter;
  67.                 index = 0;
  68.             }
  69.         }
  70.  
  71.         return decrypted_msg;
  72.     }
  73.    
  74.     // method that decrypts file
  75.     static void decrypt_file(String key){
  76.         FileInputStream fin;
  77.         Scanner sc = new Scanner(System.in);
  78.  
  79.         System.out.print("Enter the path of the file : ");
  80.         String file_name = sc.nextLine();
  81.         String fullFile = "";
  82.  
  83.         try {
  84.             fin = new FileInputStream(file_name);
  85.             Scanner fp = new Scanner(fin);
  86.             int size = 0;
  87.  
  88.             while (fp.hasNextLine()) {
  89.                 String line = fp.nextLine();
  90.                 fullFile += line + "\n";
  91.                 size += line.length();
  92.             }
  93.             System.out.println(fullFile);
  94.             System.out.println("File read successfully!!");
  95.             fullFile = decrypt(fullFile, key);
  96.             System.out.println("Data decrypted successfully!!");
  97.             System.out.println(fullFile);
  98.         }catch (Exception e){
  99.             e.printStackTrace();
  100.         }
  101.  
  102.  
  103.         try {
  104.  
  105.             File newTextFile = new File("C:\\Users\\MOHIT\\Desktop\\decrypted_file.txt");
  106.             FileWriter f1 = new FileWriter(newTextFile);
  107.             f1.write(fullFile);
  108.             f1.close();
  109.             System.out.println("decrypted file stored on Desktop");
  110.         } catch (IOException ex) {
  111.             ex.printStackTrace();
  112.         }
  113.     }
  114.  
  115.  
  116. }
  117.  
  118. /*
  119. Output for decrypting a file
  120.  
  121.  
  122. Enter File name :F:/python_3.4/sample.txt
  123. Enter File name : F:/python_3.4/sample.txt
  124. File read successufully!!
  125. File data decrypted successfully!!
  126. decrypted file stored on Desktop!!
  127.  
  128.  
  129.  */
Add Comment
Please, Sign In to add comment