主頁 > 知識庫 > ASP.NET加密解密算法分享

ASP.NET加密解密算法分享

熱門標(biāo)簽:寧夏機器人電銷 t3出行地圖標(biāo)注怎么做 400電話辦理最優(yōu)質(zhì) 河南語音外呼系統(tǒng)公司 400免費電話怎么辦理 河北網(wǎng)絡(luò)回撥外呼系統(tǒng) 外呼電銷機器人軟件 關(guān)于宗地圖標(biāo)注技術(shù)規(guī)范 威海電銷
#region DES加密解密
    /// summary>
    /// DES加密
    /// /summary>
    /// param name="strSource">待加密字串/param>
    /// param name="key">32位Key值/param>
    /// returns>加密后的字符串/returns>
    public string DESEncrypt(string strSource)
    {
      return DESEncrypt(strSource, DESKey);
    }
    public string DESEncrypt(string strSource, byte[] key)
    {
      SymmetricAlgorithm sa = Rijndael.Create();
      sa.Key = key;
      sa.Mode = CipherMode.ECB;
      sa.Padding = PaddingMode.Zeros;
      MemoryStream ms = new MemoryStream();
      CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
      byte[] byt = Encoding.Unicode.GetBytes(strSource);
      cs.Write(byt, 0, byt.Length);
      cs.FlushFinalBlock();
      cs.Close();
      return Convert.ToBase64String(ms.ToArray());
    }
    /// summary>
    /// DES解密
    /// /summary>
    /// param name="strSource">待解密的字串/param>
    /// param name="key">32位Key值/param>
    /// returns>解密后的字符串/returns>
    public string DESDecrypt(string strSource)
    {
      return DESDecrypt(strSource, DESKey);
    }
    public string DESDecrypt(string strSource, byte[] key)
    {
      SymmetricAlgorithm sa = Rijndael.Create();
      sa.Key = key;
      sa.Mode = CipherMode.ECB;
      sa.Padding = PaddingMode.Zeros;
      ICryptoTransform ct = sa.CreateDecryptor();
      byte[] byt = Convert.FromBase64String(strSource);
      MemoryStream ms = new MemoryStream(byt);
      CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read);
      StreamReader sr = new StreamReader(cs, Encoding.Unicode);
      return sr.ReadToEnd();
    }
    #endregion
    #region 一個用hash實現(xiàn)的加密解密方法
    /// summary>
    /// 加密
    /// /summary>
    /// param name="src">/param>
    /// returns>/returns>
    public static string EncryptStrByHash(string src)
    {
      if (src.Length == 0)
      {
        return "";
      }
      byte[] HaKey = System.Text.Encoding.ASCII.GetBytes((src + "Test").ToCharArray());
      byte[] HaData = new byte[20];
      HMACSHA1 Hmac = new HMACSHA1(HaKey);
      CryptoStream cs = new CryptoStream(Stream.Null, Hmac, CryptoStreamMode.Write);
      try
      {
        cs.Write(HaData, 0, HaData.Length);
      }
      finally
      {
        cs.Close();
      }
      string HaResult = System.Convert.ToBase64String(Hmac.Hash).Substring(0, 16);
      byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(HaResult.ToCharArray());
      byte[] RiDataBuf = System.Text.Encoding.ASCII.GetBytes(src.ToCharArray());
      byte[] EncodedBytes =  { };
      MemoryStream ms = new MemoryStream();
      RijndaelManaged rv = new RijndaelManaged();
      cs = new CryptoStream(ms, rv.CreateEncryptor(RiKey, RiKey), CryptoStreamMode.Write);
      try
      {
        cs.Write(RiDataBuf, 0, RiDataBuf.Length);
        cs.FlushFinalBlock();
        EncodedBytes = ms.ToArray();
      }
      finally
      {
        ms.Close();
        cs.Close();
      }
      return HaResult + System.Convert.ToBase64String(EncodedBytes);
    }
    /// summary>
    /// 解密
    /// /summary>
    /// param name="src">/param>
    /// returns>/returns>
    public static string DecrypStrByHash(string src)
    {
      if (src.Length  40) return "";
      byte[] SrcBytes = System.Convert.FromBase64String(src.Substring(16));
      byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(src.Substring(0, 16).ToCharArray());
      byte[] InitialText = new byte[SrcBytes.Length];
      RijndaelManaged rv = new RijndaelManaged();
      MemoryStream ms = new MemoryStream(SrcBytes);
      CryptoStream cs = new CryptoStream(ms, rv.CreateDecryptor(RiKey, RiKey), CryptoStreamMode.Read);
      try
      {
        cs.Read(InitialText, 0, InitialText.Length);
      }
      finally
      {
        ms.Close();
        cs.Close();
      }
      System.Text.StringBuilder Result = new System.Text.StringBuilder();
      for (int i = 0; i  InitialText.Length; ++i) if (InitialText[i] > 0) Result.Append((char)InitialText[i]);
      return Result.ToString();
    }
    /// summary>
    /// 對加密后的密文重新編碼,如果密文長>16,則去掉前16個字符,如果長度小于16,返回空字符串
    /// /summary>
    /// param name="s">/param>
    /// returns>/returns>
    public string ReEncryptStrByHash(string s)
    {
      string e = Encrypt.EncryptStrByHash(s);
      return ((e.Length > 16) ? e.Substring(16) : "");
    }
    #endregion
    #region Md5加密,生成16位或32位,生成的密文都是大寫
    public static string Md5To16(string str)
    {
      MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
      string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), 4, 8);
      t2 = t2.Replace("-", "");
      return t2;
    }
    //// summary>
    /// MD5 32位加密
    /// /summary>
    /// param name="str">/param>
    /// returns>/returns>
    public static string Md5To32(string str)
    {
      string pwd = "";
      MD5 md5 = MD5.Create();
      byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
      for (int i = 0; i  s.Length; i++)
      {
        pwd = pwd + s[i].ToString("X");
      }
      return pwd;
    }
    #endregion
    #region 3DES加密解密
    public string Encrypt3DES(string str)
    {
      //密鑰
      string sKey = "wyw308";
      //  //矢量,可為空
      string sIV = "scf521";
      //  //構(gòu)造對稱算法
      SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
      ICryptoTransform ct;
      MemoryStream ms;
      CryptoStream cs;
      byte[] byt;
      mCSP.Key = Convert.FromBase64String(sKey);
      mCSP.IV = Convert.FromBase64String(sIV);
      mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
      mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
      ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
      byt = Encoding.UTF8.GetBytes(str);
      ms = new MemoryStream();
      cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
      cs.Write(byt, 0, byt.Length);
      cs.FlushFinalBlock();
      cs.Close();
      return Convert.ToBase64String(ms.ToArray());
    }
    /// summary>
    /// 帶指定密鑰和矢量的3DES加密
    /// /summary>
    /// param name="str">/param>
    /// param name="sKey">/param>
    /// param name="sIV">/param>
    /// returns>/returns>
    public string Encrypt3DES(string str, string sKey, string sIV)
    {
      SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
      ICryptoTransform ct;
      MemoryStream ms;
      CryptoStream cs;
      byte[] byt;
      mCSP.Key = Convert.FromBase64String(sKey);
      mCSP.IV = Convert.FromBase64String(sIV);
      mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
      mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
      ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
      byt = Encoding.UTF8.GetBytes(str);
      ms = new MemoryStream();
      cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
      cs.Write(byt, 0, byt.Length);
      cs.FlushFinalBlock();
      cs.Close();
      return Convert.ToBase64String(ms.ToArray());
    }
    //解密
    public string Decrypt3DES(string Value)
    {
      string sKey = "wyw308";
      string sIV = "scf521";
      SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
      ICryptoTransform ct;
      MemoryStream ms;
      CryptoStream cs;
      byte[] byt;
      mCSP.Key = Convert.FromBase64String(sKey);
      mCSP.IV = Convert.FromBase64String(sIV);
      mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
      mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
      ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
      byt = Convert.FromBase64String(Value);
      ms = new MemoryStream();
      cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
      cs.Write(byt, 0, byt.Length);
      cs.FlushFinalBlock();
      cs.Close();
      return Encoding.UTF8.GetString(ms.ToArray());
    }
    /// summary>
    /// 帶指定密鑰和矢量的3DES解密
    /// /summary>
    /// param name="Value">/param>
    /// param name="sKey">/param>
    /// param name="sIV">/param>
    /// returns>/returns>
    public string Decrypt3DES(string str, string sKey, string sIV)
    {
      SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
      ICryptoTransform ct;
      MemoryStream ms;
      CryptoStream cs;
      byte[] byt;
      mCSP.Key = Convert.FromBase64String(sKey);
      mCSP.IV = Convert.FromBase64String(sIV);
      mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
      mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
      ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
      byt = Convert.FromBase64String(str);
      ms = new MemoryStream();
      cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
      cs.Write(byt, 0, byt.Length);
      cs.FlushFinalBlock();
      cs.Close();
      return Encoding.UTF8.GetString(ms.ToArray());
    }
    #endregion
    #region 一個簡單的加密解密方法,只支持英文
    public static string EnCryptEnStr(string str) //倒序加1加密
    {
      byte[] by = new byte[str.Length];
      for (int i = 0;
       i = str.Length - 1;
       i++)
      {
        by[i] = (byte)((byte)str[i] + 1);
      }
      str = "";
      for (int i = by.Length - 1;
       i >= 0;
       i--)
      {
        str += ((char)by[i]).ToString();
      }
      return str;
    }
    public static string DeCryptEnStr(string str) //順序減1解碼
    {
      byte[] by = new byte[str.Length];
      for (int i = 0;
       i = str.Length - 1;
       i++)
      {
        by[i] = (byte)((byte)str[i] - 1);
      }
      str = "";
      for (int i = by.Length - 1;
       i >= 0;
       i--)
      {
        str += ((char)by[i]).ToString();
      }
      return str;
    }
    #endregion
    #region 一個簡單的加密解密方法,在上一個的基礎(chǔ)上支持中文
    public static string EnCryptCnStr(string str)
    {
      string htext = ""; // blank text
      for (int i = 0; i  str.Length; i++)
      {
        htext = htext + (char)(str[i] + 10 - 1 * 2);
      }
      return htext;
    }
    public static string DeCryptCnStr(string str)
    {
      string dtext = "";
      for (int i = 0; i  str.Length; i++)
      {
        dtext = dtext + (char)(str[i] - 10 + 1 * 2);
      }
      return dtext;
    }
    #endregion
    #region Url地址編碼解碼
    /// summary>
    /// 編碼Url地址
    /// /summary>
    /// param name="url">/param>
    /// returns>/returns>
    public static string UrlEncode(string url)
    {
      byte[] mByte = null;
      mByte = System.Text.Encoding.GetEncoding("GB2312").GetBytes(url);
      return System.Web.HttpUtility.UrlEncode(mByte);
    }
    /// summary>
    /// 解碼Url地址
    /// /summary>
    /// param name="url">/param>
    /// returns>/returns>
    public static string UrlDecode(string url)
    {
      return HttpUtility.UrlDecode(url, System.Text.Encoding.GetEncoding("GB2312"));
    }
    #endregion

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

您可能感興趣的文章:
  • ASP.NET MVC結(jié)合JavaScript登錄、校驗和加密
  • asp.net的加密解密技巧
  • 在asp.net中使用加密數(shù)據(jù)庫聯(lián)接字符串保證數(shù)據(jù)安全
  • asp.net實現(xiàn)md5加密
  • 在ASP.Net中實現(xiàn)RSA加密的方法
  • 解析Asp.net,C# 純數(shù)字加密解密字符串的應(yīng)用
  • Asp.net,C# 加密解密字符串的使用詳解
  • ASP.NET加密口令的方法實例
  • asp.net中使用cookie與md5加密實現(xiàn)記住密碼功能的實現(xiàn)代碼
  • Asp.net中Microsoft.Identity的IPasswordHasher加密的默認實現(xiàn)與運用

標(biāo)簽:吉林 廣元 樂山 賀州 淮北 固原 咸寧 池州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET加密解密算法分享》,本文關(guān)鍵詞  ASP.NET,加密解密,算法,分享,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET加密解密算法分享》相關(guān)的同類信息!
  • 本頁收集關(guān)于ASP.NET加密解密算法分享的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章