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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22518608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:13:09  来源:igfitidea点击:

error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag when importing RSA public key

javaandroidencryption

提问by nKn

I've generated a RSA pair of keys (public and private). Now for testing purposes I'm trying to import the public key in Stringrepresentation into a PublicKeyto use it within an Android project, in order to send crypted messages in RSA to a remote server which will afterwards decrypt them using the private key.

我生成了一对 RSA 密钥(公钥和私钥)。现在出于测试目的,我试图将String表示形式的公钥导入 aPublicKey以在 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";
}

The problem is that this is returning the following exception:

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

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.access0(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

The exception points to this line: cipher.init(Cipher.ENCRYPT_MODE, publicKey);

异常指向这一行: cipher.init(Cipher.ENCRYPT_MODE, publicKey);

Why is this returning this exception and how to solve it? Thanks.

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

采纳答案by Duncan Jones

X509EncodedKeySpecexpects DER-encoded data, not PEM-encoded. Your code will work fine if you simply discard the "BEGIN" and "END" and base64-decode the public keys contents.

X509EncodedKeySpec需要 DER 编码的数据,而不是 PEM 编码的数据。如果您简单地丢弃“BEGIN”和“END”并对公钥内容进行 base64 解码,您的代码将可以正常工作。

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

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

回答by Tash Pemhiwa

In my case the public key was contained in a certificate and I had to do something of the nature:

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

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

Note: My file started with -----BEGIN CERTIFICATE ----

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