.net core使用MD5加密解密字符串
本文实例为大家分享了.netcore使用MD5加密解密字符串的具体代码,供大家参考,具体内容如下
//////加密 /// ///要加密的文本 /// 秘钥 /// publicstaticstringEncrypt(stringText,stringsKey="test") { DESCryptoServiceProviderdes=newDESCryptoServiceProvider(); byte[]inputByteArray; inputByteArray=Encoding.Default.GetBytes(Text); des.Key=ASCIIEncoding.ASCII.GetBytes(Md5Hash(sKey).Substring(0,8)); des.IV=ASCIIEncoding.ASCII.GetBytes(Md5Hash(sKey).Substring(0,8)); System.IO.MemoryStreamms=newSystem.IO.MemoryStream(); CryptoStreamcs=newCryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write); cs.Write(inputByteArray,0,inputByteArray.Length); cs.FlushFinalBlock(); StringBuilderret=newStringBuilder(); foreach(bytebinms.ToArray()) { ret.AppendFormat("{0:X2}",b); } md4j=ret.ToString(); returnret.ToString(); }
//////解密 /// ////// /// publicstaticstringDecrypt(stringText,stringsKey="test") { DESCryptoServiceProviderdes=newDESCryptoServiceProvider(); intlen; len=Text.Length/2; byte[]inputByteArray=newbyte[len]; intx,i; for(x=0;x //////32位MD5加密 /// ////// privatestaticstringMd5Hash(stringinput) { MD5CryptoServiceProvidermd5Hasher=newMD5CryptoServiceProvider(); byte[]data=md5Hasher.ComputeHash(Encoding.Default.GetBytes(input)); StringBuildersBuilder=newStringBuilder(); for(inti=0;i 调用加密解密看效果
publicstaticstringmd4j=""; staticvoidMain(string[]args) { //加密 Encrypt("123456"); Decrypt(md4j); }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。