diff --git a/src/main/java/it/pagopa/swclient/mil/idpay/azurekeyvault/util/EncryptUtil.java b/src/main/java/it/pagopa/swclient/mil/idpay/azurekeyvault/util/EncryptUtil.java index fb34c02..dee9ba7 100644 --- a/src/main/java/it/pagopa/swclient/mil/idpay/azurekeyvault/util/EncryptUtil.java +++ b/src/main/java/it/pagopa/swclient/mil/idpay/azurekeyvault/util/EncryptUtil.java @@ -7,19 +7,19 @@ import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; +import javax.crypto.spec.OAEPParameterSpec; +import javax.crypto.spec.PSource; import java.math.BigInteger; -import java.security.InvalidKeyException; -import java.security.KeyFactory; -import java.security.NoSuchAlgorithmException; -import java.security.PublicKey; +import java.security.*; import java.security.spec.InvalidKeySpecException; +import java.security.spec.MGF1ParameterSpec; import java.security.spec.RSAPublicKeySpec; import java.util.Base64; @ApplicationScoped public class EncryptUtil { - public String encryptSessionKeyForIdpay(PublicKeyIDPay publicKeyIDPay, String sessionKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { + public String encryptSessionKeyForIdpay(PublicKeyIDPay publicKeyIDPay, String sessionKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { String modulusBase64 = publicKeyIDPay.getN(); String exponentBase64 = publicKeyIDPay.getE(); @@ -37,8 +37,10 @@ public String encryptSessionKeyForIdpay(PublicKeyIDPay publicKeyIDPay, String se KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey rsaPublicKey = keyFactory.generatePublic(rsaPublicKeySpec); - Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); - cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey); + Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding"); + OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", + new MGF1ParameterSpec("SHA-256"), PSource.PSpecified.DEFAULT); + cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey, oaepParams); byte[] sessionKeyBytes = decodeBase64UrlOrBase64(sessionKey); byte[] encryptedSessionKeyBytes = cipher.doFinal(sessionKeyBytes);