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