欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET加密解密算法分享

程序员文章站 2024-02-14 17:44:10
#region des加密解密 /// /// des加密 /// ...
#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实现的加密解密方法
    /// <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";
      //  //构造对称算法
      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 一个简单的加密解密方法,在上一个的基础上支持中文
    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

以上所述就是本文的全部内容了,希望大家能够喜欢。