0

I have a program written in C# that receives a public RSA key, that is used to encrypt some data.

The key looks something like:

"-----BEGIN PUBLIC KEY-----\nMI..........QAB\n-----END PUBLIC KEY-----"

I need in my program to somehow use this key to encrypt some data. I have tried to look for a good method in the internet, but found only solutions that were either complicated (for example somehow extracting numbers from the Rsa-key string that are used as components in the key), or answers that relied on some third-party libraries, such as "Bouncy Castle".

Also, because I managed to successfully use a public key with some library in python. I have tried using a subrocess written in python that will recieve the key, some data and return the encrypted data.

That will probably work well, but is there a simpler solution using some built-in libraries in C#, such as "System.Security.Cryptography", that can import a public rsa key and use it?

Sami
  • 117
  • 1
  • 3
  • What would be easier than just using BouncyCastle? – Artjom B. Mar 08 '21 at 14:23
  • 1
    In .NET Core, as of version 3.0, an X.509/SPKI key (DER encoded) can be loaded _directly_ with [`RSA.ImportSubjectPublicKeyInfo()`](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsa.importsubjectpublickeyinfo?view=netcore-3.0). For earlier versions, or for .NET Framework, BouncyCastle is the simplest option. – Topaco Mar 08 '21 at 14:44

0 Answers0