c# RSA extract public key from private key

前端 未结 3 637
忘了有多久
忘了有多久 2020-12-21 10:28

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 privat

相关标签:
3条回答
  • 2020-12-21 11:08

    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.

    0 讨论(0)
  • 2020-12-21 11:10

    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.

    0 讨论(0)
  • 2020-12-21 11:13

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

    0 讨论(0)
提交回复
热议问题