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

MD5的简单用法

程序员文章站 2022-07-02 11:39:38
非常简单的MD5加密和解密(即用即copy) 点击帮助灯泡引用就可使用 //生成MD5帮助文件文件 public class MD5Help{ ///MD5加密 方法类 public static string MD5Encrypt(string pToEncrypt, string sKey) { ......

非常简单的md5加密和解密(即用即copy)

点击帮助灯泡引用就可使用

//生成md5帮助文件文件

public class md5help
{
  ///md5加密 方法类
  public static string md5encrypt(string ptoencrypt, string skey)
  {
  descryptoserviceprovider des = new descryptoserviceprovider();
  byte[] inputbytearray = encoding.default.getbytes(ptoencrypt);
  des.key = asciiencoding.ascii.getbytes(skey);
  des.iv = asciiencoding.ascii.getbytes(skey);
  memorystream ms = new memorystream();
  cryptostream cs = new cryptostream(ms, des.createencryptor(), cryptostreammode.write);
  cs.write(inputbytearray, 0, inputbytearray.length);
  cs.flushfinalblock();
  stringbuilder ret = new stringbuilder();
  foreach (byte b in ms.toarray())  
  {
  ret.appendformat("{0:x2}", b);
  }
  ret.tostring();
  return ret.tostring();

  }

  ///md5解密 方法类
  public static string md5decrypt(string ptodecrypt, string skey)
  {
  descryptoserviceprovider des = new descryptoserviceprovider();

  byte[] inputbytearray = new byte[ptodecrypt.length / 2];
  for (int x = 0; x < ptodecrypt.length / 2; x++)
  {
  int i = (convert.toint32(ptodecrypt.substring(x * 2, 2), 16));
  inputbytearray[x] = (byte)i;
  }

  des.key = asciiencoding.ascii.getbytes(skey);
  des.iv = asciiencoding.ascii.getbytes(skey);
  memorystream ms = new memorystream();
  cryptostream cs = new cryptostream(ms, des.createdecryptor(), cryptostreammode.write);
  cs.write(inputbytearray, 0, inputbytearray.length);
  cs.flushfinalblock();

  stringbuilder ret = new stringbuilder();

  return system.text.encoding.default.getstring(ms.toarray());
  }
}

-------------------------------------------------------------------------------------------------

使用:

string ipassword = md5help.md5encrypt(password, configurationmanager.appsettings["skey"].tostring()); //加密 后面的参数是密钥
string jpassword = md5help.md5decrypt(password, configurationmanager.appsettings["skey"].tostring()); //解密 后面的参数是密钥

webconfig配置:

<!--md5加密key-->
<add key="skey" value="jundaoxt"/>