【发布时间】:2014-07-13 17:25:47
【问题描述】:
我的应用程序正在访问 e-Token 以解密来自服务器的响应
来自服务器的会话密钥使用 :-
加密RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING
我正在使用 SunPKCS11 Provider 来实现对加密令牌的访问。 每当我尝试使用上述机制解密它时,即使用 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";
报错的代码sn-p如下:-
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 转换。
【问题讨论】:
-
检查privateKey.getClass().toString()的值;知道那是什么钥匙。查看异常堆栈跟踪也非常有用。
-
我得到 sun.security.pkcs11.P11Key$P11PrivateKey 作为 privateKey.getClass().toString() 的值,而 privateKy.getAlgorithm() 的值是 RSA。
-
BouncyCastle 必须是
P11Key.P11RSAPrivateKey才能使用它。这可能是关于如何创建密钥库的问题。 -
我的私钥与 P11Key$P11PrivateKey 相同,我正在创建 Keystore 作为 ks = KeyStore.getInstance("PKCS11", UserProvider);其中 Userprovider 是私有静态 final Provider UserProvider = new sun.security.pkcs11.SunPKCS11("C:\\Users\\manishs525\\pkcs11.cfg");
标签: java encryption bouncycastle smartcard pkcs#11