Guest User

Untitled

a guest
Jun 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. /*--------------------------------------------------------
  2. *   Main Program: Encrypting/Decrypting a text file
  3. *---------------------------------------------------------
  4. *   Date: 8 Febuary 2012
  5. *   @author Chris Wallace
  6. */
  7. import java.util.Scanner;
  8. import java.io.*;
  9. public class Enigma{
  10.     public static void main(String[] args){
  11.         Scanner keyboard = new Scanner(System.in);
  12.         System.out.println("Enigma Program- Main Menu");
  13.         System.out.println("\t e: Encrypt a File");    
  14.         System.out.println("\t d: Decrypt a File");    
  15.         System.out.println("\t q: Quit");
  16.         System.out.print("Please Enter A Command: ");
  17.         char command = (keyboard.next()).charAt(0);
  18.         keyboard.nextLine();
  19.         while (command != 'q'){
  20.             String fileName = getFileName(keyboard,command);
  21.             String fileOutName = getOutFileName(keyboard);
  22.             try{
  23.                 File fileobject = new File(fileName);
  24.                 FileReader inputFile = new FileReader(fileobject);
  25.                 PrintWriter outputFile = new PrintWriter(fileOutName);
  26.                 if (command == 'e'){
  27.                     encryptFile(inputFile, outputFile);
  28.                     System.out.println("Encryption Complete.");
  29.                 }
  30.                 else if (command == 'd'){
  31.                     decryptFile(inputFile, outputFile);
  32.                     System.out.println("Decryption Complete.");
  33.                 }
  34.                 inputFile.close();
  35.                 outputFile.close();
  36.             }
  37.             catch(IOException e){
  38.                 System.out.println("An Error occured during file input/output");
  39.             }
  40.             System.out.println("\n-------------------------\nEnigma Program- Main Menu");
  41.             System.out.println("\t e: Encrypt a File");    
  42.             System.out.println("\t d: Decrypt a File");    
  43.             System.out.println("\t q: Quit");
  44.             System.out.print("Please Enter A Command: ");
  45.             command = (keyboard.next()).charAt(0);
  46.             keyboard.nextLine();
  47.         }
  48.         System.out.println("\nGoodbye!");
  49.     }
  50.    
  51.     public static String getFileName(Scanner keyboard, char command){
  52.         if (command == 'e'){
  53.             System.out.print("Please enter the File Name to Encrypt: ");
  54.         }
  55.         else{
  56.             System.out.print("Please enter the File Name to Decrypt: ");
  57.         }
  58.         return keyboard.nextLine();
  59.     }
  60.  
  61.        
  62.     public static String getOutFileName(Scanner keyboard){
  63.         System.out.print("Please enter the File Name of the file to Output: ");
  64.         return keyboard.nextLine();
  65.     }
  66.  
  67.     public static char[] initRotor(int offset){
  68.         int ascii_size = 128;
  69.         char[] a = new char[ascii_size];
  70.         // add all ASCII characters to the rotor
  71.         for(int rotor_pos = 0; rotor_pos < (ascii_size); rotor_pos++)
  72.         {
  73.             a[rotor_pos] = (char)((rotor_pos + offset) % ascii_size);
  74.         }
  75.         return a;
  76.     }
  77.     public static int charIsAt(char a[], char ch){
  78.         int x = 0;
  79.         boolean cont = true;
  80.         for (int i = 0; (i < 128 && cont); i++){
  81.             if (a[i] == ch){
  82.                 cont = false;
  83.                 x = i;
  84.             }
  85.         }
  86.         return x;
  87.     }
  88.     public static void encryptFile(FileReader inputFile, PrintWriter outputFile) throws IOException{
  89.         char[] rotor1 = initRotor(0);
  90.         char[] rotor2 = initRotor(7);
  91.         char[] backplate = initRotor(13);
  92.         int rotor1Count = 0, rotor2Count = 0;
  93.         char ch = (char)inputFile.read();;
  94.         while(ch != (char)-1){
  95.             int i = 0;
  96.             //take next input char
  97.             i = charIsAt(rotor1, ch) + rotor1Count;
  98.             if (i >= 128){
  99.                 i = i - 128;
  100.             }
  101.             ch = backplate[i];
  102.             i = charIsAt(rotor2, ch) + rotor2Count;
  103.             if (i >= 128){
  104.                 i = i - 128;
  105.             }
  106.             ch = backplate[i];
  107.             //write single character
  108.             outputFile.print(ch);
  109.             ch = (char)inputFile.read();
  110.             //advance rotor
  111.             rotor1Count++;
  112.             if (rotor1Count > 127){
  113.                 rotor1Count = 0;
  114.                 rotor2Count++;
  115.                 if (rotor2Count > 127){
  116.                     rotor2Count = 0;
  117.                 }
  118.             }
  119.         }
  120.     }
  121.     public static void decryptFile(FileReader inputFile, PrintWriter outputFile) throws IOException{
  122.         char[] rotor1 = initRotor(0);
  123.         char[] rotor2 = initRotor(7);
  124.         char[] backplate = initRotor(13);
  125.         int rotor1Count = 0, rotor2Count = 0;
  126.         char ch = (char)inputFile.read();;
  127.         while(ch != (char)-1){
  128.             int i = 0;
  129.             //take next input char
  130.             i = charIsAt(backplate, ch) - rotor2Count;
  131.             if (i < 0){
  132.                 i = i + 128;
  133.             }
  134.             ch = rotor2[i];
  135.             i = charIsAt(backplate, ch) - rotor1Count;
  136.             if (i < 0){
  137.                 i = i + 128;
  138.             }
  139.             ch = rotor1[i];
  140.             //write single character
  141.             outputFile.print(ch);
  142.             ch = (char)inputFile.read();
  143.             //advance rotor
  144.             rotor1Count++;
  145.             if (rotor1Count > 127){
  146.                 rotor1Count = 0;
  147.                 rotor2Count++;
  148.                 if (rotor2Count > 127){
  149.                     rotor2Count = 0;
  150.                 }
  151.             }
  152.         }
  153.     }
  154. }
Add Comment
Please, Sign In to add comment