X509 static RSACryptoServiceProvider 高并发加解密


/*
makecert.exe -n "CN=Microshaoft X509 Test - A" -sky exchange -pe -sv a.pvk a.cer
pvk2pfx.exe -pvk a.pvk -spc a.cer -pfx a.pfx -f -po 123
makecert.exe -n "CN=Microshaoft X509 Test - B" -sky exchange -pe -sv b.pvk b.cer
pvk2pfx.exe -pvk b.pvk -spc b.cer -pfx b.pfx -f -po abc
*/
namespace ConsoleApplication
{
    using System;
    using System.IO;
    using System.Text;
    using System.Threading;
    using System.Security.Cryptography;
    using System.Security.Cryptography.X509Certificates;
    public class Class1
    {
        static void Main(string[] args)
        {
            encryptorPrivateKeyPfxProvider = encryptorPrivateKeyPfx.PrivateKey as RSACryptoServiceProvider;
            encryptorPublicKeyCerProvider = encryptorPublicKeyCer.PublicKey.Key as RSACryptoServiceProvider;
            decryptorPublicKeyCerProvider = decryptorPublicKeyCer.PublicKey.Key as RSACryptoServiceProvider;
            decryptorPrivateKeyPfxProvider = decryptorPrivateKeyPfx.PrivateKey as RSACryptoServiceProvider;
            for (int i = 0 ; i < 2000 ; i++)
            {
                ThreadStart ts = new ThreadStart(Run);
                Thread t = new Thread(ts);
                t.Name = _ThreadID.ToString();
                _ThreadID ++;
                t.Start();
                //Run();
            }
        
        }
        private static volatile int _ThreadID = 0;
        private static object _syncLockObject = new object();
        private static X509Certificate2 encryptorPrivateKeyPfx = new X509Certificate2(@"a.pfx", "123");
        private static X509Certificate2 encryptorPublicKeyCer = new X509Certificate2(@"a.cer");
        private static X509Certificate2 decryptorPublicKeyCer = new X509Certificate2(@"b.cer");
        private static X509Certificate2 decryptorPrivateKeyPfx = new X509Certificate2(@"b.pfx", "abc");
        private static RSACryptoServiceProvider encryptorPrivateKeyPfxProvider = null;
        private static RSACryptoServiceProvider encryptorPublicKeyCerProvider = null;
        private static RSACryptoServiceProvider decryptorPublicKeyCerProvider = null;
        private static RSACryptoServiceProvider decryptorPrivateKeyPfxProvider = null;
        static void Run()
        {
            //
            // TODO: 在此处添加代码以启动应用程序
            //
            string s = "原明文原明文原明文原明文原明文原明文原明文原明文原明文";
            byte[] data = Encoding.UTF8.GetBytes(s);
            byte[] signature;
            bool DoOAEPadding = false;
            bool verifyed = false;
///            X509Certificate2 encryptorPrivateKeyPfx = null;
///            X509Certificate2 encryptorPublicKeyCer = null;
///            X509Certificate2 decryptorPublicKeyCer = null;
///            X509Certificate2 decryptorPrivateKeyPfx = null;
            try
            {
///                encryptorPrivateKeyPfx = new X509Certificate2(@"a.pfx", "123");
///                encryptorPublicKeyCer = new X509Certificate2(@"a.cer");
///                decryptorPublicKeyCer = new X509Certificate2(@"b.cer");
///                decryptorPrivateKeyPfx = new X509Certificate2(@"b.pfx", "abc");
                //using (RSACryptoServiceProvider decryptorPublicKeyCerProvider = decryptorPublicKeyCer.PublicKey.Key as RSACryptoServiceProvider)
                {
                    //加密
                    data = decryptorPublicKeyCerProvider.Encrypt(data, DoOAEPadding);
                }
                byte[] hash = new SHA1CryptoServiceProvider().ComputeHash(data);
                //using (RSACryptoServiceProvider encryptorPrivateKeyPfxProvider = encryptorPrivateKeyPfx.PrivateKey as RSACryptoServiceProvider)
                {
                    //签名
                    signature = encryptorPrivateKeyPfxProvider.SignHash(hash, "SHA1");
                }
                //using (RSACryptoServiceProvider encryptorPublicKeyCerProvider = encryptorPublicKeyCer.PublicKey.Key as RSACryptoServiceProvider)
                {
                    //验签
                    verifyed = encryptorPublicKeyCerProvider.VerifyHash(hash, "SHA1", signature);
                }
                //using (RSACryptoServiceProvider decryptorPrivateKeyPfxProvider = decryptorPrivateKeyPfx.PrivateKey as RSACryptoServiceProvider)
                {
                    //解密
                    data = decryptorPrivateKeyPfxProvider.Decrypt(data, DoOAEPadding);
                }
                s = Encoding.UTF8.GetString(data);
                Console.WriteLine("{0},{1},{2}", Thread.CurrentThread.Name, verifyed, s);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
///                if (encryptorPrivateKeyPfx != null)
///                {
///                    encryptorPrivateKeyPfx.Reset();
///                }
///                if (encryptorPublicKeyCer != null)
///                {
///                    encryptorPublicKeyCer.Reset();
///                }
///                if (decryptorPublicKeyCer != null)
///                {
///                    decryptorPublicKeyCer.Reset();
///                }
            }
        }
    }
}

原文地址:https://www.cnblogs.com/Microshaoft/p/1506899.html