错误:0D0680A8:asn1编码例程:ASN1_CHECK_TLEN:导入RSA公钥时出错标记

时间:2014-03-19 21:36:18

标签: java android encryption

我已经生成了一对RSA密钥(公共和私有)。现在出于测试目的,我试图将String表示中的公钥导入PublicKey以在Android项目中使用它,以便将RSA中的加密消息发送到远程服务器,之后将会使用私钥解密它们。

public static String encryptDataRSA(final String data) throws IOException {  
  final byte[] dataToEncrypt = data.getBytes();
  byte[] encryptedData = null;  

  try {
    final String keyStr = "-----BEGIN PUBLIC KEY-----\n" +
                            "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdQudusozLmogBfU2LCO+WcM59\n" +
                            "ycup9SxMsBNCku23PxrPMO6u//QjtWPz7istE9vkQfa6tQn1Or+SDxeHLMxEesF0\n" +
                            "xiBEgFUhg7vjOF2SnFQQEADgUyizUIBBn1UgKNA8eP24Ux0P0M2aHMn78HIHsRcu\n" +
                            "pNGUNW7p51HOVoIPJQIDAQAB\n" +
                            "-----END PUBLIC KEY-----";

    PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyStr.getBytes()));

    final Cipher cipher = Cipher.getInstance("RSA");  
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
    encryptedData = cipher.doFinal(dataToEncrypt); 

    try {
      final String encryptedText = new String(Base64.encode(encryptedData, Base64.DEFAULT), "UTF-8");
      return encryptedText.toString();
    } 
    catch (final UnsupportedEncodingException e1) { return null; }      
  } catch (Exception e) { e.printStackTrace(); }     

  return "ERROR";
}

问题是这会返回以下异常:

03-19 21:14:31.449: W/System.err(2713): java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
03-19 21:14:31.480: W/System.err(2713):     at org.apache.harmony.xnet.provider.jsse.OpenSSLKey.getPublicKey(OpenSSLKey.java:89)
03-19 21:14:31.480: W/System.err(2713):     at org.apache.harmony.xnet.provider.jsse.OpenSSLRSAKeyFactory.engineGeneratePublic(OpenSSLRSAKeyFactory.java:47)
03-19 21:14:31.489: W/System.err(2713):     at java.security.KeyFactory.generatePublic(KeyFactory.java:171)
03-19 21:14:31.489: W/System.err(2713):     at com.mydomain.myproject.SecurityTools.encryptDataRSA(SecurityTools.java:85)
03-19 21:14:31.501: W/System.err(2713):     at com.mydomain.myproject.MainActivity.onCreate(MainActivity.java:93)
03-19 21:14:31.501: W/System.err(2713):     at android.app.Activity.performCreate(Activity.java:5133)
03-19 21:14:31.509: W/System.err(2713):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-19 21:14:31.521: W/System.err(2713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-19 21:14:31.521: W/System.err(2713):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-19 21:14:31.529: W/System.err(2713):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-19 21:14:31.529: W/System.err(2713):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-19 21:14:31.541: W/System.err(2713):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 21:14:31.541: W/System.err(2713):     at android.os.Looper.loop(Looper.java:137)
03-19 21:14:31.561: W/System.err(2713):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-19 21:14:31.571: W/System.err(2713):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 21:14:31.581: W/System.err(2713):     at java.lang.reflect.Method.invoke(Method.java:525)
03-19 21:14:31.592: W/System.err(2713):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-19 21:14:31.601: W/System.err(2713):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-19 21:14:31.609: W/System.err(2713):     at dalvik.system.NativeStart.main(Native Method)
03-19 21:14:31.621: W/System.err(2713): Caused by: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
03-19 21:14:31.649: W/System.err(2713):     at org.apache.harmony.xnet.provider.jsse.NativeCrypto.d2i_PUBKEY(Native Method)
03-19 21:14:31.649: W/System.err(2713):     at org.apache.harmony.xnet.provider.jsse.OpenSSLKey.getPublicKey(OpenSSLKey.java:87)
03-19 21:14:31.663: W/System.err(2713):     ... 18 more

此行的例外情况:cipher.init(Cipher.ENCRYPT_MODE, publicKey);

为什么这会返回此异常以及如何解决它?感谢。

2 个答案:

答案 0 :(得分:19)

X509EncodedKeySpec期望DER编码数据,而不是PEM编码。如果你简单地丢弃" BEGIN"你的代码将正常工作。和"结束"和base64解码公钥内容。

final String key2 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdQudusozLmogBfU2LCO+WcM59"
    + "ycup9SxMsBNCku23PxrPMO6u//QjtWPz7istE9vkQfa6tQn1Or+SDxeHLMxEesF0"
    + "xiBEgFUhg7vjOF2SnFQQEADgUyizUIBBn1UgKNA8eP24Ux0P0M2aHMn78HIHsRcu"
    + "pNGUNW7p51HOVoIPJQIDAQAB";

PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(
    new X509EncodedKeySpec(DatatypeConverter.parseBase64Binary(key2)));

答案 1 :(得分:1)

在我的情况下,公钥包含在证书中,我必须做一些自然的事情:

CertificateFactory factory = CertificateFactory.getInstance("X.509");
try {           
       X509Certificate cer = (X509Certificate) factory.generateCertificate(inputStream);
       publicKey = cer.getPublicKey();
       inputStream.close();
} catch (IOException e) {
       e.printStackTrace();
}

注意:我的文件以----- BEGIN CERTIFICATE ----

开头