I'm working on a programming and its loading strings from a file and settings variables equal to them. It's weird because in some class's they value of these variables is equal to what I am expecting, lets say "hello world" but in other class's they equal null. It makes no sense to me.
Heres the class thats reading the file
package encrypter; import java.io.*; import java.util.Scanner; import java.security.SecureRandom; import java.math.BigInteger; public class Config { public String name, passwordLogPath, randKeyPath; private Scanner getInput; private BufferedWriter out; private BufferedReader in; private String line = null; private SecureRandom random = new SecureRandom(); public File configFile = new File(System.getProperty("user.home") + "/.encrypter/EConfig.conf"), passwordLog, randKey; public void createRandomKey() throws IOException { randKey.createNewFile(); out = new BufferedWriter(new FileWriter(randKey, true)); out.write("Key: " + new BigInteger(130, random)); out.newLine(); out.close(); loadConfig(); } public void createConfig() throws IOException { System.out.println("Setup is begining..."); new File(System.getProperty("user.home") + "/.encrypter").mkdir(); configFile.createNewFile(); out = new BufferedWriter(new FileWriter(configFile, true)); System.out.print("Please enter your first name: "); getInput = new Scanner(System.in); name = getInput.nextLine(); if (!name.equals("")) out.write("Name: " + name); else out.write("Name: Bob"); out.newLine(); System.out.print("ADVANCED: Path to password log (Leave blank for default): "); passwordLogPath = getInput.nextLine(); if (!passwordLogPath.equals("")) out.write("Password_Log_Path: " + passwordLogPath); else out.write("Password_Log_Path: " + System.getProperty("user.home") + "/.encrypter/"); out.newLine(); System.out.print("ADVANCED: Path to randomly generated key (Leave blank for default): "); randKeyPath = getInput.nextLine(); if (!randKeyPath.equals("")) out.write("Random_Key_Path: " + randKeyPath); else out.write("Random_Key_Path: " + System.getProperty("user.home") + "/.encrypter/"); out.newLine(); out.close(); System.out.println("Setup is complete!"); loadConfig(); } public void loadConfig() throws IOException { Main m = new Main(); if (configFile.isFile()) { in = new BufferedReader(new FileReader(configFile)); //System.out.println("\tLoading configurations..."); while ((line = in.readLine()) != null) { if (line.startsWith("Name: ")) name = line.substring(6); if (line.startsWith("Password_Log_Path: ")) passwordLogPath = line.substring(19); if (line.startsWith("Random_Key_Path: ")) randKeyPath = line.substring(17); } passwordLog = new File(passwordLogPath + "encrypted.log"); m.manage.data = passwordLog; if (!passwordLog.isFile()) passwordLog.createNewFile(); randKey = new File(randKeyPath + "Frozen_Vault.key"); if (randKey.isFile()) { in = new BufferedReader(new FileReader(randKey)); while ((line = in.readLine()) != null) { if (line.startsWith("Key: ")) m.computerKey = line.substring(5); } } else { try { createRandomKey(); } catch (IOException e) {e.printStackTrace(); } } //System.out.println("\tConfiguration Loaded!"); //System.out.println(m.computerKey); } else { try { createConfig(); } catch (IOException e) { e.printStackTrace(); } } } }
This is the main class, also it is getting the proper value
package encrypter; /** * * @author Josh * @date Tuesday January 3, 2012 1:17 AM * If you modify this and release it please give me credit! Thank you. */ import java.io.IOException; import java.util.*; public class Main { public Scanner type; // Creates a scanner object. public String userKey, computerKey, password, verify; // Strings that will hold values. public String[] commands = {"help", "commands", "new", "ls", "rm / remove", "clear", "backup", "restore", "exit"}; // Array of commands public String[] descrip = {" - Displays helpful information."," - Lists current commands.", " - Alows you to save new passwords.", " - Lists currently saved passwords.", " - Alows you to remove a single password.", " - Deletes all saved passwords.", " - Backs up all your passwords.", " - Restores passwords from backup file.", " - Saves your work and exits the program."}; // Array of command Descriptions public Encrypt encrypt = new Encrypt(); // Creates object for the class "Encrypt". public Decrypt decrypt = new Decrypt(); // Creates object for the class "Decrypt". public FileManager manage = new FileManager(); public Config conf = new Config(); public static Commands c = new Commands(); public static void main(String[] args) { // Main method, just fires up the program and loops through it. Main m = new Main(); System.out.println("Frozen Vault Password Encrypter - Newbi3"); try { m.conf.loadConfig(); } catch (IOException e) { System.out.println("Error Loading Configuration File."); } System.out.println("\n\t Type 'help' for help..."); m.type = new Scanner(System.in); System.out.println(m.conf.passwordLogPath); while (true) { System.out.print("\n"+ m.conf.name + ", enter a command: "); c.switchCommand(m.type.nextLine()); System.out.println(m.conf.passwordLogPath); } } }
These are the rest of the class's returning nulls
package encrypter; import java.io.*; public class FileManager { public File data; public void removePassword(String lineToRemove) { Config conf = new Config(); try { data = conf.passwordLog; if (!data.isFile()) { System.out.println("\tF4 error - File not found."); return; } //Construct the new file that will later be renamed to the original filename. File tempFile = new File(data.getAbsolutePath() + ".tmp"); BufferedReader br = new BufferedReader(new FileReader(data)); PrintWriter pw = new PrintWriter(new FileWriter(tempFile)); String line = null; //Read from the original file and write to the new //unless content matches data to be removed. while ((line = br.readLine()) != null) { if (!line.trim().equals(lineToRemove)) { pw.println(line); pw.flush(); } } pw.close(); br.close(); //Delete the original file if (!data.delete()) { System.out.println("\tF4 error - Complications removing password."); return; } //Rename the new file to the filename the original file had. if (!tempFile.renameTo(data)) { System.out.println("\tF5 error - Complications removing password."); } System.out.println("\tPassword removed successfully!"); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } public void backupPasswords() throws IOException { Config conf = new Config(); //data = new File(System.getProperty("user.home") + "/.encrypter/encrypted.log"); data = conf.passwordLog; if (!data.isFile()) { System.out.println("\tF4 error - File not found."); return; } //Construct the new file that will later be renamed to the original filename. File backupFile = new File(data.getAbsolutePath() + ".backup"); BufferedReader br = new BufferedReader(new FileReader(data)); PrintWriter pw = new PrintWriter(new FileWriter(backupFile)); String line = null; //Read from the original file and write to the new while ((line = br.readLine()) != null) { pw.println(line); pw.flush(); } pw.close(); br.close(); System.out.println("\tPasswords backed up successfuly!"); } public void loadBackup() throws IOException { Config conf = new Config(); data = conf.passwordLog; //data = new File(System.getProperty("user.home") + "/.encrypter/encrypted.log"); if (!data.isFile()) { System.out.println("\tF4 error - File not found."); return; } data.delete(); data.createNewFile(); File backupFile = new File(data.getAbsolutePath() + ".backup"); BufferedReader br = new BufferedReader(new FileReader(backupFile)); PrintWriter pw = new PrintWriter(new FileWriter(data)); String line = null; while ((line = br.readLine()) != null) { pw.println(line); pw.flush(); } pw.close(); br.close(); System.out.println("\tBackup file loaded successfuly!"); } public void clearFile() throws IOException { data = new File(System.getProperty("user.home") + "/.encrypter/encrypted.log"); data.delete(); data.createNewFile(); System.out.println("\tPasswords ereased successfuly!"); } }
package encrypter; import java.io.IOException; import java.util.Scanner; public class Commands { public Scanner getInput; private Main m = new Main(); private String choice; public void switchCommand(String cmd) { getInput = new Scanner(System.in); String c = cmd.toLowerCase(); System.out.println(m.conf.passwordLogPath); switch(c) { case "help": System.out.println("\tJosh's Password Encrypter"); System.out.println("\n\tWhat is a user key? The 'password' required to retrive your saved passwords"); System.out.println("\tType 'commands' to see is a list of commands"); break; case "commands": //System.out.println("\n"); for (int i = 0; i < m.commands.length; i++) System.out.println("\t" + m.commands[i] + m.descrip[i]); break; case "new": System.out.print("Please enter your user key: "); m.userKey = getInput.nextLine(); System.out.print("Please verify your user key: "); m.verify = getInput.nextLine(); if (!m.userKey.equals(m.verify)) { System.out.println("\tYour user keys did not match!"); m.verify = ""; } else { System.out.print("Please enter your password to add: "); m.password = getInput.nextLine(); System.out.print("Please verify your password to add: "); m.verify = getInput.nextLine(); if (!m.password.equals(m.verify)) System.out.println("\tPasswords did not match!"); else { try { m.encrypt.write(m.userKey, m.password); } catch (Exception e) { System.out.println("\tE1 Error - File not found"); System.out.println(m.manage.data); } } } m.verify = ""; m.userKey = ""; m.password = ""; break; case "rm": case "remove": System.out.print("Please enter your user key: "); m.userKey = getInput.nextLine(); System.out.print("Please verify your user key: "); m.verify = getInput.nextLine(); if (!m.userKey.equals(m.verify)) { System.out.println("\tYour user keys did not match!"); } else { System.out.print("Please enter the password to remove: "); m.password = getInput.nextLine(); System.out.print("Please very the password to remove: "); m.verify = getInput.nextLine(); if (!m.password.equals(m.verify)) { System.out.println("\tPasswords did not match, nothing removed."); m.verify = ""; } else { String toRemove = m.encrypt.encrypted(m.userKey, m.password); m.manage.removePassword(toRemove);; } } m.verify = ""; m.userKey = ""; m.password = ""; break; case "ls": System.out.print("Please enter your user key: "); m.userKey = getInput.nextLine(); try { m.decrypt.readPassword(m.userKey); } catch (Exception e) { System.out.println("\tD2 Error - File not found."); } m.userKey = ""; break; case "clear": System.out.print("This will erase ALL passwords. Continue? (Y/n): "); choice = getInput.nextLine(); if (choice.equalsIgnoreCase("y")) { try { m.manage.clearFile(); } catch (IOException e) { System.out.println("\tF3 Error - Could not access file."); } } else { System.out.println("\tPasswords NOT ereased."); } break; case "backup": System.out.print("This will create a backup file of your passwords. Continue? (Y/n): "); choice = getInput.nextLine(); if (choice.equalsIgnoreCase("y")) { try { m.manage.backupPasswords(); } catch (IOException e) { e.printStackTrace(); } } else System.out.println("\tPasswords were NOT backed up!"); break; case "restore": System.out.print("This will replace all your saved data with your last backup. Continue? (Y/n): "); choice = getInput.nextLine(); if (choice.equalsIgnoreCase("y")) { try { m.manage.loadBackup(); } catch (IOException e) { e.printStackTrace(); } } else System.out.println("\tBackup file was NOT loaded!"); break; case "exit": System.out.print("Are you sure you want to exit? (Y/n): "); String choice = getInput.nextLine(); if (choice.equalsIgnoreCase("y")) { System.out.println("\tNow exiting Joshs' Password Encrypter..."); System.exit(0); } else System.out.println("\tProgram did NOT exit."); break; } } }
package encrypter; import java.io.*; import java.util.*; public class Decrypt { private Scanner scan; // Declares Scanner private String cpassword; // Creates a string to hold a value /** * Decryption Algorithm * How its works: * Works the same way as the encryption but instead of adding "power" and "position" it subtracts */ public String decrypted(String key, String toRead) { int power; char[] userKey = key.toCharArray(); char[] password = toRead.toCharArray(); for (int i = 0; i < userKey.length; i++) { for (int b = 0; b < password.length; b++) { power = userKey[i]; int position = password[b]; char c = (char) (position - power); password[b] = c; } } return String.valueOf(password); } public void readPassword(String key) throws FileNotFoundException { // Reads the encrypted string then outprints the decrypted string. scan = new Scanner(new File(System.getProperty("user.home") + "/.encrypter/encrypted.log")); System.out.println("Current Passwords: "); while (scan.hasNext()) { cpassword = scan.nextLine(); cpassword.toCharArray(); System.out.println("\t" + decrypted(key, cpassword)); } } }
package encrypter; import java.io.*; public class Encrypt { private BufferedWriter out; /** * Encryption Algorithm * How it works: * Converts string "key" and string "toWrite" into an array of characters * Converts the arrays to integers "power" and "position" * Adds them together and then converts back to an array of different characters and then back to a string. */ public String encrypted(String key, String toWrite) { int power; char[] userKey = key.toCharArray(); char[] password = toWrite.toCharArray(); for (int i = 0; i < userKey.length; i++) { for (int b = 0; b < password.length; b++) { power = userKey[i]; int position = password[b]; char c = (char) (position + power); password[b] = c; System.out.println(power + " " + (password[b = c])); } } return String.valueOf(password); } public void write(String key, String password) throws IOException { // Writes the encrypted string to a file. Main m = new Main(); out = new BufferedWriter(new FileWriter(m.manage.data, true)); out.write(encrypted(key, password)); out.newLine(); out.close(); System.out.println("Your password was saved successfuly! " + encrypted(key, password)); } }
Look at the variable passwordLogPath defined in Config and lets say it equals "/home/bob/" in the class Main it will equal "/home/bob/" but in every other class it equals null. I don't understand it's doing this.