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();
    ///                }
                }
            }
        }
    }
    
    
  • 相关阅读:
    Docker容器启动时初始化Mysql数据库
    使用Buildpacks高效构建Docker镜像
    Mybatis 强大的结果集映射器resultMap
    Java 集合排序策略接口 Comparator
    Spring MVC 函数式编程进阶
    换一种方式编写 Spring MVC 接口
    【asp.net core 系列】6 实战之 一个项目的完整结构
    【asp.net core 系列】5 布局页和静态资源
    【asp.net core 系列】4. 更高更强的路由
    【Java Spring Cloud 实战之路】- 使用Nacos和网关中心的创建
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/1506899.html
Copyright © 2011-2022 走看看