在Android Jelly Bean上恢复RSA私钥时出错

Dmi*_*hin 3 java android openssl rsa android-4.2-jelly-bean

我在我的应用程序中使用了encription.我将私钥存储为字节数组并使用以下代码来恢复它:

PrivateKey private = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(s_privateKeyIn1t));
Run Code Online (Sandbox Code Playgroud)

它适用于我所有的目标Android平台2.1 - > 4.0.4,但在Jelly Bean上失败了!

Jelly Bean引发了一个例外:

07-20 17:29:35.197: E/AnyBalance:Codec(990): Caused by: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
07-20 17:29:35.197: E/AnyBalance:Codec(990):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.d2i_PKCS8_PRIV_KEY_INFO(Native Method)
07-20 17:29:35.197: E/AnyBalance:Codec(990):    at org.apache.harmony.xnet.provider.jsse.OpenSSLRSAKeyFactory.engineGeneratePrivate(OpenSSLRSAKeyFactory.java:73)
Run Code Online (Sandbox Code Playgroud)

怎么了?

小智 18

这是适合我的代码(第二行是重要的部分):

PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(s_privateKeyIn1t);
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
PrivateKey privateKey = keyFactory.generatePrivate(privSpec);
Run Code Online (Sandbox Code Playgroud)

  • KeyFactory kf = KeyFactory.getInstance(“ RSA”,“ BC”); 工作。你救了我的日子。 (3认同)
  • “ BC”是加密服务提供商。指的是一个包或一组包,它们提供JDK Security API加密功能的子集的具体实现。 (2认同)
  • 用“BC”拯救了我的一天 (2认同)