从文件加载RSA私钥时的InvalidKeySpecExeption [英] InvalidKeySpecExeption when loadding the RSA private key from file

查看:490
本文介绍了从文件加载RSA私钥时的InvalidKeySpecExeption的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从java中的文件加载私钥。此密钥由ssh-agent生成。我实际上使用下面的代码:

I'm trying to load a private key from file in java. This key is generated by ssh-agent. I'm actually using the code below:

     public PrivateKey getPrivateKeyFromFile() {
    try {
        //String privateKey = readFileAsString(System.getProperty("user.dir")+"/clefs/"+privateKeyName);
        //byte[] encodePrivateKey = privateKey.getBytes();
        File filePrivateKey = new File(System.getProperty("user.dir")+"/clefs/"+privateKeyName);
        FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"/clefs/"+privateKeyName);
        byte[] encodePrivateKey = new byte[(int) filePrivateKey.length()];
        fis.read(encodePrivateKey);
        fis.close();

        java.security.KeyFactory keyFactory = java.security.KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec privatekeySpec = new PKCS8EncodedKeySpec(encodePrivateKey);
        PrivateKey prikey = (PrivateKey) keyFactory.generatePrivate(privatekeySpec);
        return prikey;

    } catch (NoSuchAlgorithmException ne) {
        ne.printStackTrace();
    } catch (InvalidKeySpecException is) {
        is.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;

}

但是它产生了这个例外:

But it generated this exception:

 java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)
at com.nguyenkim.cea.signature.SignChallenge.getPrivateKeyFromFile(SignChallenge.java:53)
at com.nguyenkim.cea.signature.SignChallenge.main(SignChallenge.java:128)
 Caused by: java.security.InvalidKeyException: invalid key format
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:341)
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:367)
at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:91)
at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75)
at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316)
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213)
... 3 more

这是私钥:

   -----BEGIN RSA PRIVATE KEY-----
  MIIEowIBAAKCAQEAszReSzBumVb9GR/f3ClgykWE4UsONan1Ywk/H4+Wbi4HpcwB
  8Lm9B+zJ94WdRtD8iQYmbUZFoHwFqTjRPtmQfFXcmxfuI7v64bg0csIw8hz1Af2r
  xo7HBUoVcrTG5k3YrIkjeni/vD9uK6OZ1/lb+/TIvoEp9za577GJxv1omQ6GX7kv
  baMe2GkfpJmrXnA706OEdyi3Ibdcng/V4lbJ9cm+TIBU2jLBqwEukwpL5VNghuQi
  3YfpGhnPDBEnh4h5euFs4DGs4FnCgb+00yCuEgJSPvO5HsTnGbwTtEUnkxjtg8vD
  plD7WenPsyiZqib/rLkNcpfEHKVC6G3QjEuO8QIDAQABAoIBAGliRoFY/fFW4og/
  .............................
  -----END RSA PRIVATE KEY-----

有什么建议吗?
谢谢。

Any suggestions? Thanks.

推荐答案

你确定它的RSA吗?你也确定密钥格式正确吗?

Are you sure its RSA ? also are you sure that the key is in the right format?

如果两个问题的答案都是肯定的,你可以尝试使用 bouncycastle lib

If the answer is yes to both questions you can try using bouncycastle lib

编辑:尝试删除这些行从钥匙:

EDIT : Try removing these rows from the key:

-----BEGIN RSA PRIVATE KEY-----
.............................
-----END RSA PRIVATE KEY-----

UPDATE:确保您的私钥是PKCS8格式,如果不是,您需要将其转换为< a href =https://stackoverflow.com/questions/3243018/how-to-load-rsa-private-key-from-file>这里

UPDATE : make sure that you private key is in PKCS8 format if not you need to convert it like here

这篇关于从文件加载RSA私钥时的InvalidKeySpecExeption的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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