Java asymmetric encryption: preferred way to store public/private keys

前端 未结 3 472
陌清茗
陌清茗 2021-01-30 19:06

This code generates a pair of public/private keys:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance(\"RSA\");
keyGen.initialize(1024);
KeyPair keypair = ke         


        
3条回答
  •  北海茫月
    2021-01-30 19:34

    You're actually storing the bytes in both cases whether you realize it or not. I suppose the correct answer is hinted at in @Brian M. Carr answer, which is to store the higher-level object in its most natural form. In the case of public keys, the obvious choices are as a PKCS#1 RSAPublicKey ASN.1 structure, DER-encoded, or as an X509 SubjectPublicKeyInfo ASN.1 structure, DER-encoded. The latter is what the Sun providers give you, which the sun class X509EncodedKeySpec supports. Similarly, the PKCS8EncodedKeySpec supports a private key format. Both these formats are standards, and are supported by openssl for example. Sun tends -- tended :( -- to support existing standards rather then define their own.

提交回复
热议问题