Plain Text : ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher Text : HETJWOPSQYAZIVMBDUKNRGLCXF

Turns the plain text "BOY" into the cipher text "EMX". Note the substitution is a permutation of the 26 letters of the alphabet, so we can uniquely decipher a cipher text by reading the substitution backwards; that is, cipher text "A" is plain text "k".

You are required to:
1. Write an algorithm in pseudo code to solve the above problem.
2. Determine the time complexity class of the above algorithm.
3. Using any programming language of you choice convert the above algorithm into a program that takes as input a plain text word file of not less than 2 mb, generate a random substitution, and then encrypts the plain text using that substitution.
4. Write a decrypt program that will decrypt the cipher text into a plain text.
5. By using the system clock analyze the performance of the program in (3) above with that of the algorithm in (2) above.

Specific requirements:
Menu driven and loops until i chose to exit and number 3 & 4 are option.

Example:

1 Encrypts
2 Decrypts
3 Exit

import java.util.*;
import java.io.*;
//import java.lang.*;

class Encrypt
{
        public static void main (String [] args) throws FileNotFoundException
        {
                Scanner input = new Scanner(System.in);

                //generates integer to be used as the shift
                Random rand = new Random();
                int shift = rand.nextInt(26);
                System.out.println(shift);


                //multiplies the randomly generated shift by 2 then adds 10 to it.
                //this way the actual shift value is not written to the key file
                int key = shift*2+10;

                String shiftnumber = Integer.toString(key);// converts integer to string

                //creates key file and stores "shift value" in it
            try{
                FileWriter fstream = new FileWriter("key.txt");
                BufferedWriter out = new BufferedWriter(fstream);
                out.write(shiftnumber);
                out.close();
                }catch (Exception e){//Catch exception if any
                  System.err.println("Error: " + e.getMessage());
                }

            //the following prompts user for relevant inputs
            System.out.println("What is the input file name?"); // input name of file
            String file = input.next();
            System.out.println("What is the output file name?"); // input new file name
            String out = input.next();

            System.out.println(encrypter (file, shift, out));  // calls encryption method

        }

        public static String encrypter (String input, int shift1, String out) throws FileNotFoundException
        {
                File newfile = new File(out);
                PrintStream encoded = new PrintStream(newfile);  // creates new file
                File reader = new File (input);  // creates file to scan
                Scanner in = new Scanner(reader); // creates scanner
                while (in.hasNextLine())  // runs while the file has another line
                {
                        String word = in.nextLine();            // gets next line of file
                        String cipher = word.toUpperCase();     // makes all letters uppercase
                        String encipher = "";                   // String to be written into new file
                        int loop;
                        for (loop = 0; loop < cipher.length(); loop++) // loops through the line to check each character
                        {
                                char curr = cipher.charAt(loop);  // current character
                                char newChar = crypt(curr,shift1);  // runs through crypt method (Below)
                                encipher = encipher + newChar; // adds the new character to outgoing string
                        }
                        encoded.println(encipher);
                }
                encoded.close();
                return "DONE";
        }


         public static char crypt(char c, int shift)
        {
                char encrypted = c;  // character passed in from for loop
                int alter = shift; // shift passed in
                if (Character.isLetter(encrypted)) // checks for only letters
              {
           int enc;
       if ((int) encrypted + alter > 91) // if ASCII value after altering is beyond 'Z' value, run this to loop back to 'A'
                        {
                                enc = encrypted - 65;
                                enc += alter;
                                enc = enc % 26;
                                enc += 65;
                                encrypted = (char) enc;
                        }
                        else enc =  encrypted + alter;
                        encrypted = (char) enc;
                }
                return encrypted;
        }
}
import java.util.*;
import java.io.*;
import java.lang.*;

      class Decrypt
       {
        public static void main (String [] args) throws FileNotFoundException
        {
            Scanner input = new Scanner(System.in);

            System.out.println("What is the input file name?"); // input name of file
            String file = input.next();
            System.out.println("What is the output file name?"); // input new file name
            String out = input.next();

            System.out.println("Enter the digits found in the public key file");
            int key = input.nextInt();
            int num1 = key-10; //Decrypt
            int num2 = num1/2;
            int shift = num2-num1;

            //int shift=(key/2)-10;

            System.out.println(shift);

            System.out.println(decrypter (file, shift, out));  // calls cipher method
        }


        public static String decrypter (String input, int shift1, String out) throws FileNotFoundException
        {
                PrintStream decoded = new PrintStream(out);
                File read = new File (input);
                Scanner in = new Scanner(read);
                while (in.hasNextLine())
                {
                        String word = in.nextLine();
                        word = word.toUpperCase();
                        String cipher = word;
                        String decipher = "";
                        int loop;
                        for (loop = 0; loop < cipher.length(); loop++)
                        {
                                char curr = cipher.charAt(loop);
                                char newChar = decrypt(curr, shift1);
                                decipher = decipher + newChar;
                        }
                        decoded.println(decipher);
                }
                decoded.close();
                return "DONE";
        }

        public static char crypt(char c, int shift1)
        {
                char encrypted = c;  // character passed in from for loop
                int alter = shift1; // shift passed in
                if (Character.isLetter(encrypted)) // checks for only letters
              {
           int enc;
       if ((int) encrypted + alter > 91) // if ASCII value after altering is beyond 'Z' value, run this to loop back to 'A'
                        {
                                enc = encrypted - 65;
                                enc += alter;
                                enc = enc % 26;
                                enc += 65;
                                encrypted = (char) enc;
                        }
                        else enc =  encrypted + alter;
                        encrypted = (char) enc;
                }
                return encrypted;
        }

        public static char decrypt(char c, int shift)
        {
                char decrypted = c;
                int alter = shift;
                if (Character.isLetter(decrypted))
                {
                        int dec;
      if ((int) decrypted - alter < 65)  // if ASCII value after altering is below 'A' value, run this to correct overflow
                        {
                                dec = decrypted + 65;
                                dec -= alter;
                                dec = dec % 26;
                                dec -= 65;
                                decrypted = (char) dec;
                        }
                        else dec = decrypted - alter;
                        decrypted = (char) dec;
                }
                return decrypted;
        }
}

Well its an assignment and i posted what i have done so far.
One program encrypts and the next decrypts. Decrypting doesnt works

Edited 4 Years Ago by Nickquane: was vague

Try testing the code using a very simple input of "ABCDE"

Look at the encrypted file and see if it was correctly encrypted.
Then decrypt it and see what the output is.
This simple test should allow you to easily see the problem.
If you need help with this, post the input String, the encrypted String and the decrypted String.

@ normR1 it worked for "ABCDE" but doesn't for the a file with words.

That's good. Now try for all the letters A-Z

doesn't for the a file with words.

What letters does it fail on?

ABCDEFGHIJKLMNOPQRSTUVWXYZ

ok when i encrypt i get this "Z[BCDEFGHIJKLMNOPQRSTUVWXY
"

when i decrypt i get this "o[WXYZ[]^_`abcdefghijklmn
"

What was the value of the key for that test?

A suggestion for testing:
Create a testing class with a main() method that sets the values for the files and the key
and calls the encryptor() and decryptor() methods. That will make testing much easier and remove the changing of the key's value. Then when you find what letters are not being correctly enc/dec you can make a file with just those letters and debug the code by adding lots of println statements to show what the code is doing.

Edited 4 Years Ago by NormR1

the value was 60 and im lost to what you jus said :(
can you guide me step by step please?

Create a testing class with a main() method that sets the values for the files and the key
and calls the encryptor() and decryptor() methods.

What part don't you understand? Since the methods are static you can call them without creating an instance of the classes. For example:

Decrypt.decrypter (InFileName, key, OutFileName);

Define the 3 variables and give them values in the main() method of the testing class.

Edited 4 Years Ago by NormR1

I don't think there is an easier way to test the two methods than calling the two methods from the main() method of a special testing class. It saves the tester from having to run two separate programs and answer prompts from each program and allows the setting of the key's value that is being used to encrypt and decrypt the file.

Which part of the following are you having problems with?
1)Create a testing class
2)with a main() method
3)that sets the values for the files and the key: three Strings with file names and an int
4)and calls the encryptor() metyhod
5)and calls the decryptor() method: Decrypt.decrypter (InFileName, key, OutFileName);

Edited 4 Years Ago by NormR1

This article has been dead for over six months. Start a new discussion instead.