From 63a57eaba0b64fdc0e5781711da9dede610d1201 Mon Sep 17 00:00:00 2001 From: Christian Baltzer Date: Sat, 13 Jul 2019 16:22:28 +0200 Subject: [PATCH 1/9] init --- src/main/java/com/ugsbo/Crypto/Crypto_Logic.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/com/ugsbo/Crypto/Crypto_Logic.java diff --git a/src/main/java/com/ugsbo/Crypto/Crypto_Logic.java b/src/main/java/com/ugsbo/Crypto/Crypto_Logic.java new file mode 100644 index 0000000..cc00e96 --- /dev/null +++ b/src/main/java/com/ugsbo/Crypto/Crypto_Logic.java @@ -0,0 +1,10 @@ +package com.ugsbo.Crypto; + +public class Crypto_Logic { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} From e5a69563eb818dff0604d11054e016b453b2803e Mon Sep 17 00:00:00 2001 From: Christian Baltzer Date: Sat, 13 Jul 2019 16:24:18 +0200 Subject: [PATCH 2/9] Testclassen erstellt --- src/test/java/com/ugsbo/Crypto/Crypt.java | 27 +++++++++++++++++++++ src/test/java/com/ugsbo/Crypto/DeCrypt.java | 17 +++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/test/java/com/ugsbo/Crypto/Crypt.java create mode 100644 src/test/java/com/ugsbo/Crypto/DeCrypt.java diff --git a/src/test/java/com/ugsbo/Crypto/Crypt.java b/src/test/java/com/ugsbo/Crypto/Crypt.java new file mode 100644 index 0000000..7a480d5 --- /dev/null +++ b/src/test/java/com/ugsbo/Crypto/Crypt.java @@ -0,0 +1,27 @@ +/** + * + */ +package com.ugsbo.Crypto; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + +/** + * @author christian + * + */ +public class Crypt { + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception {} + + @Test + public void test() { + fail("Not yet implemented"); + } + +} diff --git a/src/test/java/com/ugsbo/Crypto/DeCrypt.java b/src/test/java/com/ugsbo/Crypto/DeCrypt.java new file mode 100644 index 0000000..d55c094 --- /dev/null +++ b/src/test/java/com/ugsbo/Crypto/DeCrypt.java @@ -0,0 +1,17 @@ +package com.ugsbo.Crypto; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + +public class DeCrypt { + + @Before + public void setUp() throws Exception {} + + @Test + public void test() { + fail("Not yet implemented"); + } + +} From 4152a430dae42b7bdf4b6ed3d89e9b02cb1d0449 Mon Sep 17 00:00:00 2001 From: Christian Baltzer Date: Sat, 13 Jul 2019 16:59:54 +0200 Subject: [PATCH 3/9] Struktur und Tests sind fertig --- .../java/com/ugsbo/Crypto/Crypto_Logic.java | 2 +- src/main/java/com/ugsbo/Crypto/Payload.java | 59 +++++++++++++++++++ src/test/java/com/ugsbo/Crypto/Crypt.java | 38 ++++++++---- src/test/java/com/ugsbo/Crypto/DeCrypt.java | 31 +++++++++- 4 files changed, 116 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/ugsbo/Crypto/Payload.java diff --git a/src/main/java/com/ugsbo/Crypto/Crypto_Logic.java b/src/main/java/com/ugsbo/Crypto/Crypto_Logic.java index cc00e96..08bf0c9 100644 --- a/src/main/java/com/ugsbo/Crypto/Crypto_Logic.java +++ b/src/main/java/com/ugsbo/Crypto/Crypto_Logic.java @@ -4,7 +4,7 @@ public class Crypto_Logic { public static void main(String[] args) { // TODO Auto-generated method stub - + } } diff --git a/src/main/java/com/ugsbo/Crypto/Payload.java b/src/main/java/com/ugsbo/Crypto/Payload.java new file mode 100644 index 0000000..5e4e2d4 --- /dev/null +++ b/src/main/java/com/ugsbo/Crypto/Payload.java @@ -0,0 +1,59 @@ +package com.ugsbo.Crypto; + +public class Payload { + + String offen; + String verschlüsselt; + String password; + + public Payload() { + + } + + + public String getOffen() { + return offen; + } + + + + public String getVerschlüsselt() { + return verschlüsselt; + } + + + + public String getPassword() { + return password; + } + + + + 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) { + this.password = password; + } + + + public void verschlüsseln() { + + + } + + public void entschlüsseln() { + + + } + +} diff --git a/src/test/java/com/ugsbo/Crypto/Crypt.java b/src/test/java/com/ugsbo/Crypto/Crypt.java index 7a480d5..ca90b94 100644 --- a/src/test/java/com/ugsbo/Crypto/Crypt.java +++ b/src/test/java/com/ugsbo/Crypto/Crypt.java @@ -3,25 +3,43 @@ */ package com.ugsbo.Crypto; +import com.ugsbo.Crypto.*; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; -/** - * @author christian - * - */ public class Crypt { - /** - * @throws java.lang.Exception - */ + + private Payload workingobjekt; + @Before - public void setUp() throws Exception {} + public void setUp() throws Exception { + workingobjekt = new Payload(); + } @Test - public void test() { - fail("Not yet implemented"); + public void offenIstAnders() { + + String eingabe = "TestText"; + + workingobjekt.setOffen(eingabe); + workingobjekt.verschlüsseln(); + workingobjekt.getVerschlüsselt(); + } + + @Test + public void verUndEntschlüsseln() { + + String eingabe = "TestText"; + String ergebnis; + + workingobjekt.setOffen(eingabe); + workingobjekt.verschlüsseln(); + workingobjekt.entschlüsseln(); + ergebnis = workingobjekt.getOffen(); + + assertEquals("das entschlüsselte Test Wort",ergebnis,eingabe); } } diff --git a/src/test/java/com/ugsbo/Crypto/DeCrypt.java b/src/test/java/com/ugsbo/Crypto/DeCrypt.java index d55c094..2cf121e 100644 --- a/src/test/java/com/ugsbo/Crypto/DeCrypt.java +++ b/src/test/java/com/ugsbo/Crypto/DeCrypt.java @@ -1,17 +1,42 @@ package com.ugsbo.Crypto; import static org.junit.Assert.*; + +import com.ugsbo.Crypto.*; import org.junit.Before; import org.junit.Test; public class DeCrypt { + private Payload workingobjekt; + @Before - public void setUp() throws Exception {} + public void setUp() throws Exception { + workingobjekt = new Payload(); + } @Test - public void test() { - fail("Not yet implemented"); + public void verschlüsseltIstAnders() { + + String eingabe = "TestText"; + + workingobjekt.setVerschlüsselt(eingabe); + workingobjekt.entschlüsseln(); + workingobjekt.getOffen(); + } + + @Test + public void entUndVerschlüsseln() { + + String eingabe = "TestText"; + String ergebnis; + + workingobjekt.setOffen(eingabe); + workingobjekt.verschlüsseln(); + workingobjekt.entschlüsseln(); + ergebnis = workingobjekt.getOffen(); + + assertEquals("das entschlüsselte Test Wort",ergebnis,eingabe); } } From 861a0076fdd59cfde31649d89217565dedbab05f Mon Sep 17 00:00:00 2001 From: Christian Baltzer Date: Sat, 13 Jul 2019 17:22:21 +0200 Subject: [PATCH 4/9] bugs in den tests gefixt --- src/main/java/com/ugsbo/Crypto/Payload.java | 7 ++++++- src/test/java/com/ugsbo/Crypto/Crypt.java | 5 ++++- src/test/java/com/ugsbo/Crypto/DeCrypt.java | 7 ++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/ugsbo/Crypto/Payload.java b/src/main/java/com/ugsbo/Crypto/Payload.java index 5e4e2d4..ebc4fd2 100644 --- a/src/main/java/com/ugsbo/Crypto/Payload.java +++ b/src/main/java/com/ugsbo/Crypto/Payload.java @@ -1,5 +1,7 @@ package com.ugsbo.Crypto; +import javax.crypto.*; + public class Payload { String offen; @@ -7,7 +9,9 @@ public class Payload { String password; public Payload() { - + offen = ""; + verschlüsselt = ""; + password = ""; } @@ -46,6 +50,7 @@ public class Payload { } + public void verschlüsseln() { diff --git a/src/test/java/com/ugsbo/Crypto/Crypt.java b/src/test/java/com/ugsbo/Crypto/Crypt.java index ca90b94..984e97b 100644 --- a/src/test/java/com/ugsbo/Crypto/Crypt.java +++ b/src/test/java/com/ugsbo/Crypto/Crypt.java @@ -22,10 +22,13 @@ public class Crypt { public void offenIstAnders() { String eingabe = "TestText"; + String ergebnis; workingobjekt.setOffen(eingabe); workingobjekt.verschlüsseln(); - workingobjekt.getVerschlüsselt(); + ergebnis = workingobjekt.getVerschlüsselt(); + + assertNotEquals("unterschidliche Texte",eingabe,ergebnis); } @Test diff --git a/src/test/java/com/ugsbo/Crypto/DeCrypt.java b/src/test/java/com/ugsbo/Crypto/DeCrypt.java index 2cf121e..6e62c82 100644 --- a/src/test/java/com/ugsbo/Crypto/DeCrypt.java +++ b/src/test/java/com/ugsbo/Crypto/DeCrypt.java @@ -19,15 +19,20 @@ public class DeCrypt { public void verschlüsseltIstAnders() { String eingabe = "TestText"; + String password = "Test"; + String ergebnis; workingobjekt.setVerschlüsselt(eingabe); workingobjekt.entschlüsseln(); - workingobjekt.getOffen(); + ergebnis = workingobjekt.getOffen(); + + assertNotEquals("unterschidliche Texte",eingabe,ergebnis); } @Test public void entUndVerschlüsseln() { + String password = "Test"; String eingabe = "TestText"; String ergebnis; From 5637d8e53fad9f62f079a4dece0d970e3ae9163a Mon Sep 17 00:00:00 2001 From: Christian Baltzer Date: Sat, 13 Jul 2019 17:28:39 +0200 Subject: [PATCH 5/9] Password wird generiert --- src/main/java/com/ugsbo/Crypto/Payload.java | 41 +++++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/ugsbo/Crypto/Payload.java b/src/main/java/com/ugsbo/Crypto/Payload.java index ebc4fd2..80638a1 100644 --- a/src/main/java/com/ugsbo/Crypto/Payload.java +++ b/src/main/java/com/ugsbo/Crypto/Payload.java @@ -1,17 +1,32 @@ 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 javax.crypto.*; +import javax.crypto.spec.SecretKeySpec; public class Payload { String offen; String verschlüsselt; - String password; + SecretKeySpec password; public Payload() { offen = ""; verschlüsselt = ""; - password = ""; + + try { + this.setPassword(""); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (GeneralSecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } @@ -25,14 +40,6 @@ public class Payload { return verschlüsselt; } - - - public String getPassword() { - return password; - } - - - public void setOffen(String offen) { this.offen = offen; } @@ -45,8 +52,18 @@ public class Payload { - public void setPassword(String password) { - this.password = password; + 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"); } From 94f55732f44c2ab69e4596b0cd9c2c95e9597a0a Mon Sep 17 00:00:00 2001 From: Christian Baltzer Date: Sat, 13 Jul 2019 17:40:24 +0200 Subject: [PATCH 6/9] =?UTF-8?q?verschl=C3=BCsseln=20eingef=C3=BChrt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ugsbo/Crypto/Payload.java | 8 ++++++-- src/test/java/com/ugsbo/Crypto/Crypt.java | 17 +++++++++++++++-- src/test/java/com/ugsbo/Crypto/DeCrypt.java | 8 ++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/ugsbo/Crypto/Payload.java b/src/main/java/com/ugsbo/Crypto/Payload.java index 80638a1..36aa2c5 100644 --- a/src/main/java/com/ugsbo/Crypto/Payload.java +++ b/src/main/java/com/ugsbo/Crypto/Payload.java @@ -8,6 +8,7 @@ import java.util.Arrays; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; + public class Payload { String offen; @@ -68,9 +69,12 @@ public class Payload { - public void verschlüsseln() { - + 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 = encrypted.toString(); } public void entschlüsseln() { diff --git a/src/test/java/com/ugsbo/Crypto/Crypt.java b/src/test/java/com/ugsbo/Crypto/Crypt.java index 984e97b..0c7aef3 100644 --- a/src/test/java/com/ugsbo/Crypto/Crypt.java +++ b/src/test/java/com/ugsbo/Crypto/Crypt.java @@ -5,6 +5,8 @@ package com.ugsbo.Crypto; import com.ugsbo.Crypto.*; import static org.junit.Assert.*; +import java.security.GeneralSecurityException; +import java.security.NoSuchAlgorithmException; import org.junit.Before; import org.junit.Test; @@ -25,7 +27,14 @@ public class Crypt { String ergebnis; workingobjekt.setOffen(eingabe); - workingobjekt.verschlüsseln(); + try { + workingobjekt.verschlüsseln(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (GeneralSecurityException e) { + e.printStackTrace(); + } + ergebnis = workingobjekt.getVerschlüsselt(); assertNotEquals("unterschidliche Texte",eingabe,ergebnis); @@ -38,7 +47,11 @@ public class Crypt { String ergebnis; workingobjekt.setOffen(eingabe); - workingobjekt.verschlüsseln(); + try { + workingobjekt.verschlüsseln(); + } catch (GeneralSecurityException e) { + e.printStackTrace(); + } workingobjekt.entschlüsseln(); ergebnis = workingobjekt.getOffen(); diff --git a/src/test/java/com/ugsbo/Crypto/DeCrypt.java b/src/test/java/com/ugsbo/Crypto/DeCrypt.java index 6e62c82..67fff15 100644 --- a/src/test/java/com/ugsbo/Crypto/DeCrypt.java +++ b/src/test/java/com/ugsbo/Crypto/DeCrypt.java @@ -1,7 +1,7 @@ package com.ugsbo.Crypto; import static org.junit.Assert.*; - +import java.security.GeneralSecurityException; import com.ugsbo.Crypto.*; import org.junit.Before; import org.junit.Test; @@ -37,7 +37,11 @@ public class DeCrypt { String ergebnis; workingobjekt.setOffen(eingabe); - workingobjekt.verschlüsseln(); + try { + workingobjekt.verschlüsseln(); + } catch (GeneralSecurityException e) { + e.printStackTrace(); + } workingobjekt.entschlüsseln(); ergebnis = workingobjekt.getOffen(); From a91303fbf5bedf7b1429777a3e27738ae1b74f0d Mon Sep 17 00:00:00 2001 From: Christian Baltzer Date: Sat, 13 Jul 2019 17:51:29 +0200 Subject: [PATCH 7/9] =?UTF-8?q?entschl=C3=BCsseln=20eingef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ugsbo/Crypto/Payload.java | 9 ++++++-- src/test/java/com/ugsbo/Crypto/Crypt.java | 25 ++++++++++++--------- src/test/java/com/ugsbo/Crypto/DeCrypt.java | 19 ++++++++++++---- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/ugsbo/Crypto/Payload.java b/src/main/java/com/ugsbo/Crypto/Payload.java index 36aa2c5..37ab58b 100644 --- a/src/main/java/com/ugsbo/Crypto/Payload.java +++ b/src/main/java/com/ugsbo/Crypto/Payload.java @@ -77,8 +77,13 @@ public class Payload { verschlüsselt = encrypted.toString(); } - public void entschlüsseln() { - + public void entschlüsseln() throws NoSuchAlgorithmException, GeneralSecurityException { + byte[] text = verschlüsselt.getBytes(); + + Cipher cipher2 = Cipher.getInstance("AES"); + cipher2.init(Cipher.DECRYPT_MODE, password); + byte[] cipherData2 = cipher2.doFinal(text); + offen = new String(cipherData2); } diff --git a/src/test/java/com/ugsbo/Crypto/Crypt.java b/src/test/java/com/ugsbo/Crypto/Crypt.java index 0c7aef3..83a9c26 100644 --- a/src/test/java/com/ugsbo/Crypto/Crypt.java +++ b/src/test/java/com/ugsbo/Crypto/Crypt.java @@ -5,6 +5,7 @@ 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; @@ -25,15 +26,17 @@ public class Crypt { String eingabe = "TestText"; String ergebnis; + String password = ""; - workingobjekt.setOffen(eingabe); try { - workingobjekt.verschlüsseln(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (GeneralSecurityException e) { - e.printStackTrace(); - } + workingobjekt.setOffen(eingabe); + workingobjekt.setPassword(password); + workingobjekt.entschlüsseln(); + } catch (GeneralSecurityException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } ergebnis = workingobjekt.getVerschlüsselt(); @@ -43,16 +46,18 @@ public class Crypt { @Test public void verUndEntschlüsseln() { + String password = "Test"; String eingabe = "TestText"; String ergebnis; - workingobjekt.setOffen(eingabe); try { + workingobjekt.setOffen(eingabe); + workingobjekt.setPassword(password); + workingobjekt.entschlüsseln(); workingobjekt.verschlüsseln(); - } catch (GeneralSecurityException e) { + } catch (GeneralSecurityException | UnsupportedEncodingException e) { e.printStackTrace(); } - workingobjekt.entschlüsseln(); ergebnis = workingobjekt.getOffen(); assertEquals("das entschlüsselte Test Wort",ergebnis,eingabe); diff --git a/src/test/java/com/ugsbo/Crypto/DeCrypt.java b/src/test/java/com/ugsbo/Crypto/DeCrypt.java index 67fff15..4b4f9ca 100644 --- a/src/test/java/com/ugsbo/Crypto/DeCrypt.java +++ b/src/test/java/com/ugsbo/Crypto/DeCrypt.java @@ -1,6 +1,7 @@ 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; @@ -22,8 +23,16 @@ public class DeCrypt { String password = "Test"; String ergebnis; + try { workingobjekt.setVerschlüsselt(eingabe); - workingobjekt.entschlüsseln(); + workingobjekt.setPassword(password); + workingobjekt.entschlüsseln(); + } catch (GeneralSecurityException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + ergebnis = workingobjekt.getOffen(); assertNotEquals("unterschidliche Texte",eingabe,ergebnis); @@ -36,13 +45,15 @@ public class DeCrypt { String eingabe = "TestText"; String ergebnis; - workingobjekt.setOffen(eingabe); try { + workingobjekt.setOffen(eingabe); + workingobjekt.setPassword(password); workingobjekt.verschlüsseln(); - } catch (GeneralSecurityException e) { + workingobjekt.entschlüsseln(); + } catch (GeneralSecurityException | UnsupportedEncodingException e) { e.printStackTrace(); } - workingobjekt.entschlüsseln(); + ergebnis = workingobjekt.getOffen(); assertEquals("das entschlüsselte Test Wort",ergebnis,eingabe); From 96db15434d7c07c24b9e1c71307cfcdfeb46d56d Mon Sep 17 00:00:00 2001 From: Christian Baltzer Date: Sat, 13 Jul 2019 18:11:59 +0200 Subject: [PATCH 8/9] =?UTF-8?q?main=20eingef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ugsbo/Crypto/Crypto_Logic.java | 14 +++++++++++++- src/main/java/com/ugsbo/Crypto/Payload.java | 13 +++++++------ src/test/java/com/ugsbo/Crypto/Crypt.java | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/ugsbo/Crypto/Crypto_Logic.java b/src/main/java/com/ugsbo/Crypto/Crypto_Logic.java index 08bf0c9..62f1dda 100644 --- a/src/main/java/com/ugsbo/Crypto/Crypto_Logic.java +++ b/src/main/java/com/ugsbo/Crypto/Crypto_Logic.java @@ -1,10 +1,22 @@ package com.ugsbo.Crypto; +import java.io.UnsupportedEncodingException; +import java.security.GeneralSecurityException; + public class Crypto_Logic { - public static void main(String[] args) { + 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()); } } diff --git a/src/main/java/com/ugsbo/Crypto/Payload.java b/src/main/java/com/ugsbo/Crypto/Payload.java index 37ab58b..bd2d573 100644 --- a/src/main/java/com/ugsbo/Crypto/Payload.java +++ b/src/main/java/com/ugsbo/Crypto/Payload.java @@ -5,6 +5,7 @@ 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; @@ -73,16 +74,16 @@ public class Payload { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, password); byte[] encrypted = cipher.doFinal(offen.getBytes()); - - verschlüsselt = encrypted.toString(); + + verschlüsselt = Base64.getEncoder().encodeToString(encrypted); } public void entschlüsseln() throws NoSuchAlgorithmException, GeneralSecurityException { - byte[] text = verschlüsselt.getBytes(); + byte[] text = Base64.getDecoder().decode(verschlüsselt); - Cipher cipher2 = Cipher.getInstance("AES"); - cipher2.init(Cipher.DECRYPT_MODE, password); - byte[] cipherData2 = cipher2.doFinal(text); + Cipher cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.DECRYPT_MODE, password); + byte[] cipherData2 = cipher.doFinal(text); offen = new String(cipherData2); } diff --git a/src/test/java/com/ugsbo/Crypto/Crypt.java b/src/test/java/com/ugsbo/Crypto/Crypt.java index 83a9c26..1e95a3a 100644 --- a/src/test/java/com/ugsbo/Crypto/Crypt.java +++ b/src/test/java/com/ugsbo/Crypto/Crypt.java @@ -53,8 +53,8 @@ public class Crypt { try { workingobjekt.setOffen(eingabe); workingobjekt.setPassword(password); - workingobjekt.entschlüsseln(); workingobjekt.verschlüsseln(); + workingobjekt.entschlüsseln(); } catch (GeneralSecurityException | UnsupportedEncodingException e) { e.printStackTrace(); } From 1ce94fc8ba0aab3e90906ed9524eb1429c6b38c0 Mon Sep 17 00:00:00 2001 From: Christian Baltzer Date: Sat, 13 Jul 2019 18:14:41 +0200 Subject: [PATCH 9/9] =?UTF-8?q?Alle=20tests=20gr=C3=BCn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ugsbo/Crypto/Payload.java | 41 ++++++++++---------- src/test/java/com/ugsbo/Crypto/Crypt.java | 42 ++++++++++----------- src/test/java/com/ugsbo/Crypto/DeCrypt.java | 30 +++++++-------- 3 files changed, 57 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/ugsbo/Crypto/Payload.java b/src/main/java/com/ugsbo/Crypto/Payload.java index bd2d573..a2bd031 100644 --- a/src/main/java/com/ugsbo/Crypto/Payload.java +++ b/src/main/java/com/ugsbo/Crypto/Payload.java @@ -15,11 +15,11 @@ public class Payload { String offen; String verschlüsselt; SecretKeySpec password; - + public Payload() { offen = ""; verschlüsselt = ""; - + try { this.setPassword(""); } catch (UnsupportedEncodingException e) { @@ -30,8 +30,8 @@ public class Payload { e.printStackTrace(); } } - - + + public String getOffen() { 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"); - - // 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 { 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); diff --git a/src/test/java/com/ugsbo/Crypto/Crypt.java b/src/test/java/com/ugsbo/Crypto/Crypt.java index 1e95a3a..e597fc4 100644 --- a/src/test/java/com/ugsbo/Crypto/Crypt.java +++ b/src/test/java/com/ugsbo/Crypto/Crypt.java @@ -13,9 +13,9 @@ import org.junit.Test; public class Crypt { - + private Payload workingobjekt; - + @Before public void setUp() throws Exception { workingobjekt = new Payload(); @@ -23,33 +23,33 @@ public class Crypt { @Test public void offenIstAnders() { - - String eingabe = "TestText"; + + 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(); - } - + 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); + + 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); @@ -59,8 +59,8 @@ public class Crypt { e.printStackTrace(); } ergebnis = workingobjekt.getOffen(); - - assertEquals("das entschlüsselte Test Wort",ergebnis,eingabe); + + assertEquals("das entschlüsselte Test Wort", ergebnis, eingabe); } } diff --git a/src/test/java/com/ugsbo/Crypto/DeCrypt.java b/src/test/java/com/ugsbo/Crypto/DeCrypt.java index 4b4f9ca..bcc99f4 100644 --- a/src/test/java/com/ugsbo/Crypto/DeCrypt.java +++ b/src/test/java/com/ugsbo/Crypto/DeCrypt.java @@ -10,7 +10,7 @@ import org.junit.Test; public class DeCrypt { private Payload workingobjekt; - + @Before public void setUp() throws Exception { workingobjekt = new Payload(); @@ -18,33 +18,33 @@ public class DeCrypt { @Test public void verschlüsseltIstAnders() { - - String eingabe = "TestText"; + + String eingabe = "TestText"; String password = "Test"; String ergebnis; - + try { - workingobjekt.setVerschlüsselt(eingabe); - workingobjekt.setPassword(password); + 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); + + 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); @@ -53,10 +53,10 @@ public class DeCrypt { } catch (GeneralSecurityException | UnsupportedEncodingException e) { e.printStackTrace(); } - + ergebnis = workingobjekt.getOffen(); - - assertEquals("das entschlüsselte Test Wort",ergebnis,eingabe); + + assertEquals("das entschlüsselte Test Wort", ergebnis, eingabe); } }