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

ASP.NET中DES加密与解密MD5加密帮助类的实现代码

程序员文章站 2023-02-15 19:32:22
public class trialhelper { //默认密钥向量 private static byte[] keys = { 0x12...
public class trialhelper
  {    //默认密钥向量
    private static byte[] keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
    /// <summary>
    /// des加密字符串
    /// </summary>
    /// <param name="encryptstring">待加密的字符串</param>
    /// <param name="encryptkey">加密密钥,要求为8位</param>
    /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
    public static string encryptdes( string encryptstring, string encryptkey = "" )
    {
      try
      {
        if (string.isnullorempty(encryptkey) || encryptkey.length < 8)
        {
          encryptkey = "winform01";
        }
        byte[] rgbkey = encoding.utf8.getbytes(encryptkey.substring(0, 8));
        byte[] rgbiv = keys;
        byte[] inputbytearray = encoding.utf8.getbytes(encryptstring);
        descryptoserviceprovider dcsp = new descryptoserviceprovider();
        memorystream mstream = new memorystream();
        cryptostream cstream = new cryptostream(mstream, dcsp.createencryptor(rgbkey, rgbiv), cryptostreammode.write);
        cstream.write(inputbytearray, 0, inputbytearray.length);
        cstream.flushfinalblock();
        return convert.tobase64string(mstream.toarray());
      }
      catch
      {
        return encryptstring;
      }
    }
    /// <summary>
    /// des解密字符串
    /// </summary>
    /// <param name="decryptstring">待解密的字符串</param>
    /// <param name="decryptkey">解密密钥,要求为8位,和加密密钥相同</param>
    /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
    public static string decryptdes( string decryptstring, string decryptkey = "" )
    {
      try
      {
        if (string.isnullorempty(decryptkey) || decryptkey.length < 8)
        {
          decryptkey = "winform01";
        }
        byte[] rgbkey = encoding.utf8.getbytes(decryptkey.substring(0, 8));
        byte[] rgbiv = keys;
        byte[] inputbytearray = convert.frombase64string(decryptstring);
        descryptoserviceprovider dcsp = new descryptoserviceprovider();
        memorystream mstream = new memorystream();
        cryptostream cstream = new cryptostream(mstream, dcsp.createdecryptor(rgbkey, rgbiv), cryptostreammode.write);
        cstream.write(inputbytearray, 0, inputbytearray.length);
        cstream.flushfinalblock();
        return encoding.utf8.getstring(mstream.toarray());
      }
      catch
      {
        return decryptstring;
      }
    }
    /// <summary>
    /// md5数据加密
    /// </summary>
    /// <param name="sdatain">加密字段</param>
    /// <returns>加密后的字符串</returns>
    public static string getmd5( string sdatain )
    {
      system.security.cryptography.md5cryptoserviceprovider md5 = new system.security.cryptography.md5cryptoserviceprovider();
      byte[] bytvalue, bythash;
      bytvalue = system.text.encoding.utf8.getbytes(sdatain);
      bythash = md5.computehash(bytvalue);
      md5.clear();
      string stemp = "";
      for (int i = 0; i < bythash.length; i++)
      {
        stemp += bythash[i].tostring("x").padleft(2, '0');
      }
      return stemp;
    }
  }

    调用:              

//获取登录信息
        loginrecord.name = tbname.text.trim();
        loginrecord.md5pwd = trialhelper.getmd5(tbpwd.password); //保存到数据库md5加密方式
        loginrecord.pwd = trialhelper.encryptdes(tbpwd.password);//记住密码des加密方式,保存到本地

以上所述是小编给大家介绍的asp.net中des加密与解密md5加密帮助类,希望对大家有所帮助