From 27d98785de0085bd46ac0f77d9396aab612a8d99 Mon Sep 17 00:00:00 2001 From: RiccardoGiuliani Date: Mon, 12 Feb 2024 13:00:08 +0100 Subject: [PATCH] Test fix padding --- .../idpay/azurekeyvault/util/EncryptUtil.java | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) 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 fd1ed03..9341770 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 @@ -3,24 +3,21 @@ import it.pagopa.swclient.mil.idpay.bean.PublicKeyIDPay; import jakarta.enterprise.context.ApplicationScoped; +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.security.*; import java.security.spec.InvalidKeySpecException; -import java.security.spec.MGF1ParameterSpec; import java.security.spec.RSAPublicKeySpec; import java.util.Base64; -import javax.crypto.BadPaddingException; -import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.spec.OAEPParameterSpec; -import javax.crypto.spec.PSource; @ApplicationScoped public class EncryptUtil { - public String encryptSessionKeyForIdpay(PublicKeyIDPay publicKeyIDPay, String sessionKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { + public String encryptSessionKeyForIdpay(PublicKeyIDPay publicKeyIDPay, String sessionKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { String modulusBase64 = publicKeyIDPay.getN(); String exponentBase64 = publicKeyIDPay.getE(); @@ -38,10 +35,8 @@ public String encryptSessionKeyForIdpay(PublicKeyIDPay publicKeyIDPay, String se KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey rsaPublicKey = keyFactory.generatePublic(rsaPublicKeySpec); - 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); + Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); + cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey); byte[] sessionKeyBytes = sessionKey.getBytes(StandardCharsets.UTF_8); byte[] encryptedSessionKeyBytes = cipher.doFinal(sessionKeyBytes);