PublicKey 与 RSAPublicKeySpec
当我创建 RSA 密钥对时,我应该做什么
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();
save("public.key",publicKey.getEncoded())
save("private.key",privateKey.getEncoded())
或
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(),RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(),RSAPrivateKeySpec.class);
saveToFile("public.key", pub.getModulus(),pub.getPublicExponent());
saveToFile("private.key", priv.getModulus(),priv.getPrivateExponent());
哪个更好,有什么区别?
When I create an RSA keypair should I be be doing
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();
save("public.key",publicKey.getEncoded())
save("private.key",privateKey.getEncoded())
OR
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(),RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(),RSAPrivateKeySpec.class);
saveToFile("public.key", pub.getModulus(),pub.getPublicExponent());
saveToFile("private.key", priv.getModulus(),priv.getPrivateExponent());
Which is better and what is the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于公钥来说,没有太大区别。对于私钥,getEncoded() 返回的信息比私钥多得多。
这是 RSA 私钥的 ASN.1 架构,
这些额外的参数将大大加快私钥操作的速度。因此,您应该始终使用
getEncoded()
。For public keys, it doesn't make much difference. For private keys, getEncoded() returns much more information than the private key.
Here is the ASN.1 schema for RSA Private Key,
Those extra parameters will speed up private key operations considerably. So you should always use
getEncoded()
.getEncoded() 方法返回“更”标准的公钥和私钥的编码,因此更有可能与其他系统进行互操作。也就是说,他们使用 PKCS#1 等标准。
如果您不关心互操作性,那么您可能应该使用 java KeyStore 类来存储密钥。
The getEncoded() methods return encodings for public and private keys that are "more" standard and thus more likely to be interoperable with other systems. That is, they use standards such as PKCS#1.
If you are not concerned about interoperability then you should probably use the java KeyStore class for storing keys.