使用 RSA 算法包装和解开包装时出现 InvalidKeyException
我是 Java 新手,想使用 RSA 算法包装对称密钥。就我而言,我不会生成用于包装的公钥,而是从 Microsoft Keystore 检索公钥。
// Encrypt the generated Symmetric AES Key using RSA cipher
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding",
ks.getProvider().getName()); rsaCipher.init(Cipher.WRAP_MODE, RSAPubKey);
byte[] encryptedSymmKey = rsaCipher.wrap(aeskey);
我收到如下所示的 InvalidKeyException:
Exception in thread "main" java.security.InvalidKeyException: Unsupported key type: Sun RSA public key, 1024 bits
modulus: 171871587533146191561538456391418351861663300588728159334223437391061141885590024223283480319626015611710315581642512941578588886825766256507714725820048129123720143461110410353346492039350478625370269565346566901446816729164309038944197418238814947654954590754593726047828813400082450341775203029183105860831
public exponent: 65537
at sun.security.mscapi.RSACipher.init(RSACipher.java:176)
at sun.security.mscapi.RSACipher.engineInit(RSACipher.java:129)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at com.sap.srm.crpto.client.applet.CryptoClass.main(CryptoClass.java:102)
如果有人对如何正确使用 SunMSCAPI 有任何建议,请告诉我?
I am new to Java and want to wrap a symmetric key using RSA algorithm. In my case I am not generating the public key for wrapping, but retrieving the public key from Microsoft Keystore.
// Encrypt the generated Symmetric AES Key using RSA cipher
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding",
ks.getProvider().getName()); rsaCipher.init(Cipher.WRAP_MODE, RSAPubKey);
byte[] encryptedSymmKey = rsaCipher.wrap(aeskey);
I am getting an InvalidKeyException as shown below:
Exception in thread "main" java.security.InvalidKeyException: Unsupported key type: Sun RSA public key, 1024 bits
modulus: 171871587533146191561538456391418351861663300588728159334223437391061141885590024223283480319626015611710315581642512941578588886825766256507714725820048129123720143461110410353346492039350478625370269565346566901446816729164309038944197418238814947654954590754593726047828813400082450341775203029183105860831
public exponent: 65537
at sun.security.mscapi.RSACipher.init(RSACipher.java:176)
at sun.security.mscapi.RSACipher.engineInit(RSACipher.java:129)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at com.sap.srm.crpto.client.applet.CryptoClass.main(CryptoClass.java:102)
Please let me know if anyone has any suggestion how to use the SunMSCAPI appropriate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如错误消息所示,您正在使用
Sun RSA 公钥
。这意味着无论您如何检索公钥(未提供代码,所以我不知道),它都不与 MSCAPI 密钥存储关联。我的猜测是您从证书中获取公钥。因此,解决您的问题的一种方法是也使用 Sun 提供者Cipher
类来包装 AES 密钥:因此在这种情况下,无需使用 MSCAPI 来实现您的目标 - 您可以使用标准提供者。
As the error message tells you, you are using a
Sun RSA public key
. That implies that regardless of how you retrieve the public key (the code is not provided, so I don't know) it is not associated with the MSCAPI key store. My guess is you take the public key from a certificate. So one way to solve your problem is to use the Sun providerCipher
class, too, for wrapping the AES key:So in this case, there's no need to use the MSCAPI for achieving your goal - you can use the standard providers.