Search code examples
c#cng

How to create RSACng from a public key in GenericPublicBlob format?


I am using System.Security.Cryptography.Cng (4.7.0).

I have exported a public key from a CngKey object using

byte[] publicKey = cngKey.Export(CngKeyBlobFormat.GenericPublicBlob);

How can I use this at a later time to create an RSACng object for public-key encryption?


Solution

  • A CngKey can be recreated from the blob like this:

    using CngKey key = CngKey.Import(blob, CngKeyBlobFormat.GenericPublicBlob);
    

    And then we can create an RSACng for e.g. encrypting data:

    using RSACng rsaCng = new RSACng(key);