Vivian River
Vivian River

Reputation: 32400

How to use a key generated with the .net RSACryptoServiceProvider class with the browser SubtleCrypto module?

I have written the following C# code:

static void createSHA256KeyFile(string publicKeyPath, string privateKeyPath)
        {
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024))
            {
                try
                {
                    Console.WriteLine(rsa.KeySize);
                    rsa.PersistKeyInCsp = false;
                    byte[] publicKeyBytes = rsa.ExportCspBlob(false);
                    byte[] privateKeyBytes = rsa.ExportCspBlob(true);
                    File.WriteAllBytes(publicKeyPath, publicKeyBytes);
                    File.WriteAllBytes(privateKeyPath, privateKeyBytes);
                    Console.WriteLine("Base 64 Encodings");
                    Console.WriteLine($"\nPublic Key:\n{Convert.ToBase64String(publicKeyBytes)}");
                    Console.WriteLine($"\nPrivate Key:\n{Convert.ToBase64String(privateKeyBytes)}");
                }
                finally
                {
                    rsa.PersistKeyInCsp = false;
                }
            }
        }

The above function generates a key pair, saves them on the disk drive, and then prints a base64 encoding of the keys on the console.

I have been able to use the key-pair to generate digital signatures.

Suppose that I have saved a keypair generated with the above code and I want to use it with the SubtleCrypto module in my browser. (https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto)

Specifically, I want to get the private key in an variable that I can pass to sign(algorithm, key, data).

How can I use either a byte array from the file or a string of the base64 encoding to get the value to pass as the key parameter?

Upvotes: 0

Views: 113

Answers (0)

Related Questions