我在应用程序中使用了加密功能。我将私钥存储为字节数组,并使用以下代码将其还原:

PrivateKey private = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(s_privateKeyIn1t));

它可以在我所有的目标android平台2.1-> 4.0.4上完美运行,但是在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)

怎么了?

最佳答案

对我有用的This is the code(第二行是重要部分):

PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(s_privateKeyIn1t);
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
PrivateKey privateKey = keyFactory.generatePrivate(privSpec);

07-28 12:42