zoukankan      html  css  js  c++  java
  • 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();
    ///                }
                }
            }
        }
    }
    
    
  • 相关阅读:
    上学要迟到了【最短路转化】
    解方程【狄利克雷卷积+莫比乌斯反演+积性函数】
    FFT
    min25 筛
    Easy【生成函数】
    CF1406D-Three Sequences
    Alice和Bob赌糖果【赌徒破产模型】
    记MySQL自增主键修改无效的问题
    JVM学习笔记(十一、JDK分析工具)
    JVM学习笔记(十、GC3-垃圾回收机制)
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/1506899.html
Copyright © 2011-2022 走看看