使用 RSA 算法包装和解开包装时出现 InvalidKeyException

发布于 2024-11-24 08:41:29 字数 1230 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦旅人picnic 2024-12-01 08:41:29

正如错误消息所示,您正在使用 Sun RSA 公钥。这意味着无论您如何检索公钥(未提供代码,所以我不知道),它都不与 MSCAPI 密钥存储关联。我的猜测是您从证书中获取公钥。因此,解决您的问题的一种方法是也使用 Sun 提供者 Cipher 类来包装 AES 密钥:

Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsaCipher.init(Cipher.WRAP_MODE, RSAPubKey);
byte[] encryptedSymmKey = rsaCipher.wrap(aeskey);

因此在这种情况下,无需使用 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 provider Cipher class, too, for wrapping the AES key:

Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsaCipher.init(Cipher.WRAP_MODE, RSAPubKey);
byte[] encryptedSymmKey = rsaCipher.wrap(aeskey);

So in this case, there's no need to use the MSCAPI for achieving your goal - you can use the standard providers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文