在Java中从私有获取公钥 [英] Get public key from private in Java
问题描述
我记得很久以前用OpenSSL这么做了,但我想知道它是否可能以及如何,我从来没有在java上使用过Cryptography。
I remember do this long time ago with OpenSSL, but I want to know if it's possible and how, I've never used Cryptography on java.
推荐答案
您无法直接从另一个生成任一密钥。这在数学上是不可能的。如果您的密钥blob包含两者公钥和私钥,则可以相对轻松地提取其中任何一个。
You cannot generate either key directly from the other. It is mathematically impossible. If you had a key blob that contained both the public and private keys, you could extract either one of them with relative ease.
编辑,2017年:多年以后对加密的理解要好得多,而且我现在很清楚这个答案并不是真的正确。
EDIT, 2017: Many years and a much better understanding of crypto later, and it's now clear to me that this answer isn't really correct.
引用维基百科:
公钥由模数n和公共(或加密)指数e组成。私钥由模数n和私有(或解密)指数d组成,它必须保密。 p,q和λ(n)也必须保密,因为它们可用于计算d。
The public key consists of the modulus n and the public (or encryption) exponent e. The private key consists of the modulus n and the private (or decryption) exponent d, which must be kept secret. p, q, and λ(n) must also be kept secret because they can be used to calculate d.
公共模数n可以计算为p× Q值。原始私钥中唯一缺少的是e,但是这个值通常被选为65537,如果不是,你仍然可以从d和λ(n)计算e。
The public modulus n can be computed as p × q. The only thing missing from a raw private key is e, but this value is usually selected as 65537, and if not you can still compute e from d and λ(n).
但是,许多私钥存储格式实际上包含公共模数n和其他组件,因此您可以直接提取值。
However, many private key storage formats actually contain the public modulus n alongside the other components, so you can just do a direct extraction of the values.
编辑,2018年:仍然为此付出了挫折,这是正确的!我将这个答案留下来,这样人们就能明白为什么我本来就错了,并提醒自己将来不要错。
EDIT, 2018: Still getting downvotes for this, and rightly so! I'm leaving this answer up so people can see why I was originally wrong, and to remind myself not to be wrong in future.
这篇关于在Java中从私有获取公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!