MD5的简单用法
非常简单的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"/>