简体   繁体   English

使用公钥的InvalidKeySpecException

[英]InvalidKeySpecException using public key

I'm desperately trying to encrypt a message using asymmetric public / private key cryptography on an Android. 我正拼命尝试在Android上使用非对称的公钥/私钥加密来加密消息。

I'm on Windows and I've generated a public and private key using puttygen. 我在Windows上,并且已经使用puttygen生成了公钥和私钥。 I'm not sure what difference it makes but I've selected SSH-2 RSA. 我不确定会有什么区别,但是我选择了SSH-2 RSA。 Here is the public key: 这是公钥:

AAAAB3NzaC1yc2EAAAABJQAAAQEAh63orUzl0UTd7jj0KNYJg1+kNnty0QHyJu0r
Cajf5Kl7qWJaGXPfwsG8Qt3teafs5sv0JBSinab0s/5wfQmd1QPpXTMP93Wc4ucp
1VC/9B2o8XVi4fKoGTehB48yrSfI6KF2AIeASM1jUswydKxsuS4AS2mLGV/HuoKD
huMfCsRc8qK5zGQfVCoZTbQ66Z1yKdAzxMUuGmiTp7pVsle/P/UGbm6yFiee5r1/
dOR2CDyR6CP09Jaj7KSGfGuwPryCXPjEce1oCbN/FlLHVb7T1B5f6xhq+oY+Ij13
1IZPfShV8cs2kYKjsle2s23V5urSdWFv2tEcSJcpkUm2FlPdQw==

I've copied this to a text file in my main/assets folder. 我已将此文件复制到main / assets文件夹中的文本文件中。 I read this in like so: 我这样阅读:

InputStream input = context.getAssets().open(filename);

This is then read in to a byte array through a fairly standard ByteArrayOutputStream method. 然后通过相当标准的ByteArrayOutputStream方法将其读入字节数组。

I then try and convert that to a public key as such: 然后,我尝试将其转换为公钥,如下所示:

public static PublicKey getPublicKey(byte[] keyBytes){
    PublicKey publicKey = null;

    if(keyBytes != null) {

        X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
        KeyFactory kf = null;
        try {
            kf = KeyFactory.getInstance("RSA");
            publicKey = kf.generatePublic(spec);
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "NoSuchAlgorithmException");
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "InvalidKeySpecException " + e.getMessage());
            e.printStackTrace();
        }
    }

    return publicKey;
}

Problem is I keep getting this error: 问题是我不断收到此错误:

InvalidKeySpecException java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag

I've been attacking this for hours, and can't seem to get around it. 我已经攻击了几个小时,似乎无法解决它。 Please please any suggestions welcome. 请任何建议欢迎。

I've tried Base64 as such: 我已经尝试过Base64这样:

byte[] tempNewKey = Base64.decode(keyBytes, Base64.DEFAULT);

Which makes no difference and I've also tried using 这没有什么区别,我也尝试过使用

RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(module), new BigInteger(exponent));

However putty doesn't tell me anything about an exponent? 但是油灰不能告诉我有关指数的任何信息吗? If I go ahead with this method I don't get the same error, but if I try and decrypt with my private key I just get gibberish. 如果继续使用此方法,不会收到相同的错误,但是如果尝试使用私钥进行解密,则只会出现乱码。

Really hope you can help. 真的希望您能提供帮助。 Many Thanks 非常感谢

SSH keys are not X509 compatible keys. SSH密钥不是X509兼容密钥。 They are stored in a SSH proprietary format. 它们以SSH专有格式存储。 You'll need a SSH capable libary to retrieve the key value. 您将需要具有SSH功能的库来检索密钥值。

If SSH functionality is not required then it is possible to generate keys in Java (using the keytool command line or KeyPairGenerator . 如果不需要SSH功能,则可以使用Java(使用keytool命令行或KeyPairGenerator生成密钥。

Alternatively it is also possible to use external applications or libraries such as the openssl command line. 或者,也可以使用外部应用程序或库,例如openssl命令行。 In the case of OpenSSL specify DER as output. 对于OpenSSL,将DER指定为输出。 Java expects a DER encoded SubjectPublicKeyInfo structure for X509EncodedKeySpec . Java的期望一个DER编码SubjectPublicKeyInfo了结构X509EncodedKeySpec

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM