0

Basically I need to read text from a file which the user specifies and then encrypt it by turning it into ASCII values. (I know its a weak encryption thats the task though). The encryption is working and the file that the user wants to create to store the encrypted value in is working but I don't know why it's not reading the text in the file and encrypting it. As a result the file to store the encrypted text is empty.

I think the problem in the code is somewhere in between FileWriter writer = null;

and

e.printStackTrace(); }

in the code... but I'm not sure where it is. When it works it should read the contents of the file, "fileUse", store the contents as a String and then use the keywords to encrypt it.It should then store the encrypted value in the newly created file which the user has specified which is fileWriteto

    import java.util.*;
    import java.io.*; 
    import java.io.FileReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
        public class finalfinal4
        { 
            public static void main(String [] args) {// Method Created to handle exceptions when entering encrypt, decrypt, bye etc
                System.out.println(""); // Nice Formatting
                System.out.println("This is a programme that replicates the mechanics of keyword based encryption/decryption machine and encrypts or decrypts a message accordingly"); // Message Informing User what This Programme Does
                System.out.println("");
                start(); // Allows the Whole Process to Move on by going to the next and final method
            }

    public static void start() {
        Scanner input = new Scanner (System.in); // takes in User Input
        System.out.println();
        System.out.println("Please type in 'encrypt' if you want to encrypt a message"); // Tells the user what to do
        System.out.println();
        System.out.println("'decrypt' if you want to 'decrypt' a message");
        System.out.println();
        System.out.println("'bye' if you want to exit the programme");
        System.out.println();
        String firstchoice = input.nextLine().toLowerCase(); // turns all of what the user entered into lower case
        System.out.println();


        //int line = ' ';
        switch(firstchoice) // case statement
        { case "encrypt":
            //System.out.println(" Please enter the string you wish to encrypt"); // what it does if case encrypt is activate
            break;

            case "decrypt":
            //System.out.println("Please enter the string you wish to decrypt");
            break;

            case "bye":

            System.out.println(" Thank you for using this programme ");
            System.exit(0);

            default: // the default set of actions that will occur if none of the other cases have been met

            System.out.println("");
            System.out.println("You did not type in the required messages please try again");
            System.out.println("");

            start();

        }


      FileWriter writer = null;
      String rm ="";
        try {
            System.out.println("Enter the file you want to use");
            String fileUse = input.nextLine();
            System.out.println("Enter the file you want to write to");
            String fileWriteto = input.nextLine();
            System.out.println();
            //PrintWriter writer = new PrintWriter(fileWriteTo + ".txt");
            File file = new File(fileWriteto + ".txt");
            file.createNewFile();
            FileReader read = new FileReader(fileUse + ".txt");
            BufferedReader bufreader = new BufferedReader(read);
            writer = new FileWriter(fileWriteto + ".txt");
            /*FileReader freader = new FileReader(fileUse + ".txt");
            BufferedReader breader = new BufferedReader(freader);*/
            String line;
            while ((line = bufreader.readLine()) != null) {
                rm = line;
            }
            //read.close();
            //br.close();
        }catch (IOException e) {
            //found on stack overflow http://stackoverflow.com/questions/19871955/java-io-filenotfoundexception-the-system-cannot-find-the-file-specified
            //found whole example on http://www.codejava.net/java-se/file-io/how-to-read-and-write-text-file-in-java
            e.printStackTrace();
        } 


        System.out.println("");
        System.out.println("Enter the message that you wish to " + firstchoice); // Efficient way of prompting the user
        System.out.println("");
        boolean keywordcheck = false;
        boolean keywordcheck2 = false;// sets a boolean up for use
        String keyword = "";
        String keyword2 = "";// declares variables outside the for loop so that they can be used outside the for loop
        int keySize = keyword.length();
        int keySize2 = keyword.length();// creates a variable that is the length of the keyword 
        while(keywordcheck == false)
        {
            int kc = 0; 
            System.out.println(" Please Enter Your Keyword. The Keyword should only contain text"); //prompt
            System.out.println();
            keyword = input.nextLine(); //scanner to take in the keyword
            System.out.println();
            for (int numcheck = 0; numcheck < keyword.length(); numcheck++)
            {
                if (Character.isLetter(keyword.charAt(numcheck))) // the function of the following if loop is to prevent the keyword from being anything other than letters or characters
                {    
                    kc = kc + 1; //if the character in the keyword is a letter, add 1
                }

                else if (Character.isDigit(keyword.charAt(numcheck)))
                {
                    kc = kc + 1; // the sam but for numbers
                }
                else
                {
                    // if neither a letter or a number, do nothing
                }
            }
            if (kc == keyword.length()) //if all the characters in the keyword were numbers or letter than kc will equal the length of the keyword and the code wil get out of the for loop
            {
                keywordcheck = true;
            }
            else
            {
                System.out.println("You typed in numbers, special characters or entered a space in your keyword Please only enter text without spaces");
                System.out.println();

                // if the haven't entered only numbers and letters than it will force them to re-enter the keyword
            }
        }

        while(keywordcheck2 == false)
        {
            int kc2 = 0; 
            System.out.println(" Please Enter Your Keyword. The Keyword should only contain text"); //prompt
            System.out.println();
            keyword2 = input.nextLine(); //scanner to take in the keyword
            System.out.println();
            for (int numcheck2 = 0; numcheck2 < keyword2.length(); numcheck2++)
            {
                if (Character.isLetter(keyword2.charAt(numcheck2))) // the function of the following if loop is to prevent the keyword from being anything other than letters or characters
                {    
                    kc2 = kc2 + 1; //if the character in the keyword is a letter, add 1
                }

                else if (Character.isDigit(keyword2.charAt(numcheck2)))
                {
                    kc2 = kc2 + 1; // the same but for numbers
                }
                else
                {
                    // if neither a letter or a number, do nothing
                }
            }
            if (kc2 == keyword2.length()) //if all the characters in the keyword were numbers or letter than kc will equal the length of the keyword and the code wil get out of the for loop
            {
                keywordcheck2 = true;
            }
            else
            {
                System.out.println("You typed in numbers, special characters or entered a space in your keyword Please only enter text without spaces");
                System.out.println();

                // if the haven't entered only numbers and letters than it will force them to re-enter the keyword
            }
        }
        String out1 = "";
        char space = ' ';
        //int encryption = 0;    
        for (int counter5 = 0, j = 0; counter5 < rm.length(); counter5++, j++)
        {
            keyword = keyword + keyword.charAt(counter5); // loops it over the keyword
        }
        for (int counter6 = 0, j = 0; counter6 < rm.length(); counter6++, j++)
        {
            keyword2 = keyword2 + keyword2.charAt(counter6); // loops it over the keyword
        }

        if (firstchoice.equals("encrypt"))
        {
            System.out.print("");
            System.out.println("Encrypted - ");
        }
        else
        {
            System.out.print(" Decrypted - ");

        }
        int ascimessage = ' ';
        int encrypted = ' ';

        for (int t = 0, y = 0, z = 0; t < rm.length(); t++, y++, z++) {
            ascimessage = rm.charAt(t);
            int ascikeyword = keyword.charAt(y);
            int ascikeyword2 = keyword2.charAt(z);
            if (Character.isUpperCase(ascikeyword)) //char values are interchangeable with ascii values

            {

                ascikeyword = ascikeyword - 64;

            }

            else if (Character.isLowerCase(ascikeyword))

            {

                ascikeyword = ascikeyword - 96;

            }

            else if(Character.isDigit(ascikeyword))
            {
                ascikeyword = ascikeyword - 48;
            }

            else
            {
            }
            if (Character.isUpperCase(ascikeyword2)) //char values are interchangeable with ascii values

            {

                ascikeyword2 = ascikeyword2 - 64;

            }

            else if (Character.isLowerCase(ascikeyword2))

            {

                ascikeyword2 = ascikeyword2 - 96;

            }

            else if(Character.isDigit(ascikeyword2))
            {
                ascikeyword2 = ascikeyword2 - 48;
            }

            else
            {
            }
            //file writer file reader
            if (firstchoice.equals("encrypt")){
            encrypted = ascimessage + ascikeyword + ascikeyword2;
            }
            else {encrypted = ascimessage - ascikeyword - ascikeyword2;
            }
            if (encrypted >= 122)
            {
                encrypted = encrypted - 90;
            }
            if (encrypted <= 32)
            {
                encrypted = encrypted + 90;
            }
            System.out.print((char)encrypted);

            try {
                writer.write(encrypted);//for write up wasn';t working so i did
            }catch(IOException e){
                e.printStackTrace();
            }
            //out1 = out1 + ((char)encrypted);
        } 

        System.out.println("");
        System.out.println("");
        start();

    }
}
E. Sharp
  • 1
  • 3
  • 1
    Reader/Writers are for text not binary. You can't write binary to it and assume it won't be corrupted. Try using FileInputStream and FileOutputStream for binary. Also you don't need to create a file before writing to it. – Peter Lawrey Mar 24 '16 at 17:51
  • @PeterLawrey as far as I know its not binary, the text being read consists of letters, digits + special characters and the text that needs to be written consists of text and letters letters, digits + special characters – E. Sharp Mar 24 '16 at 18:09
  • 2
    Encrypted data is always binary (unless you use something like base64 encoding) – Peter Lawrey Mar 24 '16 at 18:10
  • You really should split this whole code into at least 3 methods. – Artjom B. Mar 24 '16 at 19:11

0 Answers0