{"id":736,"date":"2009-11-14T19:33:00","date_gmt":"2009-11-14T11:33:00","guid":{"rendered":""},"modified":"2013-11-17T12:34:05","modified_gmt":"2013-11-17T04:34:05","slug":"c%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/736.html","title":{"rendered":"C#\u52a0\u5bc6\u7b97\u6cd5\u603b\u7ed3"},"content":{"rendered":"<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class CryptUtil\r\n    {\r\n        public static string DecryptString(string input)\r\n        {\r\n            if (input.Equals(string.Empty))\r\n            {\r\n                return input;\r\n            } \r\n            byte&#x5B;] byKey = { 0x63, 0x68, 0x65, 0x6E, 0x79, 0x75, 0x61, 0x6E };\r\n            byte&#x5B;] IV = { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 };\r\n            byte&#x5B;] inputByteArray = new Byte&#x5B;input.Length];\r\n            DESCryptoServiceProvider des = new DESCryptoServiceProvider();\r\n            inputByteArray = Convert.FromBase64String(input);\r\n            MemoryStream ms = new MemoryStream();\r\n            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);\r\n            cs.Write(inputByteArray, 0, inputByteArray.Length);\r\n            cs.FlushFinalBlock();\r\n            Encoding encoding = new UTF8Encoding();\r\n            return encoding.GetString(ms.ToArray());\r\n        }\r\n\r\n        public static string EncryptString(string input)\r\n        {\r\n            if (input.Equals(string.Empty))\r\n            {\r\n                return input;\r\n            }\r\n\r\n            byte&#x5B;] byKey = { 0x63, 0x68, 0x65, 0x6E, 0x79, 0x75, 0x61, 0x6E };\r\n            byte&#x5B;] IV = { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 };\r\n            DESCryptoServiceProvider des = new DESCryptoServiceProvider();\r\n            byte&#x5B;] inputByteArray = Encoding.UTF8.GetBytes(input);\r\n            MemoryStream ms = new MemoryStream();\r\n            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);\r\n            cs.Write(inputByteArray, 0, inputByteArray.Length);\r\n            cs.FlushFinalBlock();\r\n            return Convert.ToBase64String(ms.ToArray());\r\n        }\r\n        \/**\/\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ DES + Base64 \u52a0\u5bc6\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;input&quot;&gt;\u660e\u6587\u5b57\u7b26\u4e32&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;\u5df2\u52a0\u5bc6\u5b57\u7b26\u4e32&lt;\/returns&gt;\r\n        public static string DesBase64Encrypt(string input)\r\n        {\r\n            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();\r\n            des.Mode = System.Security.Cryptography.CipherMode.ECB;\r\n            ICryptoTransform ct;\r\n            MemoryStream ms;\r\n            CryptoStream cs;\r\n            byte&#x5B;] byt;\r\n            byte&#x5B;] Key = new byte&#x5B;8] { 56, 50, 55, 56, 56, 55, 49, 49 };\r\n            byte&#x5B;] IV = new byte&#x5B;8] { 0, 0, 0, 0, 0, 0, 0, 0 };\r\n\r\n            ct = des.CreateEncryptor(Key, IV);\r\n\r\n            byt = Encoding.GetEncoding(&quot;GB2312&quot;).GetBytes(input); \/\/\u6839\u636e GB2312 \u7f16\u7801\u5bf9\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u8f6c\u6362\u6210 byte \u6570\u7ec4\r\n\r\n            ms = new MemoryStream();\r\n            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);\r\n            cs.Write(byt, 0, byt.Length);\r\n            cs.FlushFinalBlock();\r\n\r\n            cs.Close();\r\n\r\n            byte&#x5B;] answer = ms.ToArray();\r\n            for (int j = 0; j &lt; answer.Length; j++)\r\n            {\r\n                Console.Write(answer&#x5B;j].ToString() + &quot; &quot;);\r\n            }\r\n            Console.WriteLine();\r\n            return Convert.ToBase64String(ms.ToArray()); \/\/ \u5c06\u52a0\u5bc6\u7684 byte \u6570\u7ec4\u4f9d\u7167 Base64 \u7f16\u7801\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\r\n        }\r\n\r\n        \/**\/\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ DES + Base64 \u89e3\u5bc6\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;input&quot;&gt;\u5bc6\u6587\u5b57\u7b26\u4e32&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;\u89e3\u5bc6\u5b57\u7b26\u4e32&lt;\/returns&gt;\r\n        public static string DesBase64Decrypt(string input)\r\n        {\r\n            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();\r\n            des.Mode = System.Security.Cryptography.CipherMode.ECB;\r\n            ICryptoTransform ct;\r\n            MemoryStream ms;\r\n            CryptoStream cs;\r\n            byte&#x5B;] byt;\r\n            byte&#x5B;] Key = new byte&#x5B;8] { 56, 50, 55, 56, 56, 55, 49, 49 };\r\n            byte&#x5B;] IV = new byte&#x5B;8] { 0, 0, 0, 0, 0, 0, 0, 0 };\r\n\r\n            ct = des.CreateDecryptor(Key, IV);\r\n            byt = Convert.FromBase64String(input); \/\/ \u5c06 \u5bc6\u6587 \u4ee5 Base64 \u7f16\u7801\u8f6c\u6362\u6210 byte \u6570\u7ec4\r\n\r\n            ms = new MemoryStream();\r\n            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);\r\n            cs.Write(byt, 0, byt.Length);\r\n            cs.FlushFinalBlock();\r\n\r\n            cs.Close();\r\n\r\n            return Encoding.GetEncoding(&quot;GB2312&quot;).GetString(ms.ToArray()); \/\/ \u5c06 \u660e\u6587 \u4ee5 GB2312 \u7f16\u7801\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\r\n        }\r\n\r\n \r\n\r\n        \/**\/\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ DES + Base64 \u52a0\u5bc6\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;input&quot;&gt;\u660e\u6587\u5b57\u7b26\u4e32&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;\u5df2\u52a0\u5bc6\u5b57\u7b26\u4e32&lt;\/returns&gt;\r\n        public static string DesBase64EncryptForID5(string input)\r\n        {\r\n            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();\r\n            des.Mode = System.Security.Cryptography.CipherMode.CBC;\r\n            ICryptoTransform ct;\r\n            MemoryStream ms;\r\n            CryptoStream cs;\r\n            byte&#x5B;] byt;\r\n            byte&#x5B;] Key = new byte&#x5B;8] { 56, 50, 55, 56, 56, 55, 49, 49 };\r\n            byte&#x5B;] IV = new byte&#x5B;8] { 56, 50, 55, 56, 56, 55, 49, 49 };\r\n\r\n            ct = des.CreateEncryptor(Key, IV);\r\n\r\n            byt = Encoding.GetEncoding(&quot;GB2312&quot;).GetBytes(input); \/\/\u6839\u636e GB2312 \u7f16\u7801\u5bf9\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u8f6c\u6362\u6210 byte \u6570\u7ec4\r\n\r\n            ms = new MemoryStream();\r\n            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);\r\n            cs.Write(byt, 0, byt.Length);\r\n            cs.FlushFinalBlock();\r\n\r\n            cs.Close();\r\n\r\n            byte&#x5B;] answer = ms.ToArray();\r\n            for (int j = 0; j &lt; answer.Length; j++)\r\n            {\r\n                Console.Write(answer&#x5B;j].ToString() + &quot; &quot;);\r\n            }\r\n            Console.WriteLine();\r\n            return Convert.ToBase64String(ms.ToArray()); \/\/ \u5c06\u52a0\u5bc6\u7684 byte \u6570\u7ec4\u4f9d\u7167 Base64 \u7f16\u7801\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\r\n        }\r\n\r\n        \/**\/\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ DES + Base64 \u89e3\u5bc6\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;input&quot;&gt;\u5bc6\u6587\u5b57\u7b26\u4e32&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;\u89e3\u5bc6\u5b57\u7b26\u4e32&lt;\/returns&gt;\r\n        public static string DesBase64DecryptForID5(string input)\r\n        {\r\n            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();\r\n            des.Mode = System.Security.Cryptography.CipherMode.CBC;\r\n            ICryptoTransform ct;\r\n            MemoryStream ms;\r\n            CryptoStream cs;\r\n            byte&#x5B;] byt;\r\n            byte&#x5B;] Key = new byte&#x5B;8] { 56, 50, 55, 56, 56, 55, 49, 49 };\r\n            byte&#x5B;] IV = new byte&#x5B;8] { 56, 50, 55, 56, 56, 55, 49, 49 };\r\n\r\n            ct = des.CreateDecryptor(Key, IV);\r\n            byt = Convert.FromBase64String(input); \/\/ \u5c06 \u5bc6\u6587 \u4ee5 Base64 \u7f16\u7801\u8f6c\u6362\u6210 byte \u6570\u7ec4\r\n\r\n            ms = new MemoryStream();\r\n            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);\r\n            cs.Write(byt, 0, byt.Length);\r\n            cs.FlushFinalBlock();\r\n\r\n            cs.Close();\r\n\r\n            return Encoding.GetEncoding(&quot;GB2312&quot;).GetString(ms.ToArray()); \/\/ \u5c06 \u660e\u6587 \u4ee5 GB2312 \u7f16\u7801\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\r\n        }\r\n\r\n        \/**\/\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ 3DES \u52a0\u5bc6 Byte&#x5B;] to HEX string\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;input&quot;&gt;\u660e\u6587\u5b57\u7b26\u4e32&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;\u5df2\u52a0\u5bc6\u5b57\u7b26\u4e32&lt;\/returns&gt;\r\n        public static string ThreeDesEncryptHEX(string input)\r\n        {\r\n            string result = &quot;&quot;;\r\n            System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create();\r\n            des.Mode = System.Security.Cryptography.CipherMode.CBC;\r\n            des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;\r\n            ICryptoTransform ct;\r\n            MemoryStream ms;\r\n            CryptoStream cs;\r\n            byte&#x5B;] byt;\r\n            byte&#x5B;] Key = new byte&#x5B;24]{\r\n                                       1,2,3,4,5,6,\r\n                                       1,2,3,4,5,6,\r\n                                       1,2,3,4,5,6,\r\n                                       1,2,3,4,5,6\r\n                                   };\r\n            byte&#x5B;] IV = new byte&#x5B;8] { 1, 2, 3, 4, 5, 6, 1, 2 };\r\n\r\n            ct = des.CreateEncryptor(Key, IV);\r\n\r\n            byt = Encoding.GetEncoding(&quot;GB2312&quot;).GetBytes(input); \/\/\u6839\u636e GB2312 \u7f16\u7801\u5bf9\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u8f6c\u6362\u6210 byte \u6570\u7ec4\r\n\r\n            ms = new MemoryStream();\r\n            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);\r\n            cs.Write(byt, 0, byt.Length);\r\n            cs.FlushFinalBlock();\r\n\r\n            cs.Close();\r\n\r\n            byte&#x5B;] answer = ms.ToArray();\r\n            for (int j = 0; j &lt; answer.Length; j++)\r\n            {\r\n                result += answer&#x5B;j].ToString(&quot;x&quot;).PadLeft(2, '0');\r\n            }\r\n            return result;\r\n        }\r\n\r\n        \/**\/\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ 3DES + HEX to byte&#x5B;] \u89e3\u5bc6\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;input&quot;&gt;\u5bc6\u6587\u5b57\u7b26\u4e32&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;\u89e3\u5bc6\u5b57\u7b26\u4e32&lt;\/returns&gt;\r\n        public static string ThreeDesDecryptHEX(string input)\r\n        {\r\n            System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create();\r\n            des.Mode = System.Security.Cryptography.CipherMode.CBC;\r\n            des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;\r\n            ICryptoTransform ct;\r\n            MemoryStream ms;\r\n            CryptoStream cs;\r\n            byte&#x5B;] Key = new byte&#x5B;24]{\r\n                                       1,2,3,4,5,6,\r\n                                       1,2,3,4,5,6,\r\n                                       1,2,3,4,5,6,\r\n                                       1,2,3,4,5,6\r\n                                   };\r\n            byte&#x5B;] IV = new byte&#x5B;8] { 1, 2, 3, 4, 5, 6, 1, 2 };\r\n\r\n            ct = des.CreateDecryptor(Key, IV);\r\n            \/\/byt = Convert.FromBase64String(input); \/\/ \u5c06 \u5bc6\u6587 \u4ee5 HEX to byte&#x5B;]\u7f16\u7801\u8f6c\u6362\u6210 byte \u6570\u7ec4\r\n            if (input.Length &lt;= 1)\r\n            {\r\n                throw new Exception(&quot;encrypted HEX string is too short!&quot;);\r\n            }\r\n            byte&#x5B;] byt = new byte&#x5B;input.Length \/ 2];\r\n            for (int i = 0; i &lt; byt.Length; i++)\r\n            {\r\n                \/\/Console.WriteLine(input.Substring(i*2,2));\r\n                byt&#x5B;i] = Convert.ToByte(input.Substring(i * 2, 2), 16);\r\n            }\r\n\r\n            ms = new MemoryStream();\r\n            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);\r\n            cs.Write(byt, 0, byt.Length);\r\n            cs.FlushFinalBlock();\r\n\r\n            cs.Close();\r\n\r\n            return Encoding.GetEncoding(&quot;GB2312&quot;).GetString(ms.ToArray()); \/\/ \u5c06 \u660e\u6587 \u4ee5 GB2312 \u7f16\u7801\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\r\n        }\r\n        \/**\/\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ Base64\u89e3\u7801\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;base64Str&quot;&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public static string DecodingFromBase64(string base64Str)\r\n        {\r\n            Byte&#x5B;] bytes = Convert.FromBase64String(base64Str);\r\n            return System.Text.Encoding.UTF8.GetString(bytes);\r\n        }\r\n        \/**\/\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ Base64\u7f16\u7801\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;str&quot;&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public static string EncodingToBase64(string str)\r\n        {\r\n            return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));\r\n        }\r\n        \/**\/\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u6839\u636e\u6307\u5b9a\u7684\u7f16\u7801\u65b9\u5f0fBase64\u89e3\u7801\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;base64Str&quot;&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=&quot;strEncoding&quot;&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public static string DecodingFromBase64(string base64Str, System.Text.Encoding strEncoding)\r\n        {\r\n            Byte&#x5B;] bytes = Convert.FromBase64String(base64Str);\r\n            return strEncoding.GetString(bytes);\r\n        }\r\n        \/**\/\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u6839\u636e\u6307\u5b9a\u7684\u7f16\u7801\u65b9\u5f0fBase64\u7f16\u7801\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;str&quot;&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=&quot;strEncoding&quot;&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public static string EncodingToBase64(string str, System.Text.Encoding strEncoding)\r\n        {\r\n            return Convert.ToBase64String(strEncoding.GetBytes(str));\r\n        }\r\n    }\r\n\r\n \r\n\r\n        \/\/\u4e24\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\r\n        \/**\/\/\/\/ &lt;summary&gt;\r\n       \/\/\/ \u901a\u8fc7\u5b57\u8282\u6570\u7ec4\u5f62\u5f0f\u7684\u5bc6\u94a5\u83b7\u53d6\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5bc6\u94a5\r\n       \/\/\/ &lt;\/summary&gt;\r\n        void GetStringByByteArray()\r\n        {\r\n            byte&#x5B;] Key = new byte&#x5B;8] { 56, 50, 55, 56, 56, 55, 49, 49 };\r\n            Response.Write(System.Text.Encoding.Default.GetString(Key));\r\n            Response.End();\r\n        }\r\n\r\n       \/\/\/**\/\/\/\/ &lt;summary&gt;\r\n       \/\/\/\/\/ \u901a\u8fc7\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5bc6\u94a5\u83b7\u53d6\u5b57\u8282\u6570\u7ec4\u5f62\u5f0f\u7684\u5bc6\u94a5\r\n       \/\/\/\/\/ &lt;\/summary&gt;\r\n        void GetByteArrayByString()\r\n        {\r\n            string key = &quot;82788711&quot;;\r\n            Response.Write(System.Text.Encoding.Default.GetBytes(key));\r\n            Response.End();\r\n\r\n        }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>public class CryptUtil { public static string DecryptSt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-736","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=736"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/736\/revisions"}],"predecessor-version":[{"id":4588,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/736\/revisions\/4588"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}