Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*--------------------------------------------------------
- * Main Program: Encrypting/Decrypting a text file
- *---------------------------------------------------------
- * Date: 8 Febuary 2012
- * @author Chris Wallace
- */
- import java.util.Scanner;
- import java.io.*;
- public class Enigma{
- public static void main(String[] args){
- Scanner keyboard = new Scanner(System.in);
- System.out.println("Enigma Program- Main Menu");
- System.out.println("\t e: Encrypt a File");
- System.out.println("\t d: Decrypt a File");
- System.out.println("\t q: Quit");
- System.out.print("Please Enter A Command: ");
- char command = (keyboard.next()).charAt(0);
- keyboard.nextLine();
- while (command != 'q'){
- String fileName = getFileName(keyboard,command);
- String fileOutName = getOutFileName(keyboard);
- try{
- File fileobject = new File(fileName);
- FileReader inputFile = new FileReader(fileobject);
- PrintWriter outputFile = new PrintWriter(fileOutName);
- if (command == 'e'){
- encryptFile(inputFile, outputFile);
- System.out.println("Encryption Complete.");
- }
- else if (command == 'd'){
- decryptFile(inputFile, outputFile);
- System.out.println("Decryption Complete.");
- }
- inputFile.close();
- outputFile.close();
- }
- catch(IOException e){
- System.out.println("An Error occured during file input/output");
- }
- System.out.println("\n-------------------------\nEnigma Program- Main Menu");
- System.out.println("\t e: Encrypt a File");
- System.out.println("\t d: Decrypt a File");
- System.out.println("\t q: Quit");
- System.out.print("Please Enter A Command: ");
- command = (keyboard.next()).charAt(0);
- keyboard.nextLine();
- }
- System.out.println("\nGoodbye!");
- }
- public static String getFileName(Scanner keyboard, char command){
- if (command == 'e'){
- System.out.print("Please enter the File Name to Encrypt: ");
- }
- else{
- System.out.print("Please enter the File Name to Decrypt: ");
- }
- return keyboard.nextLine();
- }
- public static String getOutFileName(Scanner keyboard){
- System.out.print("Please enter the File Name of the file to Output: ");
- return keyboard.nextLine();
- }
- public static char[] initRotor(int offset){
- int ascii_size = 128;
- char[] a = new char[ascii_size];
- // add all ASCII characters to the rotor
- for(int rotor_pos = 0; rotor_pos < (ascii_size); rotor_pos++)
- {
- a[rotor_pos] = (char)((rotor_pos + offset) % ascii_size);
- }
- return a;
- }
- public static int charIsAt(char a[], char ch){
- int x = 0;
- boolean cont = true;
- for (int i = 0; (i < 128 && cont); i++){
- if (a[i] == ch){
- cont = false;
- x = i;
- }
- }
- return x;
- }
- public static void encryptFile(FileReader inputFile, PrintWriter outputFile) throws IOException{
- char[] rotor1 = initRotor(0);
- char[] rotor2 = initRotor(7);
- char[] backplate = initRotor(13);
- int rotor1Count = 0, rotor2Count = 0;
- char ch = (char)inputFile.read();;
- while(ch != (char)-1){
- int i = 0;
- //take next input char
- i = charIsAt(rotor1, ch) + rotor1Count;
- if (i >= 128){
- i = i - 128;
- }
- ch = backplate[i];
- i = charIsAt(rotor2, ch) + rotor2Count;
- if (i >= 128){
- i = i - 128;
- }
- ch = backplate[i];
- //write single character
- outputFile.print(ch);
- ch = (char)inputFile.read();
- //advance rotor
- rotor1Count++;
- if (rotor1Count > 127){
- rotor1Count = 0;
- rotor2Count++;
- if (rotor2Count > 127){
- rotor2Count = 0;
- }
- }
- }
- }
- public static void decryptFile(FileReader inputFile, PrintWriter outputFile) throws IOException{
- char[] rotor1 = initRotor(0);
- char[] rotor2 = initRotor(7);
- char[] backplate = initRotor(13);
- int rotor1Count = 0, rotor2Count = 0;
- char ch = (char)inputFile.read();;
- while(ch != (char)-1){
- int i = 0;
- //take next input char
- i = charIsAt(backplate, ch) - rotor2Count;
- if (i < 0){
- i = i + 128;
- }
- ch = rotor2[i];
- i = charIsAt(backplate, ch) - rotor1Count;
- if (i < 0){
- i = i + 128;
- }
- ch = rotor1[i];
- //write single character
- outputFile.print(ch);
- ch = (char)inputFile.read();
- //advance rotor
- rotor1Count++;
- if (rotor1Count > 127){
- rotor1Count = 0;
- rotor2Count++;
- if (rotor2Count > 127){
- rotor2Count = 0;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment