Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 public key information saved with the private key. So now im thinking that is there a way to extract public key from private?

like image 936
hs2d Avatar asked Sep 13 '25 19:09

hs2d


1 Answers

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).

like image 94
Thomas Pornin Avatar answered Sep 16 '25 09:09

Thomas Pornin