How to convert Byte array to PrivateKey or PublicKey type?

前端 未结 1 966
孤街浪徒
孤街浪徒 2020-12-07 23:06

I am using RSA algorithm to generate public and private key

final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
keyGen.initialize(1024)         


        
相关标签:
1条回答
  • 2020-12-07 23:34

    If you have a byte[] representing the output of getEncoded() on a key, you can use KeyFactory to turn that back into a PublicKey object or a PrivateKey object.

    byte[] privateKeyBytes;
    byte[] publicKeyBytes;
    KeyFactory kf = KeyFactory.getInstance("RSA"); // or "EC" or whatever
    PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
    PublicKey publicKey = kf.generatePublic(new X509EncodedKeySpec(publicKeyBytes));
    
    0 讨论(0)
提交回复
热议问题