使用公钥的InvalidKeySpecException [英] InvalidKeySpecException using public key

查看:1184
本文介绍了使用公钥的InvalidKeySpecException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在拼命尝试在Android上使用非对称公钥/私钥加密来加密邮件。

I'm desperately trying to encrypt a message using asymmetric public / private key cryptography on an Android.

我在Windows上并且我已经生成了一个公开信息和使用puttygen的私钥。我不确定它有什么不同,但我选择了SSH-2 RSA。这里是公钥:

I'm on Windows and I've generated a public and private key using puttygen. I'm not sure what difference it makes but I've selected SSH-2 RSA. Here is the public key:

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

我已将其复制到main / assets文件夹中的文本文件中。我这样读过:

I've copied this to a text file in my main/assets folder. I read this in like so:

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

然后通过相当标准的ByteArrayOutputStream方法将其读入字节数组。

This is then read in to a byte array through a fairly standard ByteArrayOutputStream method.

然后尝试将其转换为公钥:

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.

我已经尝试过Base64:

I've tried Base64 as such:

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));

但是putty并没有告诉我任何有关指数的信息?如果我继续使用这种方法,我不会得到同样的错误,但如果我尝试用我的私钥解密,我就会得到胡言乱语。

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密钥不是X509兼容密钥。它们以SSH专有格式存储。您需要一个支持SSH的库来检索键值。

SSH keys are not X509 compatible keys. They are stored in a SSH proprietary format. You'll need a SSH capable libary to retrieve the key value.

如果不需要SSH功能,则可以用Java生成密钥(使用 keytool 命令行或 KeyPairGenerator

If SSH functionality is not required then it is possible to generate keys in Java (using the keytool command line or KeyPairGenerator.

或者它也可以使用外部应用程序或库,例如 openssl 命令行。在OpenSSL的情况下,将DER指定为输出.Java期望DER编码 SubjectPublicKeyInfo X509EncodedKeySpec 的结构。

Alternatively it is also possible to use external applications or libraries such as the openssl command line. In the case of OpenSSL specify DER as output. Java expects a DER encoded SubjectPublicKeyInfo structure for X509EncodedKeySpec.

这篇关于使用公钥的InvalidKeySpecException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆