zoukankan      html  css  js  c++  java
  • C# 常用加密方法

    1.一般加密用户的敏感数据,都采用不可逆的加密方式如:MD5

      示例:MD5

        System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("lin.su", "MD5");

       HSA1:

       System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("lin.su", "SHA1");

    2.加密字符串

      示例:

      /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="str">加密字符串对象</param>
        /// <param name="keys">密钥</param>
        /// <param name="rgbIv">初始化向量</param>
        /// <returns></returns>
        string EncryString(string str, byte[] keys, byte[] rgbIv)
        {
           
            byte[] strs = System.Text.Encoding.Unicode.GetBytes(str);

            //定义加密数据标准 DESCryptoServiceProvider
            System.Security.Cryptography.DESCryptoServiceProvider desc = new      System.Security.Cryptography.DESCryptoServiceProvider();
            System.IO.MemoryStream mStream = new System.IO.MemoryStream();

            System.Security.Cryptography.ICryptoTransform transform = desc.CreateEncryptor(keys, rgbIv);//加密对象
            System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, transform, System.Security.Cryptography.CryptoStreamMode.Write);
            cStream.Write(strs, 0, strs.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }

    /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="str">解密字符串对象</param>
        /// <param name="keys">密钥</param>
        /// <param name="rgbIv">初始化向量</param>
        /// <returns></returns>
        string DecryString(string str, byte[] keys, byte[] rgbIv)
        {
            byte[] strs = Convert.FromBase64String(str);

            System.Security.Cryptography.DESCryptoServiceProvider desc = new System.Security.Cryptography.DESCryptoServiceProvider();
            System.IO.MemoryStream mStream = new System.IO.MemoryStream();

            System.Security.Cryptography.ICryptoTransform transform = desc.CreateDecryptor(keys, rgbIv);//解密对象

            System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, transform, System.Security.Cryptography.CryptoStreamMode.Write);
            cStream.Write(strs, 0, strs.Length);
            cStream.FlushFinalBlock();
            return System.Text.Encoding.Unicode.GetString(mStream.ToArray());
        }

  • 相关阅读:
    1.两数之和
    [Udemy] ES 7 and Elastic Stack
    [Udemy] ES 7 and Elastic Stack
    Common Linux Commands 日常工作常用Linux命令
    ELK 学习
    web 3d 技术预研及数据可视化技术
    AWS Cloud Practioner 官方课程笔记
    怎么用 pytorch 查看 GPU 信息
    ECG 项目预研
    怎么查看keras 或者 tensorflow 正在使用的GPU
  • 原文地址:https://www.cnblogs.com/linsu/p/2491993.html
Copyright © 2011-2022 走看看