Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update deprecated usage of encrypt and decrypt #514

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/main/java/com/michelin/ns4kafka/util/EncryptionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.nimbusds.jose.JWEHeader;
import com.nimbusds.jose.crypto.AESDecrypter;
import com.nimbusds.jose.crypto.AESEncrypter;
import com.nimbusds.jose.crypto.impl.AAD;
import com.nimbusds.jose.util.Base64URL;
import io.micronaut.core.util.StringUtils;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -86,10 +87,13 @@ public static String encryptAes256Gcm(String clearText, String key) {
return clearText;
}

JWEHeader header = new JWEHeader(JWEAlgorithm.A256KW, EncryptionMethod.A256GCM);
AESEncrypter encrypter = new AESEncrypter(key.getBytes(StandardCharsets.UTF_8));
JWECryptoParts encryptedData =
encrypter.encrypt(new JWEHeader(JWEAlgorithm.A256KW, EncryptionMethod.A256GCM),
clearText.getBytes(StandardCharsets.UTF_8));
JWECryptoParts encryptedData = encrypter.encrypt(
header,
clearText.getBytes(StandardCharsets.UTF_8),
AAD.compute(header)
);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(encryptedData.getEncryptedKey().decode());
Expand Down Expand Up @@ -126,8 +130,8 @@ public static String decryptAes256Gcm(String encryptedText, String key) {
Base64URL auth = Base64URL.encode(Arrays.copyOfRange(encryptedData, 52, 68));
Base64URL text = Base64URL.encode(Arrays.copyOfRange(encryptedData, 68, encryptedData.length));

byte[] clearTextAsBytes = decrypter.decrypt(new JWEHeader(JWEAlgorithm.A256KW, EncryptionMethod.A256GCM),
encryptedKey, iv, text, auth);
JWEHeader header = new JWEHeader(JWEAlgorithm.A256KW, EncryptionMethod.A256GCM);
byte[] clearTextAsBytes = decrypter.decrypt(header, encryptedKey, iv, text, auth, AAD.compute(header));

return new String(clearTextAsBytes);
} catch (JOSEException e) {
Expand Down