c# RSA extract public key from private key
Is there a way to extract public key from private key in c#? Becouse if i do ToXMLString()
then i can set if i want publ开发者_如何学编程ic key information saved with the private key. So now im thinking that is there a way to extract public key from private?
The normal private key format for RSA includes the public key (the "public exponent" is useful for implementation of private key operations in a way which resists timing attacks). Therefore, it is possible to extract the public key from the private key.
(It is theoretically possible to have a "pure RSA private key" which does NOT include the public exponent, but it has drawbacks, such as much harder protection against side-channel attacks, and reduced performance. Therefore nobody in their right mind does that. You can assume that when you have the private key you actually have the complete key pair.)
In the C#/.NET standard library, public and private RSA keys can be represented as XML strings (ToXmlString()
and FromXmlString()
) or a custom RSAParameters
structure (ExportParameters()
and ImportParameters()
). If you can obtain the complete private key then you just have to pick the public fields (modulus and public exponent), which constitute together the public key. Note that RSACryptoServiceProvider
may be an interface to an underlying RSA implementation which could refuse to export the private key (but will usually accept to export the public key).
Take the modulus from the private key, and the public exponent is - most likely - 65537. All Microsoft cryptosystems I've seen so far produce public keys with that exponent. The combination of public exponent and modulus is the public key.
If you can save it, and include the private key, then what you're saving is not just the private key.
However, if you really do have only the private key, then no, you can't "extract" the public key from it. If you could do that, you would render most of todays security obsolete.
So basically, I don't believe you have only the private key, you have a key pair, and you should be able to extract the public key from that.
Whether that is easily doable in C# or .NET, I have no idea.
精彩评论