在Web服务器上缓解RsaCryptoServiceProvider线程安全问题

时间:2011-05-26 15:03:34

标签: asp.net security azure thread-safety x509certificate2

我有一个X509Certificate2实例,并获取其PrivateKey属性,即RsaCryptoServiceProvider。 MSDN文档指出此RsaCryptoServiceProvider是线程安全的。因此,如果给出一些X.509证书,我需要在多个线程上执行非对称加密(通常在Web服务器上),创建RsaCryptoServiceProvider的多个实例的最佳方法是什么?

X509Certificate2上的私钥未标记为可导出,因此我无法简单地导出原始RsaCryptoServiceProvider上的参数并将其重新导入另一个实例,以解决线程安全问题

我通过X509Store获取了原始内容,但这似乎是X509Certificate2个实例的集合,如果我想要RsaCryptoServiceProvider的新实例,我必须实例化 X509Store找到 X509Certificate2,以获取 RsaCryptoServiceProvider。仅仅让.NET克隆RsaCryptoServiceProvider实例似乎非常重要。

有没有更好的方法?

1 个答案:

答案 0 :(得分:2)

RsaCryptoServiceProvider,尽管它的MSDN文档声明它不是线程安全的,但它似乎是线程安全的,足以在多个线程上同时加密/解密。我编写了以下应用程序来测试使用此类的高并发性,并且它没有崩溃或无法正确加密/解密:

using System;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Threading;

namespace ConsoleApplication1 {
    class Program {
        static bool exit;

        static void Main(string[] args) {
            var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            try {
                store.Open(OpenFlags.OpenExistingOnly);

                Func<RSACryptoServiceProvider> rsaFactory = null;
                X509Certificate2 winningCert = null;
                exit = true;
                foreach (X509Certificate2 cert in store.Certificates) {
                    try {
                        var result = store.Certificates.Find(X509FindType.FindByThumbprint, cert.Thumbprint, false).Cast<X509Certificate2>().FirstOrDefault();
                        rsaFactory = () => (RSACryptoServiceProvider)result.PrivateKey;
                        UseRsa(rsaFactory());
                        winningCert = cert;
                        break;
                    } catch (CryptographicException) {
                        Console.WriteLine("Cert {0} failed", cert.Thumbprint);
                    }
                }

                exit = false;
                Console.WriteLine("Winning cert: {0}", winningCert.Thumbprint);
                RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)winningCert.PrivateKey;
                rsaFactory = () => rsa;
                Thread[] threads = new Thread[16];
                for (int i = 0; i < threads.Length; i++) {
                    threads[i] = new Thread(state => UseRsa(rsaFactory()));
                    threads[i].Start();
                }

                Thread.Sleep(10000);

                exit = true;
                for (int i = 0; i < threads.Length; i++) {
                    threads[i].Join();
                }

                Console.WriteLine("Success.");
            } finally {
                store.Close();
            }
        }

        static void UseRsa(RSACryptoServiceProvider rsa) {
            var rng = RandomNumberGenerator.Create();
            var buffer = new byte[64];

            do {
                rng.GetBytes(buffer);
                var cipher = rsa.Encrypt(buffer, true);

                var plaintext = rsa.Decrypt(cipher, true);
                for (int i = 0; i < buffer.Length; i++) {
                    if (buffer[i] != plaintext[i]) {
                        Debugger.Break();
                    }
                }
            } while (!exit);
        }
    }
}