m2skills

vignere cipher java

Sep 3rd, 2017
2,767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. /**
  2.  * Created by MOHIT on 27-08-2017.
  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 vignereCipher {
  14.  
  15.     public static void main(String arg[]){
  16.         Scanner sc = new Scanner(System.in);
  17.  
  18.  
  19.         System.out.print("Enter the Encryption key : ");
  20.         String key = sc.nextLine();
  21.         encrypt_file(key);
  22.  
  23.  
  24.         //System.out.print("Enter the String to be encrypted : ");
  25.         //String inString = sc.nextLine();
  26.         //String finalString = encrypt(inString, key);
  27.         //System.out.print("\nThe encrypted String is : " + finalString);
  28.     }
  29.  
  30.     // method that removes all the numbers, 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.     static String encrypt(String original_msg, String key){
  37.  
  38.         // allchars is our keyspace
  39.         String allChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  40.         int all_chars_length = allChars.length();
  41.         String encrypted_msg = "";
  42.  
  43.         // converting our string to uppercase
  44.         original_msg = original_msg.toUpperCase();
  45.        
  46.         // removing all numbers, punctuations
  47.         original_msg = remove_ichars(original_msg);
  48.        
  49.         // converting key to uppercase
  50.         key = key.toUpperCase();
  51.        
  52.         int index = 0;
  53.         // iterating over the original message character by chracter and decrypting it and appending it to the new string
  54.         for(int i=0; i< original_msg.length(); i++){
  55.             Character letter = original_msg.charAt(i);
  56.             if(allChars.indexOf(letter) >= 0) {
  57.                 int position = (allChars.indexOf(letter) + allChars.indexOf(key.charAt(index))) % all_chars_length;
  58.                 Character e_letter = allChars.charAt(position);
  59.                 encrypted_msg += e_letter;
  60.                 index++;
  61.                 if(index >= key.length()){
  62.                     index %= key.length();
  63.                 }
  64.             }else{
  65.                 encrypted_msg += letter;
  66.                 index = 0;
  67.             }
  68.         }
  69.  
  70.         return encrypted_msg;
  71.     }
  72.  
  73.  
  74.     static void encrypt_file(String key){
  75.         FileInputStream fin;
  76.         Scanner sc = new Scanner(System.in);
  77.  
  78.         System.out.print("Enter the path of the file : ");
  79.         String file_name = sc.nextLine();
  80.         String fullFile = "";
  81.  
  82.         try {
  83.             fin = new FileInputStream(file_name);
  84.             Scanner fp = new Scanner(fin);
  85.             int size = 0;
  86.  
  87.             while (fp.hasNextLine()) {
  88.                 String line = fp.nextLine();
  89.                 fullFile += line + "\n";
  90.                 size += line.length();
  91.             }
  92.             System.out.println("File read successfully!!");
  93.             fullFile = encrypt(fullFile, key);
  94.             System.out.println("Data encrypted successfully!!");
  95.             System.out.println(fullFile);
  96.         }catch (Exception e){
  97.             e.printStackTrace();
  98.         }
  99.  
  100.  
  101.         try {
  102.  
  103.             File newTextFile = new File("C:\\Users\\MOHIT\\Desktop\\encrypted_file.txt");
  104.             FileWriter f1 = new FileWriter(newTextFile);
  105.             f1.write(fullFile);
  106.             f1.close();
  107.             System.out.println("Encrypted file stored on Desktop");
  108.         } catch (IOException ex) {
  109.             ex.printStackTrace();
  110.         }
  111.     }
  112.  
  113.  
  114. }
  115.  
  116. /*
  117. Output for encrypting a file
  118.  
  119.  
  120. Enter File name :F:/python_3.4/sample.txt
  121. Enter File name : F:/python_3.4/sample.txt
  122. File read successufully!!
  123. File data encrypted successfully!!
  124. Encrypted file stored on Desktop!!
  125.  
  126.  
  127.  */
Add Comment
Please, Sign In to add comment