• c# DeS 加密解密字符串


     

    public static class DESSecurity

        {

            #region DES加密字符串

            /// <summary>

            /// DES加密方法

            /// </summary>

            /// <param name="plain">明文</param>

            /// <param name="key">密钥</param>

            /// <param name="iv">向量</param>

            /// <param name="isBase64">生成的密码是否用base64编码</param>

            /// <returns>密文</returns>

            public static String EncryptDES(string plain, byte[] rgbKey, byte[] rgbIV)

            {

                if (rgbKey.Length != 8) throw new ArgumentException("长度必须为8", "rgbKey");

                if (rgbIV.Length != 8) throw new ArgumentException("长度必须为8", "rgbIV");

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                MemoryStream ms = new MemoryStream();

                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);

                using (StreamWriter sw = new StreamWriter(cs))

                {

                    sw.Write(plain);

                    sw.Flush();

                    cs.FlushFinalBlock();

                    ms.Flush();

                    return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

                }

            }

            #endregion

     

            #region DES解密字符串

            /// <summary>

            /// DES解密方法

            /// </summary>

            /// <param name="cipher">密文</param>

            /// <param name="key">密钥</param>

            /// <param name="iv">向量</param>

            /// <param name="isBase64">密文是否用base64解码</param>

            /// <returns>明文</returns>

            public static String DecryptDES(string cipher, byte[] key64, byte[] iv64)

            {

                byte[] bytesCipher;

                bytesCipher = Convert.FromBase64String(cipher);

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                MemoryStream ms = new MemoryStream(bytesCipher);

                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key64, iv64), CryptoStreamMode.Read);

                using (StreamReader sr = new StreamReader(cs))

                {

                    return sr.ReadToEnd();

                }

            }

            #endregion

        }

  • 相关阅读:
    张艾迪(创始人):AOOOiA.global因梦想而诞生
    openx中如何使用site-variable(参数)限制(关键词)广告显示
    那些支持图片外链的免费相册
    php获取html纯文本,解决编辑器手动键入空格造成的无意义空白字符(空值问题)
    ios7迎来完美越狱,果粉狂欢!
    如何记录搜索引擎爬行记录php版
    购物返现网址大全
    jquery .post .get中文参数乱码解决方法详解
    【BUG】wego购物分享系统未登陆分享宝贝时查看宝贝自动新增产品数据
    让wego微购购物分享系统采集拍拍数据功能之腾讯paipai功能采集插件
  • 原文地址:https://www.cnblogs.com/xianzuoqiaoqi/p/1644207.html
Copyright © 2020-2023  润新知