|
@ -15,11 +15,11 @@ public class Payload { |
|
|
String offen; |
|
|
String offen; |
|
|
String verschlüsselt; |
|
|
String verschlüsselt; |
|
|
SecretKeySpec password; |
|
|
SecretKeySpec password; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Payload() { |
|
|
public Payload() { |
|
|
offen = ""; |
|
|
offen = ""; |
|
|
verschlüsselt = ""; |
|
|
verschlüsselt = ""; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
this.setPassword(""); |
|
|
this.setPassword(""); |
|
|
} catch (UnsupportedEncodingException e) { |
|
|
} catch (UnsupportedEncodingException e) { |
|
@ -30,8 +30,8 @@ public class Payload { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getOffen() { |
|
|
public String getOffen() { |
|
|
return offen; |
|
|
return offen; |
|
|
} |
|
|
} |
|
@ -54,33 +54,34 @@ public class Payload { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setPassword(String password) throws GeneralSecurityException, UnsupportedEncodingException { |
|
|
|
|
|
|
|
|
public void setPassword(String password) |
|
|
|
|
|
throws GeneralSecurityException, UnsupportedEncodingException { |
|
|
byte[] key = (password).getBytes("UTF-8"); |
|
|
byte[] key = (password).getBytes("UTF-8"); |
|
|
|
|
|
|
|
|
// aus dem Array einen Hash-Wert erzeugen mit MD5 oder SHA |
|
|
|
|
|
MessageDigest sha = MessageDigest.getInstance("SHA-256"); |
|
|
|
|
|
key = sha.digest(key); |
|
|
|
|
|
|
|
|
|
|
|
// nur die ersten 128 bit nutzen |
|
|
|
|
|
key = Arrays.copyOf(key, 16); |
|
|
|
|
|
|
|
|
|
|
|
// der fertige Schluessel |
|
|
|
|
|
this.password = new SecretKeySpec(key, "AES"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// aus dem Array einen Hash-Wert erzeugen mit MD5 oder SHA |
|
|
|
|
|
MessageDigest sha = MessageDigest.getInstance("SHA-256"); |
|
|
|
|
|
key = sha.digest(key); |
|
|
|
|
|
|
|
|
|
|
|
// nur die ersten 128 bit nutzen |
|
|
|
|
|
key = Arrays.copyOf(key, 16); |
|
|
|
|
|
|
|
|
|
|
|
// der fertige Schluessel |
|
|
|
|
|
this.password = new SecretKeySpec(key, "AES"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void verschlüsseln() throws NoSuchAlgorithmException, GeneralSecurityException { |
|
|
public void verschlüsseln() throws NoSuchAlgorithmException, GeneralSecurityException { |
|
|
Cipher cipher = Cipher.getInstance("AES"); |
|
|
Cipher cipher = Cipher.getInstance("AES"); |
|
|
cipher.init(Cipher.ENCRYPT_MODE, password); |
|
|
cipher.init(Cipher.ENCRYPT_MODE, password); |
|
|
byte[] encrypted = cipher.doFinal(offen.getBytes()); |
|
|
byte[] encrypted = cipher.doFinal(offen.getBytes()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verschlüsselt = Base64.getEncoder().encodeToString(encrypted); |
|
|
verschlüsselt = Base64.getEncoder().encodeToString(encrypted); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void entschlüsseln() throws NoSuchAlgorithmException, GeneralSecurityException { |
|
|
public void entschlüsseln() throws NoSuchAlgorithmException, GeneralSecurityException { |
|
|
byte[] text = Base64.getDecoder().decode(verschlüsselt); |
|
|
byte[] text = Base64.getDecoder().decode(verschlüsselt); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Cipher cipher = Cipher.getInstance("AES"); |
|
|
Cipher cipher = Cipher.getInstance("AES"); |
|
|
cipher.init(Cipher.DECRYPT_MODE, password); |
|
|
cipher.init(Cipher.DECRYPT_MODE, password); |
|
|
byte[] cipherData2 = cipher.doFinal(text); |
|
|
byte[] cipherData2 = cipher.doFinal(text); |
|
|