Load a RSA private key in Java (algid parse error, not a sequence)

后端 未结 6 1636
夕颜
夕颜 2020-12-08 05:00

I\'m trying to load a private RSA key generated with ssl into java, my code is:

Generate the key:

openssl genrsa -out mykey.pem 1024         


        
6条回答
  •  北海茫月
    2020-12-08 05:20

    You could still load the keys if necessary,

    public static PublicKey bigIntegerToPublicKey(BigInteger e, BigInteger m)  {
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PublicKey pubKey = fact.generatePublic(keySpec);
        return pubKey;
    }
    
    public static PrivateKey bigIntegerToPrivateKey(BigInteger e, BigInteger m) {
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PrivateKey privKey = fact.generatePrivate(keySpec);
        return privKey;
    }
    

    all you need is the modulus and the exponent.

提交回复
热议问题