Skip to content

Commit

Permalink
Deleted unused files
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorBalog-Eng committed Feb 19, 2024
1 parent bba7510 commit 0ba4d59
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 60 deletions.
5 changes: 2 additions & 3 deletions src/main/java/it/eng/idsa/businesslogic/entity/AuditLog.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,8 +12,6 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import it.eng.idsa.businesslogic.util.AES256Static;

@Entity
@Table(name = "AuditLogs")
public class AuditLog {
Expand All @@ -30,7 +29,7 @@ public AuditLog() {
}

public AuditLog(String event) {
this.event = AES256Static.encrypt(event);
this.event = AES256.encrypt(event);
this.timestamp = LocalDateTime.now();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -42,7 +42,7 @@ public List<AuditLog> 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;
}
Expand Down
55 changes: 0 additions & 55 deletions src/main/java/it/eng/idsa/businesslogic/util/AES256.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
*/
}
21 changes: 21 additions & 0 deletions src/test/java/it/eng/idsa/businesslogic/util/AES256Test.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 0ba4d59

Please sign in to comment.