zoukankan      html  css  js  c++  java
  • 在.NET Core 里使用 BouncyCastle 的DES加密算法

    .NET Core上面的DES等加密算法要等到1.2 才支持,我们可是急需这个算法的支持,文章《使用 JavaScriptService 在.NET Core 里实现DES加密算法》需要用Nodejs,很多人觉得这个有点不好,今天就给大家介绍下BouncyCastle (Portable.BouncyCastle)https://www.nuget.org/packages/Portable.BouncyCastle/库为我们提供的原生的.NET Core的支持库的Des算法。BouncyCastle的文档比较少,折腾了好久才写出了.NET 代码等价的一个封装。

      public class TDesbouncy
        {

            IBlockCipher engine = new DesEngine();

            /// <summary>
            /// 使用DES加密,key输入密码的时候,必须使用英文字符,区分大小写,且字符数量是8个,不能多也不能少
            /// </summary>
            /// <param name="plainText">需要加密的字符串</param>
            /// <param name="keys">加密字符串的密钥</param>
            /// <returns>加密后的字符串</returns>
            public string Encrypt(string keys, string plainText)
            {

                byte[] ptBytes = Encoding.UTF8.GetBytes(plainText);
                byte[] rv = Encrypt(keys, ptBytes);
                StringBuilder ret = new StringBuilder();
                foreach (byte b in rv)
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                return ret.ToString();
            }

            private byte[] Encrypt(string keys, byte[] ptBytes)
            {
                byte[] key = Encoding.UTF8.GetBytes(keys);
                BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine),new Pkcs7Padding());
                cipher.Init(true, new ParametersWithIV(new DesParameters(key),key));
                byte[] rv = new byte[cipher.GetOutputSize(ptBytes.Length)];
                int tam = cipher.ProcessBytes(ptBytes, 0, ptBytes.Length, rv, 0);

                 cipher.DoFinal(rv, tam);
                 return rv;
            }

            /// <summary>
            /// 使用DES解密,key输入密码的时候,必须使用英文字符,区分大小写,且字符数量是8个,不能多也不能少
            /// </summary>
            /// <param name="cipherText">需要加密的字符串</param>
            /// <param name="keys">加密字符串的密钥</param>
            /// <returns>解密后的字符串</returns>
            public string Decrypt(string keys, string cipherText)
            {
                byte[] inputByteArray = new byte[cipherText.Length / 2];
                for (int x = 0; x < cipherText.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(cipherText.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
                var rv = Decrypt(keys, inputByteArray);

                return Encoding.UTF8.GetString(rv);

            }

            private byte[] Decrypt(string keys, byte[] cipherText)
            {
                byte[] key = Encoding.UTF8.GetBytes(keys);
                BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine));
                cipher.Init(false, new ParametersWithIV(new DesParameters(key), key));
                byte[] rv = new byte[cipher.GetOutputSize(cipherText.Length)];
                int tam = cipher.ProcessBytes(cipherText, 0, cipherText.Length, rv, 0);

                cipher.DoFinal(rv, tam);

                return rv;
            }

        }

    public static void Main(string[] args)

                
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

                string key = "geffzhan";
                string content =   "This project.config whoopla is a mess. So what they mean by .NetCore, you still have to reference everything correctly";

                TDesbouncy bouncy = new TDesbouncy();

                var encrypt = bouncy.Encrypt(key, content);
                Console.WriteLine(encrypt);

                string descontent = bouncy.Decrypt(key, encrypt);
                Console.WriteLine(descontent);
    }

    【本文由“oSeHTs-IsJAOjA2yk-lgG7H6ivAU@wechat.sohu.com”发布,2017年5月23日】

  • 相关阅读:
    EasyDSS视频平台定制项目中普通用户登录无法全局搜索出文件的排查及优化
    EasyDSS前端代码运行npm install命令报错如何处理?
    运行EasyDSS前端代码报错Can't find Python executable“python”如何处理?
    EasyDSS视频平台前端点播服务列表跨页新增水印不展示第二页数据优化
    EasyDSS视频平台点播服务选中点播文件删除失败分析及排查
    EasyDSS视频平台下载录像由于ts文件原因导致MP4文件缺秒问题优化
    ffmpeg推流到服务器如何通过EasyDSS视频平台将视频保存为文件?
    EasyDSS视频平台是如何对直播流和点播流做处理的?
    EasyDSS视频平台点播页选中视频通道进行导出表单操作无反应排查
    如何允许EasyDSS在线课堂系统上传点播文件时文件名带有空格?
  • 原文地址:https://www.cnblogs.com/siaslfslovewp/p/6894023.html
Copyright © 2011-2022 走看看