Christian Baltzer
5 years ago
4 changed files with 242 additions and 0 deletions
-
22src/main/java/com/ugsbo/Crypto/Crypto_Logic.java
-
92src/main/java/com/ugsbo/Crypto/Payload.java
-
66src/test/java/com/ugsbo/Crypto/Crypt.java
-
62src/test/java/com/ugsbo/Crypto/DeCrypt.java
@ -0,0 +1,22 @@ |
|||
package com.ugsbo.Crypto; |
|||
|
|||
import java.io.UnsupportedEncodingException; |
|||
import java.security.GeneralSecurityException; |
|||
|
|||
public class Crypto_Logic { |
|||
|
|||
public static void main(String[] args) throws UnsupportedEncodingException, GeneralSecurityException { |
|||
// TODO Auto-generated method stub |
|||
String test = "Hallo, mein Name ist Christian"; |
|||
|
|||
Payload temp = new Payload(); |
|||
temp.setOffen(test); |
|||
temp.setPassword("geheim"); |
|||
temp.verschlüsseln(); |
|||
System.out.println(temp.getVerschlüsselt()); |
|||
|
|||
temp.entschlüsseln(); |
|||
System.out.println(temp.getOffen()); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,92 @@ |
|||
package com.ugsbo.Crypto; |
|||
|
|||
import java.io.UnsupportedEncodingException; |
|||
import java.security.GeneralSecurityException; |
|||
import java.security.MessageDigest; |
|||
import java.security.NoSuchAlgorithmException; |
|||
import java.util.Arrays; |
|||
import java.util.Base64; |
|||
import javax.crypto.*; |
|||
import javax.crypto.spec.SecretKeySpec; |
|||
|
|||
|
|||
public class Payload { |
|||
|
|||
String offen; |
|||
String verschlüsselt; |
|||
SecretKeySpec password; |
|||
|
|||
public Payload() { |
|||
offen = ""; |
|||
verschlüsselt = ""; |
|||
|
|||
try { |
|||
this.setPassword(""); |
|||
} catch (UnsupportedEncodingException e) { |
|||
// TODO Auto-generated catch block |
|||
e.printStackTrace(); |
|||
} catch (GeneralSecurityException e) { |
|||
// TODO Auto-generated catch block |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
|
|||
public String getOffen() { |
|||
return offen; |
|||
} |
|||
|
|||
|
|||
|
|||
public String getVerschlüsselt() { |
|||
return verschlüsselt; |
|||
} |
|||
|
|||
public void setOffen(String offen) { |
|||
this.offen = offen; |
|||
} |
|||
|
|||
|
|||
|
|||
public void setVerschlüsselt(String verschlüsselt) { |
|||
this.verschlüsselt = verschlüsselt; |
|||
} |
|||
|
|||
|
|||
|
|||
public void setPassword(String password) |
|||
throws GeneralSecurityException, UnsupportedEncodingException { |
|||
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"); |
|||
} |
|||
|
|||
|
|||
|
|||
public void verschlüsseln() throws NoSuchAlgorithmException, GeneralSecurityException { |
|||
Cipher cipher = Cipher.getInstance("AES"); |
|||
cipher.init(Cipher.ENCRYPT_MODE, password); |
|||
byte[] encrypted = cipher.doFinal(offen.getBytes()); |
|||
|
|||
verschlüsselt = Base64.getEncoder().encodeToString(encrypted); |
|||
} |
|||
|
|||
public void entschlüsseln() throws NoSuchAlgorithmException, GeneralSecurityException { |
|||
byte[] text = Base64.getDecoder().decode(verschlüsselt); |
|||
|
|||
Cipher cipher = Cipher.getInstance("AES"); |
|||
cipher.init(Cipher.DECRYPT_MODE, password); |
|||
byte[] cipherData2 = cipher.doFinal(text); |
|||
offen = new String(cipherData2); |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,66 @@ |
|||
/** |
|||
* |
|||
*/ |
|||
package com.ugsbo.Crypto; |
|||
|
|||
import com.ugsbo.Crypto.*; |
|||
import static org.junit.Assert.*; |
|||
import java.io.UnsupportedEncodingException; |
|||
import java.security.GeneralSecurityException; |
|||
import java.security.NoSuchAlgorithmException; |
|||
import org.junit.Before; |
|||
import org.junit.Test; |
|||
|
|||
public class Crypt { |
|||
|
|||
|
|||
private Payload workingobjekt; |
|||
|
|||
@Before |
|||
public void setUp() throws Exception { |
|||
workingobjekt = new Payload(); |
|||
} |
|||
|
|||
@Test |
|||
public void offenIstAnders() { |
|||
|
|||
String eingabe = "TestText"; |
|||
String ergebnis; |
|||
String password = ""; |
|||
|
|||
try { |
|||
workingobjekt.setOffen(eingabe); |
|||
workingobjekt.setPassword(password); |
|||
workingobjekt.entschlüsseln(); |
|||
} catch (GeneralSecurityException e) { |
|||
e.printStackTrace(); |
|||
} catch (UnsupportedEncodingException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
ergebnis = workingobjekt.getVerschlüsselt(); |
|||
|
|||
assertNotEquals("unterschidliche Texte", eingabe, ergebnis); |
|||
} |
|||
|
|||
@Test |
|||
public void verUndEntschlüsseln() { |
|||
|
|||
String password = "Test"; |
|||
String eingabe = "TestText"; |
|||
String ergebnis; |
|||
|
|||
try { |
|||
workingobjekt.setOffen(eingabe); |
|||
workingobjekt.setPassword(password); |
|||
workingobjekt.verschlüsseln(); |
|||
workingobjekt.entschlüsseln(); |
|||
} catch (GeneralSecurityException | UnsupportedEncodingException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
ergebnis = workingobjekt.getOffen(); |
|||
|
|||
assertEquals("das entschlüsselte Test Wort", ergebnis, eingabe); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,62 @@ |
|||
package com.ugsbo.Crypto; |
|||
|
|||
import static org.junit.Assert.*; |
|||
import java.io.UnsupportedEncodingException; |
|||
import java.security.GeneralSecurityException; |
|||
import com.ugsbo.Crypto.*; |
|||
import org.junit.Before; |
|||
import org.junit.Test; |
|||
|
|||
public class DeCrypt { |
|||
|
|||
private Payload workingobjekt; |
|||
|
|||
@Before |
|||
public void setUp() throws Exception { |
|||
workingobjekt = new Payload(); |
|||
} |
|||
|
|||
@Test |
|||
public void verschlüsseltIstAnders() { |
|||
|
|||
String eingabe = "TestText"; |
|||
String password = "Test"; |
|||
String ergebnis; |
|||
|
|||
try { |
|||
workingobjekt.setVerschlüsselt(eingabe); |
|||
workingobjekt.setPassword(password); |
|||
workingobjekt.entschlüsseln(); |
|||
} catch (GeneralSecurityException e) { |
|||
e.printStackTrace(); |
|||
} catch (UnsupportedEncodingException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
ergebnis = workingobjekt.getOffen(); |
|||
|
|||
assertNotEquals("unterschidliche Texte", eingabe, ergebnis); |
|||
} |
|||
|
|||
@Test |
|||
public void entUndVerschlüsseln() { |
|||
|
|||
String password = "Test"; |
|||
String eingabe = "TestText"; |
|||
String ergebnis; |
|||
|
|||
try { |
|||
workingobjekt.setOffen(eingabe); |
|||
workingobjekt.setPassword(password); |
|||
workingobjekt.verschlüsseln(); |
|||
workingobjekt.entschlüsseln(); |
|||
} catch (GeneralSecurityException | UnsupportedEncodingException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
ergebnis = workingobjekt.getOffen(); |
|||
|
|||
assertEquals("das entschlüsselte Test Wort", ergebnis, eingabe); |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue