diff --git a/src/main/java/it/eng/idsa/businesslogic/entity/AuditLog.java b/src/main/java/it/eng/idsa/businesslogic/entity/AuditLog.java index 72a2804f..f4427bca 100644 --- a/src/main/java/it/eng/idsa/businesslogic/entity/AuditLog.java +++ b/src/main/java/it/eng/idsa/businesslogic/entity/AuditLog.java @@ -1,6 +1,7 @@ package it.eng.idsa.businesslogic.entity; import java.time.LocalDateTime; +import it.eng.idsa.businesslogic.util.AES256; import javax.persistence.Column; import javax.persistence.Entity; @@ -11,8 +12,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; -import it.eng.idsa.businesslogic.util.AES256Static; - @Entity @Table(name = "AuditLogs") public class AuditLog { @@ -30,7 +29,7 @@ public AuditLog() { } public AuditLog(String event) { - this.event = AES256Static.encrypt(event); + this.event = AES256.encrypt(event); this.timestamp = LocalDateTime.now(); } diff --git a/src/main/java/it/eng/idsa/businesslogic/service/AuditEventService.java b/src/main/java/it/eng/idsa/businesslogic/service/AuditEventService.java index f931020f..6f867ba8 100644 --- a/src/main/java/it/eng/idsa/businesslogic/service/AuditEventService.java +++ b/src/main/java/it/eng/idsa/businesslogic/service/AuditEventService.java @@ -11,7 +11,7 @@ import it.eng.idsa.businesslogic.entity.AuditLog; import it.eng.idsa.businesslogic.repository.AuditEventRepository; -import it.eng.idsa.businesslogic.util.AES256Static; +import it.eng.idsa.businesslogic.util.AES256; @Service public class AuditEventService { @@ -42,7 +42,7 @@ public List getAuditEventsForDate(LocalDate date) { private AuditLog decryptAuditLog(AuditLog auditLog) { AuditLog a = new AuditLog(); a.setId(auditLog.getId()); - a.setEvent(AES256Static.decrypt(auditLog.getEvent())); + a.setEvent(AES256.decrypt(auditLog.getEvent())); a.setTimestamp(auditLog.getTimestamp()); return a; } diff --git a/src/main/java/it/eng/idsa/businesslogic/util/AES256.java b/src/main/java/it/eng/idsa/businesslogic/util/AES256.java index c19a93f8..33f452d9 100644 --- a/src/main/java/it/eng/idsa/businesslogic/util/AES256.java +++ b/src/main/java/it/eng/idsa/businesslogic/util/AES256.java @@ -82,59 +82,4 @@ public static String decrypt(String strToDecrypt) { return null; } } - - /* - public static String encrypt(String strToEncrypt) { - try { - SecureRandom secureRandom = new SecureRandom(); - byte[] iv = new byte[16]; - secureRandom.nextBytes(iv); - IvParameterSpec ivspec = new IvParameterSpec(iv); - - SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); - KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), ITERATION_COUNT, KEY_LENGTH); - SecretKey tmp = factory.generateSecret(spec); - SecretKeySpec secretKeySpec = new SecretKeySpec(tmp.getEncoded(), "AES"); - - Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); - cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivspec); - - byte[] cipherText = cipher.doFinal(strToEncrypt.getBytes("UTF-8")); - byte[] encryptedData = new byte[iv.length + cipherText.length]; - System.arraycopy(iv, 0, encryptedData, 0, iv.length); - System.arraycopy(cipherText, 0, encryptedData, iv.length, cipherText.length); - - return Base64.getEncoder().encodeToString(encryptedData); - } catch (Exception e) { - logger.error("Error while encrypting", e); - return null; - } - } - - public static String decrypt(String strToDecrypt) { - try { - byte[] encryptedData = Base64.getDecoder().decode(strToDecrypt); - byte[] iv = new byte[16]; - System.arraycopy(encryptedData, 0, iv, 0, iv.length); - IvParameterSpec ivspec = new IvParameterSpec(iv); - - SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); - KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), ITERATION_COUNT, KEY_LENGTH); - SecretKey tmp = factory.generateSecret(spec); - SecretKeySpec secretKeySpec = new SecretKeySpec(tmp.getEncoded(), "AES"); - - Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); - cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivspec); - - byte[] cipherText = new byte[encryptedData.length - 16]; - System.arraycopy(encryptedData, 16, cipherText, 0, cipherText.length); - - byte[] decryptedText = cipher.doFinal(cipherText); - return new String(decryptedText, "UTF-8"); - } catch (Exception e) { - logger.error("Error while decrypting", e); - return null; - } - } - */ } diff --git a/src/test/java/it/eng/idsa/businesslogic/util/AES256Test.java b/src/test/java/it/eng/idsa/businesslogic/util/AES256Test.java new file mode 100644 index 00000000..b7cb24c5 --- /dev/null +++ b/src/test/java/it/eng/idsa/businesslogic/util/AES256Test.java @@ -0,0 +1,21 @@ +package it.eng.idsa.businesslogic.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.Test; + +public class AES256Test { + + private String originalString = "String used to test AES256 encryption/decryption"; + + @Test + public void encryptDecrypt() { + String encrypted = AES256.encrypt(originalString); + assertNotNull(encrypted); + System.out.println(encrypted); + String decrypted = AES256.decrypt(encrypted); + assertEquals(originalString, decrypted); + System.out.println(decrypted); + } +}