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());
        }

  • 相关阅读:
    bzoj 4010: [HNOI2015]菜肴制作
    bzoj4827: [Hnoi2017]礼物
    bzoj3160: 万径人踪灭
    bzoj4503: 两个串
    bzoj2648: SJY摆棋子
    bzoj2780: [Spoj]8093 Sevenk Love Oimaster
    bzoj3926: [Zjoi2015]诸神眷顾的幻想乡
    MySQL:记录的增删改查、单表查询、约束条件、多表查询、连表、子查询、pymysql模块、MySQL内置功能
    MySQL数据库:SQL语句基础、库操作、表操作、数据类型、约束条件、表之间的关系
    网络编程进阶:并发编程之协程、IO模型
  • 原文地址:https://www.cnblogs.com/linsu/p/2491993.html
Copyright © 2011-2022 走看看