diff --git a/server/src/main/java/de/pietro/lusso/territory/utils/EncryptionTool.java b/server/src/main/java/de/pietro/lusso/territory/utils/EncryptionTool.java index ab66f52..4c43861 100644 --- a/server/src/main/java/de/pietro/lusso/territory/utils/EncryptionTool.java +++ b/server/src/main/java/de/pietro/lusso/territory/utils/EncryptionTool.java @@ -1,6 +1,9 @@ package de.pietro.lusso.territory.utils; import de.pietro.lusso.territory.exceptions.CryptographyException; +import de.pietro.lusso.territory.services.DatabaseService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import javax.crypto.Cipher; import javax.crypto.SecretKey; @@ -10,12 +13,15 @@ import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import java.util.Base64; public class EncryptionTool { + private static final Logger logger = LogManager.getLogger(EncryptionTool.class); + private SecretKeyFactory factory; private IvParameterSpec ivspec; @@ -30,7 +36,7 @@ private void init() { byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; ivspec = new IvParameterSpec(iv); } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); + logger.error(e); } } @@ -42,7 +48,7 @@ public String encrypt(String password, String strToEncrypt) throws CryptographyE return Base64.getEncoder() .encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8))); } catch (Exception e) { - System.out.println("Error while encrypting: " + e.toString()); + logger.error("Error while encrypting: " , e); throw new CryptographyException("Error while encrypting", e); } } @@ -54,7 +60,7 @@ public String decrypt(String password, String strToDecrypt) { cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivspec); return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); } catch (Exception e) { - System.out.println("Error while decrypting: " + e.toString()); + logger.error("Error while decrypting: " ,e); } return null; } @@ -78,4 +84,29 @@ private static String[] splitPassword(String password) { String salt = password.substring(password.length() / 2); return new String[]{secretKey,salt}; } + + public String generateStrongSecret(int keyLength, boolean encodeInHex) { + try { + SecureRandom secureRandom = new SecureRandom(); + byte[] secretBytes = new byte[keyLength / 8]; + secureRandom.nextBytes(secretBytes); + + if (encodeInHex) { + return bytesToHex(secretBytes); + } else { + return new String(secretBytes); + } + } catch (Exception e) { + logger.error(e); + return null; + } + } + + private static String bytesToHex(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + for (byte b : bytes) { + sb.append(String.format("%02x", b)); + } + return sb.toString(); + } } diff --git a/server/src/test/java/de/pietro/lusso/territory/services/EncryptionTest.java b/server/src/test/java/de/pietro/lusso/territory/services/EncryptionTest.java index 2eeb033..fffec31 100644 --- a/server/src/test/java/de/pietro/lusso/territory/services/EncryptionTest.java +++ b/server/src/test/java/de/pietro/lusso/territory/services/EncryptionTest.java @@ -7,9 +7,9 @@ public class EncryptionTest { - private String input = "JSON bla bla 123 abc"; + private String input = "{\"JSON\":\"bla bla 123 abc\"}"; private String password = "871a5c07-5c2d-41bd-98af-bb8cbdb06519cd185d47-bd50-4325-a599-a1a80d91924a"; - private String cipherText = "31kWl3RdPP5X9QVcN+Tb12bu1yofdvkoglLfD4oakbc="; + private String cipherText = "pxlPTUr6OpYqwr8+Q4y1Fdeins+5SUIaMST9R8uENAA="; private EncryptionTool encryptionTool = new EncryptionTool(); @Test @@ -22,19 +22,25 @@ public void testEncryptionAES() throws CryptographyException { System.out.println("cipherText = " + cipherText2); Assertions.assertEquals(cipherText, cipherText2); - String plainText = encryptionTool.decrypt(password, cipherText2); - System.out.println("encrypted = " + plainText); + String decrypted = encryptionTool.decrypt(password, cipherText2); + System.out.println("decrypted = " + decrypted); - Assertions.assertEquals(input, plainText); + Assertions.assertEquals(input, decrypted); } @Test public void testDecryptionAES() { System.out.println("cipherText = " + cipherText); - String plainText = encryptionTool.decrypt(password, cipherText); - System.out.println("encrypted = " + plainText); - Assertions.assertEquals("JSON bla bla 123 abc", plainText); + String decrypted = encryptionTool.decrypt(password, cipherText); + System.out.println("decrypted = " + decrypted); + Assertions.assertEquals(input, decrypted); + } + + @Test + public void testGenerateStrongSecret() { + String strongSecret = encryptionTool.generateStrongSecret(256, true); + System.out.println(strongSecret); } }