读取文件的内容,将其存储在变量中,加密并将其存储在新文件中

时间:2016-03-24 17:46:04

标签: java encryption filereader filewriter

基本上我需要从用户指定的文件中读取文本,然后通过将其转换为ASCII值来加密它。 (我知道它的弱加密就是这个任务)。加密工作正常,用户想要创建的用于存储加密值的文件正在运行,但我不知道为什么它不读取文件中的文本并加密它。因此,存储加密文本的文件为空。

我认为代码中的问题介于两者之间 FileWriter writer = null;

e.printStackTrace();             }

代码中的

......但我不知道它在哪里。当它工作时,它应该读取文件的内容," fileUse",将内容存储为字符串,然后使用关键字加密它。然后应该将加密的值存储在新创建的文件中,用户指定了哪个是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();

    }
}

0 个答案:

没有答案