RSA Encryption by Submitting Module and Metric

I am creating a C # Winforms application that sends data to a server via HTTPS.

The entry mechanism should be like this:

  • I send the username to the server, it responds with the rsa-module and rsa-exponent

  • I encrypt the password using this data and send the username + password to the server for authentication

I tried the RSACryptoServiceProvider class, but I can not find samples or anything that said , how can we do encryption using this module and exponent? .

I think that without specifying any values, it executes the default encryption settings.

So, if someone has done this before, can they give me some hints? thanks

UPDATE : as proposed by Mr. Karsten Koenig. I tried to do this using RSAParameters and RSA.ImportParameters , but it returns a "BAD DATA" error with a cryptographic exception. My code is below.

I also tried RSA.FromXmlString(mykey) ; (where mykey contains an xml string with module and exp), but I also get a "BAD DATA" error with a cryptographic exception ... any idea is anyone? or if its some bug at Microsoft, can anyone suggest some other decent library to make this easy?

 RSAParameters rsaparam = new RSAParameters(); rsaparam.Modulus = modbytes; rsaparam.Exponent = expbytes; RSACryptoServiceProvider RSA = new RSACryptoServiceProvider() ; RSA.ImportParameters(rsaparam); byte[] encryptedData = RSA.Encrypt(dataToEncrypt, false) 
+6
source share
3 answers

You can do this using the RSACryptoServiceProvider.Encrypt method. You will also need to use the RSACryptoServiceProvider.ImportParameters method and pass it to RSAParameters (here you set the exponent, module, etc.).

Please take a look at the documentation for the link for RSAParameters - it very well documented which parameter you should pass for any structural field - this should not be a problem if you are now using the algorithm.

EDIT: here is an example directly from the MSDN website :

 class RSACSPSample { static void Main() { try { //initialze the byte arrays to the public key information. byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56, 74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222, 207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175, 108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194, 240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139, 168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171, 38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93, 106,99,179,68,175,211,164,116,64,148,226,254,172,147}; byte[] Exponent = {1,0,1}; //Values to store encrypted symmetric keys. byte[] EncryptedSymmetricKey; byte[] EncryptedSymmetricIV; //Create a new instance of RSACryptoServiceProvider. RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //Create a new instance of RSAParameters. RSAParameters RSAKeyInfo = new RSAParameters(); //Set RSAKeyInfo to the public key values. RSAKeyInfo.Modulus = PublicKey; RSAKeyInfo.Exponent = Exponent; //Import key parameters into RSA. RSA.ImportParameters(RSAKeyInfo); //Create a new instance of the RijndaelManaged class. RijndaelManaged RM = new RijndaelManaged(); //Encrypt the symmetric key and IV. EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false); EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false); Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider."); } //Catch and display a CryptographicException //to the console. catch(CryptographicException e) { Console.WriteLine(e.Message); } } } 

Please note that only the / iv switch is encrypted - not arbitrary bytes - the length of these bytes is also important!

The permissible length is described in MSDN and depends on the OS!

+6
source

One additional hint that was very helpful to me:

In this line

 //Set RSAKeyInfo to the public key values. SAKeyInfo.Modulus = PublicKey; 

PublicKey can also be a direct, simple byte array that you can get from the "Public Key" field of the X509 certificate (directly).

0
source

If you use RSACryptoServiceProvider.ToXmlString to export the module and exponent sent by the server, you need to use Convert.FromBase64String.

  public RSAParameters SetPublicKey(string modulus, string exponent) { RSAParameters result = new RSAParameters(); result.Modulus = Convert.FromBase64String(modulus); result.Exponent = Convert.FromBase64String(exponent); return result; } 
0
source

Source: https://habr.com/ru/post/911495/


All Articles