Skip to content
This repository was archived by the owner on Aug 12, 2023. It is now read-only.

Commit

Permalink
Move concatenation and splitting to own method
Browse files Browse the repository at this point in the history
  • Loading branch information
svenpopping authored and Julian committed Jan 7, 2022
1 parent 78b4147 commit b71f500
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/main/java/ch/wisv/converters/AbstractCryptoConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import javax.crypto.NoSuchPaddingException;
import javax.persistence.AttributeConverter;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

/**
* AbstractCryptoConverter class.
Expand Down Expand Up @@ -52,7 +54,7 @@ public String convertToDatabaseColumn(T attribute) {
try {
Cipher cipher = cipherInitializer.prepareAndInitCipher(Cipher.ENCRYPT_MODE, DATABASE_ENCRYPTION_KEY, null);

return this.encrypt(cipher, attribute) + CONCATENATION + Base64.getEncoder().encodeToString(cipher.getIV());
return this.concatenatedCipherTextAndIv(this.encrypt(cipher, attribute), cipher.getIV());
} catch (NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException | BadPaddingException |
NoSuchPaddingException | IllegalBlockSizeException e) {
throw new RuntimeException(e);
Expand All @@ -73,13 +75,10 @@ public String convertToDatabaseColumn(T attribute) {
public T convertToEntityAttribute(String dbData) {
if (isNotEmpty(DATABASE_ENCRYPTION_KEY) && isNotEmpty(dbData)) {
try {
String[] splitDbData = dbData.split(CONCATENATION);
String cipherText = splitDbData[0];
byte[] iv = Base64.getDecoder().decode(splitDbData[1]);
Pair<String, byte[]> cipherTextAndIv = this.splitDbData(dbData);
Cipher cipher = cipherInitializer.prepareAndInitCipher(Cipher.DECRYPT_MODE, DATABASE_ENCRYPTION_KEY, cipherTextAndIv.getRight());

Cipher cipher = cipherInitializer.prepareAndInitCipher(Cipher.DECRYPT_MODE, DATABASE_ENCRYPTION_KEY, iv);

return this.decrypt(cipher, cipherText);
return this.decrypt(cipher, cipherTextAndIv.getLeft());
} catch (NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException | BadPaddingException |
NoSuchPaddingException | IllegalBlockSizeException e) {
throw new RuntimeException(e);
Expand All @@ -89,6 +88,33 @@ public T convertToEntityAttribute(String dbData) {
return this.stringToEntityAttribute(dbData);
}

/**
* Concatenated cipher text and IV together.
*
* @param cipherText of type String
* @param iv of type byte[]
*
* @return String
*/
private String concatenatedCipherTextAndIv(String cipherText, byte[] iv) {
return cipherText + CONCATENATION + Base64.getEncoder().encodeToString(iv);
}

/**
* Split db data into cipher text and IV.
*
* @param dbData of type String
*
* @return Pair
*/
private Pair<String, byte[]> splitDbData(String dbData) {
String[] splitDbData = dbData.split(CONCATENATION);
String cipherText = splitDbData[0];
byte[] iv = Base64.getDecoder().decode(splitDbData[1]);

return new ImmutablePair<>(cipherText, iv);
}

/**
* Do final Cipher call.
*
Expand Down

0 comments on commit b71f500

Please sign in to comment.