zoukankan      html  css  js  c++  java
  • <经验杂谈>C#对CA证书加密解密的简单介绍

    最近做项目接触了一些关于用CA证书加密解密的知识,现在分享一下,加密主要分为对称加密和非对称加密以及单项加密这三种,CA是一个权威的第三方认证机构,CA加密有公钥和私钥之分。

    以下是C#读取证书文件进行加密解密的Code,供各位参考

     CA 加密:

    public static string CAEncryption(string xml)
            {
                X509Certificate2 pubcrt =new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + BaseConfig.CaPubkey);
                return Core.CaUtilHelper.Encrypt(xml, pubcrt);
            }
    
    
    public static String Encrypt(String plaintext, X509Certificate2 pubcrt)
            {
                X509Certificate2 _X509Certificate2 = pubcrt;
                using (RSACryptoServiceProvider RSACryptography = _X509Certificate2.PublicKey.Key as RSACryptoServiceProvider)
                {
                    Byte[] PlaintextData = Encoding.UTF8.GetBytes(plaintext);
                    int MaxBlockSize = RSACryptography.KeySize / 8 - 11;    //加密块最大长度限制  
                    if (PlaintextData.Length <= MaxBlockSize)
                        return Convert.ToBase64String(RSACryptography.Encrypt(PlaintextData, false));
                    using (MemoryStream PlaiStream = new MemoryStream(PlaintextData))
                    using (MemoryStream CrypStream = new MemoryStream())
                    {
                        Byte[] Buffer = new Byte[MaxBlockSize];
                        int BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize);
                        while (BlockSize > 0)
                        {
                            Byte[] ToEncrypt = new Byte[BlockSize];
                            Array.Copy(Buffer, 0, ToEncrypt, 0, BlockSize);
                            Byte[] Cryptograph = RSACryptography.Encrypt(ToEncrypt, false);
                            CrypStream.Write(Cryptograph, 0, Cryptograph.Length);
                            BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize);
                        }
                        return Convert.ToBase64String(CrypStream.ToArray(), Base64FormattingOptions.None);
                    }
                }
            }

    CA 解密:

    public static string CADecrypt(string content)
            {
                X509Certificate2 prvcrt = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + BaseConfig.CaPrvkey,BaseConfig.CaPwd, X509KeyStorageFlags.Exportable);
                return Core.CaUtilHelper.Decrypt(content, prvcrt);
            }
    
    
    public static String Decrypt(String ciphertext, X509Certificate2 prvpfx)
            {
                X509Certificate2 _X509Certificate2 = prvpfx;
                using (RSACryptoServiceProvider RSACryptography = _X509Certificate2.PrivateKey as RSACryptoServiceProvider)
                {
                    Byte[] CiphertextData = Convert.FromBase64String(ciphertext);
                    int MaxBlockSize = RSACryptography.KeySize / 8; //解密块最大长度限制  
                    if (CiphertextData.Length <= MaxBlockSize)
                        return Encoding.UTF8.GetString(RSACryptography.Decrypt(CiphertextData, false));
                    using (MemoryStream CrypStream = new MemoryStream(CiphertextData))
                    using (MemoryStream PlaiStream = new MemoryStream())
                    {
                        Byte[] Buffer = new Byte[MaxBlockSize];
                        int BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize);
                        while (BlockSize > 0)
                        {
                            Byte[] ToDecrypt = new Byte[BlockSize];
                            Array.Copy(Buffer, 0, ToDecrypt, 0, BlockSize);
                            Byte[] Plaintext = RSACryptography.Decrypt(ToDecrypt, false);
                            PlaiStream.Write(Plaintext, 0, Plaintext.Length);
                            BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize);
                        }
                        return Encoding.UTF8.GetString(PlaiStream.ToArray());
                    }
                }
            }
  • 相关阅读:
    应用性能提升 要速度也要激情
    投票练习题
    租房子多条件查询练习
    JS时间戳格式化日期时间 由于mysql数据库里面存储时间存的是时间戳,取出来之后,JS要格式化一下显示。
    处理PHP字符串的10个简单方法;mysql出现乱码:character_set_server=utf8
    预定义数组(超全局数组)]
    静态方法
    类的构造方法和析构方法和封装的目的和封装的做法+访问修饰符
    面向对象与面向过程 $this的注意事项和魔术方法set和get
    PHP正则表达式;数组:for()遍历、 foreach ()遍历、each()list()组合遍历;指针遍历
  • 原文地址:https://www.cnblogs.com/benpao/p/6741565.html
Copyright © 2011-2022 走看看