made encryption much more general
This commit is contained in:
parent
d647ecb8a1
commit
2926b5a9f4
|
@ -61,7 +61,7 @@ public class SecretController {
|
|||
Secret secret = new Secret(
|
||||
null,
|
||||
user.getId(),
|
||||
new EncryptUtil(newSecret.getEncryptPassword()).encrypt(newSecret.getContent())
|
||||
new EncryptUtil(newSecret.getEncryptPassword()).encrypt(newSecret.getContent().toString())
|
||||
);
|
||||
//save secret in db
|
||||
secretService.createSecret(secret);
|
||||
|
@ -195,7 +195,7 @@ public class SecretController {
|
|||
Secret secret = new Secret(
|
||||
secretId,
|
||||
user.getId(),
|
||||
new EncryptUtil(newSecret.getEncryptPassword()).encrypt(newSecret.getContent())
|
||||
new EncryptUtil(newSecret.getEncryptPassword()).encrypt(newSecret.getContent().toString())
|
||||
);
|
||||
Secret updatedSecret = secretService.updateSecret(secret);
|
||||
//save secret in db
|
||||
|
|
|
@ -31,19 +31,12 @@ public class EncryptUtil {
|
|||
this.secretKey = generateKey(secretKey);
|
||||
}
|
||||
|
||||
public String encrypt(JsonNode content) {
|
||||
return changeRelevantValues(content, str -> encryptString(str, secretKey));
|
||||
public String encrypt(String content) {
|
||||
return encryptString(content, secretKey);
|
||||
}
|
||||
|
||||
public String decrypt(String data) {
|
||||
|
||||
try {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode jsonNode = mapper.readTree(data);
|
||||
return changeRelevantValues(jsonNode, str -> decryptString(str, secretKey));
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return decryptString(data, secretKey);
|
||||
}
|
||||
|
||||
private String changeRelevantValues(JsonNode content, Function<String, String> encryptOrDecrypt) {
|
||||
|
@ -67,7 +60,6 @@ public class EncryptUtil {
|
|||
try {
|
||||
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
|
||||
|
||||
// Generate a random IV for CBC mode
|
||||
byte[] iv = new byte[16];
|
||||
SecureRandom.getInstanceStrong().nextBytes(iv);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
|
@ -75,7 +67,6 @@ public class EncryptUtil {
|
|||
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
|
||||
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
|
||||
|
||||
// Prepend the IV to the ciphertext (IV doesn't need to be secret)
|
||||
byte[] combined = new byte[iv.length + encryptedBytes.length];
|
||||
System.arraycopy(iv, 0, combined, 0, iv.length);
|
||||
System.arraycopy(encryptedBytes, 0, combined, iv.length, encryptedBytes.length);
|
||||
|
@ -94,12 +85,10 @@ public class EncryptUtil {
|
|||
throw new IllegalArgumentException("Invalid ciphertext (too short)");
|
||||
}
|
||||
|
||||
// Extract IV (first 16 bytes)
|
||||
byte[] iv = new byte[16];
|
||||
System.arraycopy(combined, 0, iv, 0, iv.length);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
|
||||
// Extract ciphertext (remaining bytes)
|
||||
byte[] ciphertext = new byte[combined.length - iv.length];
|
||||
System.arraycopy(combined, iv.length, ciphertext, 0, ciphertext.length);
|
||||
|
||||
|
|
Loading…
Reference in New Issue