0

I generated a private key and a public key from a website and so I have my keys as string.

I would like to use them to encrypt and decrypt data. However, I can't find any solution that explain how to do that properly. Using only the System.Security.Cryptography namespace (I can't use other librairies).

I understand I have to set, in an RSAParameter object, my keys. However, how can I do that from raw string keys? The documentation (and mostly 100% of the solutions I found on internet) generates keys using the RSA classe(s) of .Net, but don't explain how to encrypt/decrypt using already generated keys.

So, basically:

How to encrypt/decrypt data using the System.Security.Cryptography namespace with the RSA algorithm and already generated keys (as strings)?

Itération 122442
  • 2,644
  • 2
  • 27
  • 73
  • In .NET RSA is good only to encrypt an hash if I remember correctly. You can't use it to encrypt directly a generic message. – xanatos Jun 20 '18 at 18:38
  • Actually, I want to encrypt small json. @xanatos – Itération 122442 Jun 20 '18 at 18:46
  • 245 bytes (for a 2048 bits key) are quite little for a json. RSA isn't the right "instrument" to encrypt data that isn't a key or a hash. If you want I can reopen the question. – xanatos Jun 20 '18 at 18:50
  • @xanatos The size is not a problem. My JSON is composed of small strings and SHA256 hashes that I can encrypt separately. But I take your advices in account and will think about it. However, I'm still curious about how to do it. The source you provided explains how to do it using XML, but not plain text. – Itération 122442 Jun 20 '18 at 18:55
  • 1
    https://stackoverflow.com/a/17137218/1260204, you can ignore the key generation and plug in your own values. – Igor Jun 20 '18 at 18:58
  • @xanatos Considering the fact that it is not well suited to encrypt "big" amount of data, any suggestion for that? – Itération 122442 Jun 20 '18 at 19:03
  • @Igor That link doesn't support the key format of the site in this question. This question uses PEM, that question uses XML. – xanatos Jun 20 '18 at 19:19
  • @Andromelus The code you asked. Can't gaurantee the quality because I had to use methods written by other persons and RSA, PEM Keys, RSA padding aren't my forte. https://gist.github.com/xanatos/bb515af28c39f66d3fccb57b60fdf3bc – xanatos Jun 20 '18 at 19:28
  • 1
    @xanatos - I assumed the op made it work as he/she marked their own question as that dupe first. – Igor Jun 20 '18 at 19:34

0 Answers0