Passwort im Klartext

Hi,
ich habe in meiner Oberfläche ein JPasswordField. In dieses gebe ich ein Passwort ein und schreibe dieses anschliessend mit der getPassword() Methode in eine Datei. Nun moechte ich gern in meinen Konsolen Programm diese Zeile aus der Datei auslesen und mit einem eingegebenen Passwort vergleichen. Aus diesem Grund brauche ich das Passwort aus der Datei im Klartext. Leider habe ich keine weg gefunden diese zu tun. Hat einer eine idee von euch wie ich das hinbekommen kann?
 
Hallo,

schau mal hier:
Java Almanac - e35. Reading Text from a File
http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html

Noch komfortabler und einfacher:
Java Almanac - e365. Reading and Writing a Properties File
http://javaalmanac.com/egs/java.util/Props.html
Java Almanac - e366. Getting and Setting Properties
http://javaalmanac.com/egs/java.util/GetSetProps.html


Du solltest die variante Properties bevorzugen, da du hier unter anderem die Möglichkeit hast Stringpaare zu speichern. Beispielsweise könnte eines der Benutzername und eines das Passwort sein. So hättest du direkten Zugriff auf das gewünschte Passwort. Für weiteres lies mal folgendes durch:
http://www.galileocomputing.de/open...sel11_006.htm#Rxx747java11006040003921F020100


Vg Erdal
 
Hallo der Kleine vom See,

hab ein ausführliches Beispiel geschrieben, in der du einige von den verschiedenen Anwendungen der Klasse java.util.Properties sehen kannst.

Java:
import java.io.*;

public class PasswordManager {

	private static PrintStream o = System.out;

	private BufferedReader in;

	private int task = -1;

	private String input = "";

	private PropertiesHelp ph = new PropertiesHelp("datastore.txt");

	public PasswordManager() {
		greet();
		askForTask();
	}

	public static void main(String[] args) {
		new PasswordManager();
	}

	public void askForTask() {

		o.println("Type a number for wanted task, please!");
		o.println("After any input you have to press ENTER.");
		o.println("If you want ...");
		o.println();
		o.println("e) exit Super PasswordManager 2000 -> type 0");
		o.println("a) write new user data -> type 1");
		o.println("b) get password for a given username -> type 2");
		o.println("c) check password for a given username -> type 3");
		o.println("d) check whether user exists or not-> type 4");
		o.println("d) print user data list -> type 5");

		o.println();

		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			input = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			task = Integer.valueOf(input);
		} catch (NumberFormatException e) {
			o.println("Your input was invalid. Try it again, please!");
			o.println();
			task = 99;
		}

		switch (task) {
		case 0: {
			o.println("Bye till next session");
			System.exit(0);
		}
			break;
		case 1: {
			enterNewUserdata();
		}
			break;
		case 2: {
			getPasswordforUsername();
		}
			break;
		case 3: {
			checkPassword();
		}
			break;
		case 4: {
			containsUser();
		}
			break;
		case 5: {
			listUserData();
		}
			break;
		default:
			askForTask();
		}

	}

	public void greet() {
		o.println("Hallo! Welcome to the prompt of Super PasswordManager 2000");
		o.println("Please, press ENTER to go on.");
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			input = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		o.println();
	}

	public void enterNewUserdata() {
		String username = "";
		String password = "";

		o.println("Type a user name, please!");

		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			input = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		username = input;
		o.println("Type the password for this user, please!");

		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			input = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		password = input;
		if (username != null && username.length() > 0 && password != null
				&& password.length() > 0) {
			ph.setProperty(username, password);
			o.println("user data is succesfully saved.");
		}
		o.println();
		askForTask();
	}

	public void getPasswordforUsername() {
		String username = "";

		o.println("Type a user name, please!");

		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			input = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		username = input;

		if (username != null && username.length() > 0) {
			if (ph.containsKey(username))
				o.println("password: " + ph.getProperty(username));
			else
				o.println("User don't exists in database.");
		}

		o.println();
		askForTask();
	}

	public void checkPassword() {
		String username = "";
		String password = "";

		o.println("Type a user name, please!");

		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			input = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		username = input;
		o.println("Type the password for this user, please!");

		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			input = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		password = input;
		if (username != null && username.length() > 0 && password != null
				&& password.length() > 0) {
			if (ph.containsKey(username))
				if (ph.getProperty(username).equals(password))
					o.println("Yes, password is right.");
				else
					o.println("No, password is wrong.");
			else
				o.println("User don't exists.");
		} else
			o.println("Input is invalid, try it again, please!");
		o.println();
		askForTask();
	}

	public void containsUser() {
		String username = "";

		o.println("Type a user name, please!");

		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			input = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		username = input;

		if (username != null && username.length() > 0) {
			if (ph.containsKey(username))
				o.println("User exists in database.");
			else
				o.println("User don't exists in database.");
		}

		o.println();
		askForTask();
	}

	public void listUserData() {
		ph.printOut();

		o.println();
		askForTask();
	}
}

Hilfsklasse um die Nutzung der Klasse Properties zu vereinfachen:
Java:
import java.io.*;
import java.util.Properties;

public class PropertiesHelp {

	private String file = "";

	public PropertiesHelp(String qfile) {
		file = qfile;
	}

	public String getProperty(String key) {
		String out = "";
		try {
			Properties prop = new Properties();
			FileInputStream in = new FileInputStream(file);
			prop.load(in);
			in.close();
			out = prop.getProperty(key);
		} catch (IOException e) {
		}
		return out;
	}

	public void setProperty(String key, String value) {
		try {
			Properties prop = new Properties();
			FileInputStream in = new FileInputStream(file);
			prop.load(in);
			in.close();
			prop.setProperty(key, value);
			FileOutputStream out = new FileOutputStream(file);
			prop.store(out, null);
			out.close();
		} catch (IOException e) {
		}
	}

	public boolean containsKey(String key) {
		boolean out = false;
		try {
			Properties prop = new Properties();
			FileInputStream in = new FileInputStream(file);
			prop.load(in);
			in.close();
			out = prop.containsKey(key);
		} catch (IOException e) {
		}
		return out;
	}

	public boolean isEmpty() {
		boolean out = false;
		try {
			Properties prop = new Properties();
			FileInputStream in = new FileInputStream(file);
			prop.load(in);
			in.close();
			out = prop.isEmpty();
		} catch (IOException e) {
		}
		return out;
	}

	public void printOut() {
		try {
			Properties prop = new Properties();
			FileInputStream in = new FileInputStream(file);
			prop.load(in);
			in.close();
			prop.list(System.out);
		} catch (IOException e) {
		}
	}
}


Vg Erdal
 
Zurück