· 7 years ago · Jun 30, 2018, 09:04 PM
1import java.io.UnsupportedEncodingException;
2
3
4 import javax.swing.JOptionPane;
5
6import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
7
8import javax.crypto.SecretKey;
9 import javax.crypto.SecretKeyFactory;
10 import javax.crypto.spec.PBEKeySpec;
11 import java.security.NoSuchAlgorithmException;
12 import java.security.spec.InvalidKeySpecException;
13
14 public class PasswortGen
15 {
16 static final int SOLL_LAENGE_EINGABE = "laenge".length();
17 static final int POSITION_DOPPELPUNKT = 2;
18 static final String Masterpasswort = JOptionPane.showInputDialog("Geben Sie Ihr Masterpasswort ein");
19 static final String Versionsnummer = "Geben Sie die Website, sowie die aktuelle Versionsnummer ein";
20 static final String Kennung = "Geben Sie Ihre Kennung ein";
21 static final String laenge = JOptionPane.showInputDialog("Geben Sie die gewünschte Laenge ein (16 - Empfohlen)");
22
23
24
25 public static void main(String[] args) throws UnsupportedEncodingException { /*
26 * Die main1-Methode kombiniert alle Methoden zu Erstellung eines Passwords und leifert diesen als String, mit Hilfe der Konsole, aus.
27 */
28
29
30
31 String password = "pass"; //Dem Datentyp String mit der Erkennung password wird der Wert "pass" zugewiesen
32
33 String salt = "1234";//Dem Datentyp String mit der Erkennung salt wird der Wert "1234" zugewiesen
34
35 int iterations = 10000;//Dem Datentyp Int mit der Erkennung iterations wird der Wert 10000 zugewiesen
36
37 int keyLength = 16 ; //Dem Datentyp Int mit der Erkennung keyLength wird der Wert 16 zugewiesen, also das Passwort hat eine Lönge von 16
38
39 char[] passwordChars = password.toCharArray(); //Dem Datentyp Char[] mit der Erkennung passwordChars weist die Charakters zu.
40
41 byte[] saltBytes = salt.getBytes(); //Dem Datentyp byte mit der Erkennung saltBytes wird der Wert von saltBytes nachgefragt.
42
43
44
45 byte[] hashedBytes = hashPassword(passwordChars, saltBytes, iterations, keyLength); // WEIS ICH NICHT
46
47 String hashedString = HexBin.encodeHexString(hashedBytes); // WEIS ICH NICHT
48
49
50
51 System.out.println(hashedString); //Die Konsole liefert den String "hashedString".
52 }
53
54/*
55* Die main1-Methode kombiniert alle Methoden zu Erstellung eines Passwords und leifert diesen als String, mit Hilfe der Konsole, aus.
56*/
57
58
59
60 public static byte[] hashPassword( final char[] password, final byte[] salt, final int iterations, final int keyLength ) {
61
62
63
64 try {
65
66 SecretKeyFactory skf = SecretKeyFactory.getInstance( "PBKDF2WithHmacSHA512" );
67
68 PBEKeySpec spec = new PBEKeySpec( password );
69
70 SecretKey key = skf.generateSecret( spec );
71
72 byte[] res = key.getEncoded( );
73
74 return res;
75
76 } catch ( NoSuchAlgorithmException | InvalidKeySpecException e ) {
77
78 throw new RuntimeException( e );
79
80 }
81
82 }
83
84
85 }
86
87I have to put in a password( like water), that will be encrypted into a password which i should set the length of. So after that i should type water again and the same password it got encrypted to should appear the same again.
88
89Thanks