我在我的应用中使用了encription。我将私钥存储为字节数组并使用以下代码来恢复它:
PrivateKey private = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(s_privateKeyIn1t));
它适用于我所有的目标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)
有什么问题?
答案 0 :(得分:15)
This is the code对我有用(第二行是重要部分):
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(s_privateKeyIn1t);
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
PrivateKey privateKey = keyFactory.generatePrivate(privSpec);
答案 1 :(得分:2)
嗯,我不知道它发生的原因,但我已经想出如何处理它。我刚刚在以前的Android版本上重新编码了密钥,这个重新编码的密钥在Jelly Bean上运行。
我使用以下代码重新编码密钥:
Private key = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(s_privateKeyIn1t));
byte [] xxx = s_privateKey.getEncoded(); //Then I watched this byte array in debugger and inserted it in a source code.
//Now it works on Jelly Bean