Nutzername und Passwort abfragen und checken
import java.util.*; import java.util.Arrays; public class Starter { public static void main(String[] args) { System.out.println("Hallo Leute, Nutzername und Passwort bitte:"); Scanner eingabe = new Scanner(System.in); int i=0; String[] Nutzdaten = new String[2]; while (i < 2) { Nutzdaten[i]=eingabe.next(); i++; } eingabe.close(); UserData Accounts = new UserData(); String[] userNames = Accounts.UserNames; System.out.println(Arrays.asList(userNames).contains("Keule")); System.out.println(Arrays.asList(userNames).indexOf("Keule")); boolean UserLogin = Accounts.UserCheck(Nutzdaten); if (UserLogin) { System.out.println("Nutzername und Pass OK "); } else { System.out.println("Nutzername und/oder Pass sind falsch. "); } System.out.println("Nutzername: " + Nutzdaten[0] + "\n Nutzerpass:"+ Nutzdaten[1]); } }
import java.util.Arrays; import java.security.NoSuchAlgorithmException; //für alle Welt public class UserData { //für uns, unsere Kinder und Eltern protected String[] UserNames = {"Atze","Keule","Aisha","Semiramis"}; //nur für uns private String[] UserPass = { "f65255094d7773ed8dd417badc9fc045c1f80fdc5b2d25172b031ce6933e039a", "f65255094d7773ed8dd417badc9fc045c1f80fdc5b2d25172b031ce6933e039a", "f65255094d7773ed8dd417badc9fc045c1f80fdc5b2d25172b031ce6933e039a", "f65255094d7773ed8dd417badc9fc045c1f80fdc5b2d25172b031ce6933e039a" }; public boolean UserCheck(String[] UserInput) { boolean check=false; Werkzeug Hashmaker = new Werkzeug(); int PassIndex = Arrays.asList(this.UserNames).indexOf(UserInput[0]); String Pass = this.UserPass[PassIndex]; try { String InputPass = Hashmaker.Encrypter(UserInput[1]); System.out.println(InputPass); System.out.println(Pass); if (Pass.contentEquals(InputPass)) { check = true; } } catch(NoSuchAlgorithmException ie) { ie.printStackTrace(); } return check; } }
import java.util.*; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * @author zettberlin * */ public class Werkzeug { public double Rechner(){ Scanner sc = new Scanner(System.in); while(sc.hasNext()) { String next = sc.next(); System.out.println(next); if (next.contains("meh")) { break; } } sc.close(); double Result = 0.0; Result = 4 + 2; return Result; } public String Encrypter (String inputString) throws NoSuchAlgorithmException{ String hashedString =""; String password = inputString; MessageDigest md = MessageDigest.getInstance("SHA-256"); byte[] hashInBytes = md.digest(password.getBytes(StandardCharsets.UTF_8)); // bytes to hex StringBuilder sb = new StringBuilder(); for (byte b : hashInBytes) { sb.append(String.format("%02x", b)); } hashedString = sb.toString(); return hashedString; } }
public class NutzerAnzeige extends UserData{ public String[] getUsers() { String[] Users; Users = this.UserNames; return Users; } }