Home c# How to properly generate the RSA-2048 key?

How to properly generate the RSA-2048 key?

Author

Date

Category

Tell me how to correctly generate a private and private key precisely for RSA-2048

I correctly understood that Public WHERE WE CHRICBT THE TEXT, A Private We have to decipher ?!

[UPD ]

wrote two methods for receiving keys:

public string getprivatekey ()
{
  RSAPARAMETERS PRIVATEKEY;
  Using (RSACryptoServiceProvider RSA = New RsacryptoServiceProvider (2048))
  {
   PrivateKey = RSA.ExportParameters (TRUE);
   rsa.persistkeyincsp = false;
  }
  Return this.getKeyString (PrivateKey);
}
Public String GetPublicKey ()
{
 RSAPARAMETERS PUBLICKEY;
 Using (RSACryptoServiceProvider RSA = New RsacryptoServiceProvider (2048))
 {
   PublicKey = RSA.ExportParameters (false);
   rsa.persistkeyincsp = false;
 }
 Return this.getKeyString (Publickey);
}
Public String GetkeyString (RSAPARAMETER PUBLICKEY)
{
  Try.
  {
   Using (Stringwriter Stringwriter = New Stringwriter ())
   {
    XmlSerializer Xmlserializer = New Xmlserializer (Typeof (RSAPARAMETERS));
    xmlserializer.serialize (Stringwriter, Publickey);
    Return stringwriter.tostring ();
   }
  }
  Catch (Exception) {Return String.Empty; }
 }

Private key I get: [Cut the text – too big ]

& lt; xml version = "1.0" encoding = "UTF-16"? & gt;
& lt; RSAPARAMETERS XMLNS: XSD = "http://www.w3.org/2001/xmlschema"
xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" & gt;
& lt; EXPONENT & GT; AQAB & LT; / EXPONENT & GT; & lt; modulus & gt; 6vydkck8ilvq164pfa5vyjl2ymgfwoxowcqca5vjb5e
/Srixu8wwv5cukx8z8ifelsf4os2p4gsmjynivxnbxjvseiw/pupz....q==< ;/modulus>
& lt; / rsaparameters & gt;

Public key I also get: [Cut the text – too big ]

& lt; xml version = "1.0" encoding = "UTF-16"? & gt;
& lt; rsaparameters xmlns: xsd = "http://www.w3.org/2001/xmlschema" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" & gt;
 & lt; EXPONENT & GT; AQAB & LT; / Exponent & GT;
 & lt; modulus & gt; xyzdflixvcshkqgzojgiiu9y99 + 0 + xylqtq == & lt; / modulus & gt;
 & lt; p & gt; 3DDMXVQT3AGKMSBJJ ++ 2DWiJJYTFASYJDEFYN8 = & lt; / p & gt;
 & lt; q & gt; 5aauwqevrushtfdlf4fbmwv3mjuhljdyz1gtorczljlqszs3wppwss = & lt; / q & gt;
 & lt; dp & gt; ggkdp2t1ndoqb9alc8 = & lt; / dp & gt;
 & lt; DQ & GT; E9JFGGFFCTDC = & LT; / DQ & GT;
 & lt; Inverseq & gt; FruC1FURCMMG80D / W = & lt; / Inverseq & gt;
 & lt; d & gt; u2w4hhuunz / + ifc2weusnugbbqrbq == & lt; / d & gt;
& lt; / rsaparameters & gt;

Tell me I did everything right?


is also very interested in these questions))

  • 1 – how to save keys to a file without an XML format?

  • 2 – as it was written on other forums in using RSA key is saved by etc.
    and what you need to set rsa.persistkeyincsp = false; – whether
    Is it?

  • 3 – Is it possible to call RSAcryptoServiceProvider several times
    using ?


Answer 1

I correctly understood that the public key we encrypt the text, and private
Do we have to decipher ?!

True.

  1. Rsaparameters has public fields (p , q , modulus , etc.). They are all simple byte arrays. You can save them in another format convenient for you. And restore back simply writing an array byte in an empty instance.
  2. You need to do this only when you initiate rsacryptoServiceProvider with the key storage container. By default, no container is used, which means that the key is not saved anywhere. with documentation :

    The PersistKeyincSp Property Has No Effect If The RsacryptoServiceProvider Object Is Created with A NULL KEY CONTAINER NAME.

  3. only if you import the key from somewhere. You can create a new key only by creating a new instance RSAcryptoServiceProvider .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions