m2skills

ceasar cipher JAVA

Aug 13th, 2017
1,639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 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 caesarCipher {
  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 encrypted : ");
  19.         //String inString = sc.nextLine();
  20.  
  21.         System.out.print("\nEnter the Shift value : ");
  22.         int shift_value = sc.nextInt();
  23.  
  24.         // String finalString = encrypt_(inString,cipher);
  25.         encrypt_file(shift_value);
  26.         // System.out.print("\nThe encrypted String is : " + finalString);
  27.     }
  28.  
  29.     static String encrypt(String original_msg, int shift_value){
  30.  
  31.         String allChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
  32.         int all_chars_length = allChars.length();
  33.         String encrypted_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.                 position %= all_chars_length;
  40.                 Character e_letter = allChars.charAt(position);
  41.                 encrypted_msg += e_letter;
  42.  
  43.             }else{
  44.                 encrypted_msg += letter;
  45.             }
  46.         }
  47.  
  48.  
  49.         return encrypted_msg;
  50.     }
  51.  
  52.     static void encrypt_file(int shift_value){
  53.         FileInputStream fin;
  54.         Scanner sc = new Scanner(System.in);
  55.  
  56.         System.out.print("Enter the path of the file : ");
  57.         String file_name = sc.nextLine();
  58.         String fullFile = "";
  59.  
  60.         try {
  61.             fin = new FileInputStream(file_name);
  62.             Scanner fp = new Scanner(fin);
  63.             int size = 0;
  64.  
  65.             while (fp.hasNextLine()) {
  66.                 String line = fp.nextLine();
  67.                 fullFile += line + "\n";
  68.                 size += line.length();
  69.             }
  70.             System.out.println("File read successfully!!");
  71.             fullFile = encrypt(fullFile, shift_value);
  72.             System.out.println("Data encrypted successfully!!");
  73.             System.out.println(fullFile);
  74.         }catch (Exception e){
  75.             e.printStackTrace();
  76.         }
  77.  
  78.  
  79.         try {
  80.  
  81.             File newTextFile = new File("C:\\Users\\MOHIT\\Desktop\\encrypted_file.txt");
  82.             FileWriter f1 = new FileWriter(newTextFile);
  83.             f1.write(fullFile);
  84.             f1.close();
  85.             System.out.println("Encrypted file stored on Desktop");
  86.         } catch (IOException ex) {
  87.             ex.printStackTrace();
  88.         }
  89.     }
  90.  
  91.  
  92. }
  93.  
  94. /*
  95. Output for encrypting a file
  96.  
  97.  
  98. Enter File name :F:/python_3.4/sample.txt
  99. Enter File name : F:/python_3.4/sample.txt
  100. File read successufully!!
  101. File data encrypted successfully!!
  102. Encrypted file stored on Desktop!!
  103.  
  104.  
  105.  */
Add Comment
Please, Sign In to add comment