﻿{"id":648,"date":"2010-06-20T19:49:00","date_gmt":"2010-06-20T11:49:00","guid":{"rendered":"http:\/\/blog.jixin.ntxz.net\/?p=648"},"modified":"2010-07-30T15:38:50","modified_gmt":"2010-07-30T07:38:50","slug":"%e5%ae%8c%e6%95%b4%e5%8a%a0%e5%af%86%e7%b1%bb%ef%bc%88%e6%8f%90%e4%be%9bmd5%ef%bc%8csha1%ef%bc%8csha256%ef%bc%8csha512%ef%bc%8cdes%e5%8a%a0%e5%af%86%e5%92%8c%e8%a7%a3%e5%af%86%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.ntxz.net\/?p=648","title":{"rendered":"\u5b8c\u6574\u52a0\u5bc6\u7c7b\uff08\u63d0\u4f9bMD5\uff0cSHA1\uff0cSHA256\uff0cSHA512\uff0cDES\u52a0\u5bc6\u548c\u89e3\u5bc6\uff09"},"content":{"rendered":"<p>using System;<br \/>using System.IO;[s] [\/s]<br \/>using System.Da<wbr>ta;<br \/>using System.Text;<br \/>using System.Diagnostics;<br \/>using System.Security;<br \/>using System.Security.Cryptography;<\/p>\n<p>namespace com.Quickline.Encrypt<br \/>{<br \/>&nbsp;\/\/\/ &lt;summary&gt;<br \/>&nbsp;\/\/\/ \u7c7b\u540d\uff1aHashEncrypt<br \/>&nbsp;\/\/\/ \u4f5c\u7528\uff1a\u5bf9\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u8fdb\u884cHash\u8fd0\u7b97\uff0c\u8fd4\u56de\u901a\u8fc7Hash\u7b97\u6cd5\u52a0\u5bc6\u8fc7\u7684\u5b57\u4e32\u3002<br \/>&nbsp;\/\/\/ \u5c5e\u6027\uff1a\uff3b\u65e0\uff3d<br \/>&nbsp;\/\/\/ \u6784\u9020\u51fd\u6570\u989d\u53c2\u6570\uff1a<br \/>&nbsp;\/\/\/ IsReturnNum:\u662f\u5426\u8fd4\u56de\u4e3a\u52a0\u5bc6\u540e\u5b57\u7b26\u7684Byte\u4ee3\u7801<br \/>&nbsp;\/\/\/ IsCaseSensitive\uff1a\u662f\u5426\u533a\u5206\u5927\u5c0f\u5199\u3002<br \/>&nbsp;\/\/\/ \u65b9\u6cd5\uff1a\u6b64\u7c7b\u63d0\u4f9bMD5\uff0cSHA1\uff0cSHA256\uff0cSHA512\u7b49\u56db\u79cd\u7b97\u6cd5\uff0c\u52a0\u5bc6\u5b57\u4e32\u7684\u957f<br \/>\u5ea6\u4f9d\u6b21\u589e\u5927\u3002<br \/>&nbsp;\/\/\/ &lt;\/summary&gt;<br \/>&nbsp;public class HashEncrypt<br \/>&nbsp;{<br \/>&nbsp;\/\/private string strIN;<br \/>&nbsp;private bool isReturnNum;<br \/>&nbsp;private bool isCaseSensitive;<br \/>&nbsp;<br \/>&nbsp;public HashEncrypt(bool IsCaseSensitive,bool IsReturnNum)<br \/>&nbsp;{<br \/>&nbsp;this.isReturnNum = IsReturnNum;<br \/>&nbsp;this.isCaseSensitive = IsCaseSensitive;<br \/>&nbsp;}<br \/>&nbsp;<br \/>&nbsp;<br \/>&nbsp;private string getstrIN(string strIN)<br \/>&nbsp;{<br \/>&nbsp;\/\/string strIN = strIN;<br \/>&nbsp;if (strIN.Length == 0)<br \/>&nbsp;{<br \/>&nbsp;strIN = &#8220;~NULL~&#8221;;<br \/>&nbsp;}<br \/>&nbsp;if (isCaseSensitive == false)<br \/>&nbsp;{<br \/>&nbsp;strIN = strIN.ToUpper();<br \/>&nbsp;}<br \/>&nbsp;return strIN;<br \/>&nbsp;}<br \/>&nbsp;public string MD5Encrypt(string strIN)<br \/>&nbsp;{<br \/>&nbsp;\/\/string strIN = getstrIN(strIN);<br \/>&nbsp;byte[] tmpByte;<br \/>&nbsp;MD5 md5 = new MD5CryptoServiceProvider();<br \/>&nbsp;tmpByte =<br \/>md5.ComputeHash(GetKeyByteArray(getstrIN(strIN)));<br \/>&nbsp;md5.Clear();<\/p>\n<p>&nbsp;return GetStringValue(tmpByte);<\/p>\n<p>&nbsp;}<br \/>&nbsp;<br \/>&nbsp;public string SHA1Encrypt(string strIN)<br \/>&nbsp;{<br \/>&nbsp;\/\/string strIN = getstrIN(strIN);<br \/>&nbsp;byte[] tmpByte;<br \/>&nbsp;SHA1 sha1 = new SHA1CryptoServiceProvider();<\/p>\n<p>&nbsp;tmpByte = sha1.ComputeHash(GetKeyByteArray(strIN));<br \/>&nbsp;sha1.Clear();<\/p>\n<p>&nbsp;return GetStringValue(tmpByte);<\/p>\n<p>&nbsp;}<\/p>\n<p>&nbsp;public string SHA256Encrypt(string strIN)<br \/>&nbsp;{<br \/>&nbsp;\/\/string strIN = getstrIN(strIN);<br \/>&nbsp;byte[] tmpByte;<br \/>&nbsp;SHA256 sha256 = new SHA256Managed();<\/p>\n<p>&nbsp;tmpByte =<br \/>sha256.ComputeHash(GetKeyByteArray(strIN));<br \/>&nbsp;sha256.Clear();<\/p>\n<p>&nbsp;return GetStringValue(tmpByte);<\/p>\n<p>&nbsp;}<\/p>\n<p>&nbsp;public string SHA512Encrypt(string strIN)<br \/>&nbsp;{<br \/>&nbsp;\/\/string strIN = getstrIN(strIN);<br \/>&nbsp;byte[] tmpByte;<br \/>&nbsp;SHA512 sha512 = new SHA512Managed();<\/p>\n<p>&nbsp;tmpByte =<br \/>sha512.ComputeHash(GetKeyByteArray(strIN));<br \/>&nbsp;sha512.Clear();<\/p>\n<p>&nbsp;return GetStringValue(tmpByte);<\/p>\n<p>&nbsp;}<br \/>&nbsp;<br \/>&nbsp;\/\/\/ &lt;summary&gt;<br \/>&nbsp;\/\/\/ \u4f7f\u7528DES\u52a0\u5bc6\uff08Added by niehl 2005-4-6\uff09<br \/>&nbsp;\/\/\/ &lt;\/summary&gt;<br \/>&nbsp;\/\/\/ &lt;param name=&#8221;originalValue&#8221;&gt;\u5f85\u52a0\u5bc6\u7684\u5b57\u7b26\u4e32&lt;\/param&gt;<br \/>&nbsp;\/\/\/ &lt;param name=&#8221;key&#8221;&gt;\u5bc6\u94a5(\u6700\u5927\u957f\u5ea68)&lt;\/param&gt;<br \/>&nbsp;\/\/\/ &lt;param name=&#8221;IV&#8221;&gt;\u521d\u59cb\u5316\u5411\u91cf(\u6700\u5927\u957f\u5ea68)&lt;\/param&gt;<br \/>&nbsp;\/\/\/ &lt;returns&gt;\u52a0\u5bc6\u540e\u7684\u5b57\u7b26\u4e32&lt;\/returns&gt;<br \/>&nbsp;public string DESEncrypt(string originalValue,string key,string IV)<br \/>&nbsp;{<br \/>&nbsp;\/\/\u5c06key\u548cIV\u5904\u7406\u62108\u4e2a\u5b57\u7b26<br \/>&nbsp;key += &#8220;12345678&#8221;;<br \/>&nbsp;IV += &#8220;12345678&#8221;;<br \/>&nbsp;key = key.Substring(0,8);<br \/>&nbsp;IV = IV.Substring(0,8);<\/p>\n<p>&nbsp;SymmetricAlgorithm sa;<br \/>&nbsp;ICryptoTransform ct;<br \/>&nbsp;MemoryStream ms;<br \/>&nbsp;CryptoStream cs;<br \/>&nbsp;byte[] byt;<\/p>\n<p>&nbsp;sa = new DESCryptoServiceProvider();<br \/>&nbsp;sa.Key = Encoding.UTF8.GetBytes(key);<br \/>&nbsp;sa.IV = Encoding.UTF8.GetBytes(IV);<br \/>&nbsp;ct = sa.CreateEncryptor();<\/p>\n<p>&nbsp;byt = Encoding.UTF8.GetBytes(originalValue);<\/p>\n<p>&nbsp;ms = new MemoryStream();<br \/>&nbsp;cs = new CryptoStream(ms, ct,<br \/>CryptoStreamMode.Write);<br \/>&nbsp;cs.Write(byt, 0, byt.Length);<br \/>&nbsp;cs.FlushFinalBlock();<\/p>\n<p>&nbsp;cs.Close();<\/p>\n<p>&nbsp;return Convert.ToBase64String(ms.ToArray());<\/p>\n<p>&nbsp;}<\/p>\n<p>&nbsp;public string DESEncrypt(string originalValue,string key)<br \/>&nbsp;{<br \/>&nbsp;return DESEncrypt(originalValue,key,key);<br \/>&nbsp;}<\/p>\n<p>&nbsp;\/\/\/ &lt;summary&gt;<br \/>&nbsp;\/\/\/ \u4f7f\u7528DES\u89e3\u5bc6\uff08Added by niehl 2005-4-6\uff09<br \/>&nbsp;\/\/\/ &lt;\/summary&gt;<br \/>&nbsp;\/\/\/ &lt;param name=&#8221;encryptedValue&#8221;&gt;\u5f85\u89e3\u5bc6\u7684\u5b57\u7b26\u4e32&lt;\/param&gt;<br \/>&nbsp;\/\/\/ &lt;param name=&#8221;key&#8221;&gt;\u5bc6\u94a5(\u6700\u5927\u957f\u5ea68)&lt;\/param&gt;<br \/>&nbsp;\/\/\/ &lt;param name=&#8221;IV&#8221;&gt;m\u521d\u59cb\u5316\u5411\u91cf(\u6700\u5927\u957f\u5ea68)&lt;\/param&gt;<br \/>&nbsp;\/\/\/ &lt;returns&gt;\u89e3\u5bc6\u540e\u7684\u5b57\u7b26\u4e32&lt;\/returns&gt;<br \/>&nbsp;public string DESDecrypt(string encryptedValue,string key,string IV)<br \/>&nbsp;{<br \/>&nbsp;\/\/\u5c06key\u548cIV\u5904\u7406\u62108\u4e2a\u5b57\u7b26<br \/>&nbsp;key += &#8220;12345678&#8221;;<br \/>&nbsp;IV += &#8220;12345678&#8221;;<br \/>&nbsp;key = key.Substring(0,8);<br \/>&nbsp;IV = IV.Substring(0,8);<\/p>\n<p>&nbsp;SymmetricAlgorithm sa;<br \/>&nbsp;ICryptoTransform ct;<br \/>&nbsp;MemoryStream ms;<br \/>&nbsp;CryptoStream cs;<br \/>&nbsp;byte[] byt;<\/p>\n<p>&nbsp;sa = new DESCryptoServiceProvider();<br \/>&nbsp;sa.Key = Encoding.UTF8.GetBytes(key);<br \/>&nbsp;sa.IV = Encoding.UTF8.GetBytes(IV);<br \/>&nbsp;ct = sa.CreateDecryptor();<\/p>\n<p>&nbsp;byt = Convert.FromBase64String(encryptedValue);<\/p>\n<p>&nbsp;ms = new MemoryStream();<br \/>&nbsp;cs = new CryptoStream(ms, ct,<br \/>CryptoStreamMode.Write);<br \/>&nbsp;cs.Write(byt, 0, byt.Length);<br \/>&nbsp;cs.FlushFinalBlock();<\/p>\n<p>&nbsp;cs.Close();<\/p>\n<p>&nbsp;return Encoding.UTF8.GetString(ms.ToArray());<\/p>\n<p>&nbsp;}<\/p>\n<p>&nbsp;public string DESDecrypt(string encryptedValue,string key)<br \/>&nbsp;{<br \/>&nbsp;return DESDecrypt(encryptedValue,key,key);<br \/>&nbsp;}<\/p>\n<p>&nbsp;private string GetStringValue(byte[] Byte)<br \/>&nbsp;{<br \/>&nbsp;string tmpString = &#8220;&#8221;;<\/p>\n<p>&nbsp;if (this.isReturnNum == false)<br \/>&nbsp;{<br \/>&nbsp;ASCIIEncoding Asc = new ASCIIEncoding();<br \/>&nbsp;tmpString = Asc.GetString(Byte);<br \/>&nbsp;}<br \/>&nbsp;else<br \/>&nbsp;{<br \/>&nbsp;int iCounter;<\/p>\n<p>&nbsp;for<br \/>(iCounter=0;iCounter&lt;Byte.Length;iCounter++)<br \/>&nbsp;{<br \/>&nbsp;tmpString = tmpString +<br \/>Byte[iCounter].ToString();<br \/>&nbsp;}<br \/>&nbsp;<br \/>&nbsp;}<br \/>&nbsp;<br \/>&nbsp;return tmpString;<br \/>&nbsp;}<\/p>\n<p>&nbsp;private byte[] GetKeyByteArray(string strKey)<br \/>&nbsp;{<\/p>\n<p>&nbsp;ASCIIEncoding Asc = new ASCIIEncoding();<br \/>&nbsp;<br \/>&nbsp;int tmpStrLen = strKey.Length;<br \/>&nbsp;byte[] tmpByte = new byte[tmpStrLen-1];<\/p>\n<p>&nbsp;tmpByte = Asc.GetBytes(strKey);<\/p>\n<p>&nbsp;return tmpByte;<\/p>\n<p>&nbsp;}<\/p>\n<p>&nbsp;}<br \/>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using System;using System.IO;[s] [\/s]using System.Data; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,8],"tags":[],"class_list":["post-648","post","type-post","status-publish","format-standard","hentry","category-1","category-oldblog"],"views":762,"_links":{"self":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=648"}],"version-history":[{"count":1,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/648\/revisions"}],"predecessor-version":[{"id":744,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/648\/revisions\/744"}],"wp:attachment":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=648"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}