gpt4 book ai didi

java - 错误填充异常 - pkcs11 中的 RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING

转载 作者:行者123 更新时间:2023-12-03 23:05:40 26 4
gpt4 key购买 nike

我的应用程序正在访问电子 token 以解密来自服务器的响应

来自服务器的 session key 使用以下方法加密:-

RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING

我正在使用 SunPKCS11 Provider 来实现对加密 token 的访问。每当我尝试使用上述机制解密时,即使用 RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING 我得到:-

**javax.crypto.BadPaddingException: doFinal() failed  
at sun.security.pkcs11.P11RSACipher.implDoFinal(P11RSACipher.java:328)
at sun.security.pkcs11.P11RSACipher.engineDoFinal(P11RSACipher.java:353)
at javax.crypto.Cipher.doFinal(DashoA13*..)

以下是我的代码:-

private static final String TRANSFORMATION = "RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING";
private static final String SECURITY_PROVIDER = "BC";
private static final String DIGEST_ALGORITHM = "SHA-256";
private static final String MASKING_FUNCTION = "MGF1";

出现错误的代码片段如下:-

private byte[] decryptSecretKeyData(byte[] encryptedSecretKey, byte[] iv, PrivateKey privateKey) throws Exception {

try {
Cipher rsaCipher = Cipher.getInstance(TRANSFORMATION, SECURITY_PROVIDER);

System.out.println("Cipher block initialized"); - **Printed**
PSource pSrc = (new PSource.PSpecified(iv));
System.out.println("PSource inisitialized"); - **Printed**


rsaCipher.init(Cipher.DECRYPT_MODE, privateKey,
new OAEPParameterSpec(DIGEST_ALGORITHM, MASKING_FUNCTION,
MGF1ParameterSpec.SHA256, pSrc));


System.out.println("Here after cipher initilaization"); - **Not Printed***

return rsaCipher.doFinal(encryptedSecretKey);
} catch (GeneralSecurityException e) {
System.out.println("GeneralSecurityException is "+e.getMessage());
throw new Exception("Failed to decrypt AES secret key using RSA.", e);
}
}

我被困在这里,无法解密 OAEP 转换。

最佳答案

您从加密狗中获取不可提取的私钥 P11Key.P11PrivateKey。它不能在 PKCS11 提供程序之外使用,因此,应该使用 SunPKCS11 提供程序来使用该 key 进行操作。

不幸的是,SunPKCS11 提供程序不支持 OAEP 填充,这使得它变得更加困难。加密仍然可以使用 BouncyCaSTLe 完成,但解密可以在没有填充和 SunPKCS11 提供程序的情况下完成。 keyLength 参数是以位为单位的 RSA key 模数长度(1024、2048 等)。

private void testEncryption(byte[] plainText, PrivateKey privateKey, PublicKey publicKey, int keyLength) throws GeneralSecurityException {

System.out.println("Plain text: " + DatatypeConverter.printHexBinary(plainText));

Provider bcProvider = new BouncyCastleProvider();
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING", bcProvider);
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = rsaCipher.doFinal(plainText);

System.out.println("Cipher text: " + DatatypeConverter.printHexBinary(cipherText));

Provider pkcs11provider = new SunPKCS11("C:\\Users\\manishs525\\pkcs11.cfg");
Cipher rsaCipher2 = Cipher.getInstance("RSA/ECB/NoPadding", pkcs11provider);
rsaCipher2.init(Cipher.DECRYPT_MODE, privateKey);
byte[] paddedPlainText = rsaCipher2.doFinal(cipherText);

/* Ensure leading zeros not stripped */
if (paddedPlainText.length < keyLength / 8) {
byte[] tmp = new byte[keyLength / 8];
System.arraycopy(paddedPlainText, 0, tmp, tmp.length - paddedPlainText.length, paddedPlainText.length);
System.out.println("Zero padding to " + (keyLength / 8));
paddedPlainText = tmp;
}

System.out.println("OAEP padded plain text: " + DatatypeConverter.printHexBinary(paddedPlainText));

OAEPParameterSpec paramSpec = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA1,
PSource.PSpecified.DEFAULT);
RSAPadding padding = RSAPadding.getInstance(RSAPadding.PAD_OAEP_MGF1, keyLength / 8, new SecureRandom(), paramSpec);
byte[] plainText2 = padding.unpad(paddedPlainText);

System.out.println("Unpadded plain text: " + DatatypeConverter.printHexBinary(plainText2));
}

注意事项:

  • JDK1.7 之前的 SunPKCS11 未实现 RSA/ECB/NoPadding。
  • 此示例使用 BouncyCaSTLe 1.50 和 JDK 1.7 进行了测试

关于java - 错误填充异常 - pkcs11 中的 RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23844694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com