0

Hi every body I'm building Client/Server meetings System

I had a problem "Cannot implicitly convert type 'string' to 'System.Security.Cryptography.RSAParameters'"

I will explain the process quickly

  • Client Connect to Server
  • server send back his public Key
  • Client will encrypt the username+his public key By Server public key

I received Server reply ( public key ) but when I tried to encrypt using RSA the error above appeared

This is my Code :

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

// mess is Server response as string
RSAParameters publickey = mess;

rsa.ImportParameters(publickey);

byte[] encryptedData = rsa.Encrypt(StringToByte(uname.Text + "|||" + PUBKEY), true);

1 Answer 1

3

First of all, ImportParameters() does not accept a string. It is expecting something of type RSAParameters. And it cannot implicitly convert a string to RSAParameter object, and that is why the error is shown.

I believe you haven't fundamentally understood on how RSA works. Or is it technical? Read the examples provided in here to understand it technically.

What you should be looking for is how to correctly export the public key from the server, and use the same public key to encrypt something in the client end.

As you would have guessed ExportParameters and ImportParameters looks promising. However I had trouble sending it through an SMS (in my project). So what I used was ExportCspBlob and ImportCspBlob. Make sure you specify false to ExportCspBlob so as to not include private key information.

To send it as a string, what I had to do was base64 encode the byte array that was returned from ExportCspBlob. To do the conversion, use the Convert class. The methods you are specifically looking for are ToBase64String and FromBase64String

Sign up to request clarification or add additional context in comments.

2 Comments

Dear Ranhiru Thank you for your reply I want to ask you how should public key format look like is my public key below correct : e=010001, n=9060434af1e861fef7b918e31067fcacf62ebed29dcfb5deb251e3190d29bf32c63107b9f0301632c4f36a62387028c0d78dc4158a4f7299d0bd77daa66a1a036745d1bd7608b887b7330e2b8b630a7797e7e374635360905965e7a7000aaf6687f406cafc1f9d9309fdc23a04a139c1403335297e925badeb206c87735b326b
How did you generate this? From ExportCspBlob method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.