Cryptography.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. using System.IO;
  7. using System.Net;
  8. namespace Tencent
  9. {
  10. class Cryptography
  11. {
  12. public static UInt32 HostToNetworkOrder(UInt32 inval)
  13. {
  14. UInt32 outval = 0;
  15. for (int i = 0; i < 4; i++)
  16. outval = (outval << 8) + ((inval >> (i * 8)) & 255);
  17. return outval;
  18. }
  19. public static Int32 HostToNetworkOrder(Int32 inval)
  20. {
  21. Int32 outval = 0;
  22. for (int i = 0; i < 4; i++)
  23. outval = (outval << 8) + ((inval >> (i * 8)) & 255);
  24. return outval;
  25. }
  26. /// <summary>
  27. /// 解密方法
  28. /// </summary>
  29. /// <param name="Input">密文</param>
  30. /// <param name="EncodingAESKey"></param>
  31. /// <returns></returns>
  32. ///
  33. public static string AES_decrypt(String Input, string EncodingAESKey, ref string corpid)
  34. {
  35. byte[] Key;
  36. Key = Convert.FromBase64String(EncodingAESKey + "=");
  37. byte[] Iv = new byte[16];
  38. Array.Copy(Key, Iv, 16);
  39. byte[] btmpMsg = AES_decrypt(Input, Iv, Key);
  40. int len = BitConverter.ToInt32(btmpMsg, 16);
  41. len = IPAddress.NetworkToHostOrder(len);
  42. byte[] bMsg = new byte[len];
  43. byte[] bCorpid = new byte[btmpMsg.Length - 20 - len];
  44. Array.Copy(btmpMsg, 20, bMsg, 0, len);
  45. Array.Copy(btmpMsg, 20+len , bCorpid, 0, btmpMsg.Length - 20 - len);
  46. string oriMsg = Encoding.UTF8.GetString(bMsg);
  47. corpid = Encoding.UTF8.GetString(bCorpid);
  48. return oriMsg;
  49. }
  50. public static String AES_encrypt(String Input, string EncodingAESKey, string corpid)
  51. {
  52. byte[] Key;
  53. Key = Convert.FromBase64String(EncodingAESKey + "=");
  54. byte[] Iv = new byte[16];
  55. Array.Copy(Key, Iv, 16);
  56. string Randcode = CreateRandCode(16);
  57. byte[] bRand = Encoding.UTF8.GetBytes(Randcode);
  58. byte[] bCorpid = Encoding.UTF8.GetBytes(corpid);
  59. byte[] btmpMsg = Encoding.UTF8.GetBytes(Input);
  60. byte[] bMsgLen = BitConverter.GetBytes(HostToNetworkOrder(btmpMsg.Length));
  61. byte[] bMsg = new byte[bRand.Length + bMsgLen.Length + bCorpid.Length + btmpMsg.Length];
  62. Array.Copy(bRand, bMsg, bRand.Length);
  63. Array.Copy(bMsgLen, 0, bMsg, bRand.Length, bMsgLen.Length);
  64. Array.Copy(btmpMsg, 0, bMsg, bRand.Length + bMsgLen.Length, btmpMsg.Length);
  65. Array.Copy(bCorpid, 0, bMsg, bRand.Length + bMsgLen.Length + btmpMsg.Length, bCorpid.Length);
  66. return AES_encrypt(bMsg, Iv, Key);
  67. }
  68. private static string CreateRandCode(int codeLen)
  69. {
  70. string codeSerial = "2,3,4,5,6,7,a,c,d,e,f,h,i,j,k,m,n,p,r,s,t,A,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,U,V,W,X,Y,Z";
  71. if (codeLen == 0)
  72. {
  73. codeLen = 16;
  74. }
  75. string[] arr = codeSerial.Split(',');
  76. string code = "";
  77. int randValue = -1;
  78. Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
  79. for (int i = 0; i < codeLen; i++)
  80. {
  81. randValue = rand.Next(0, arr.Length - 1);
  82. code += arr[randValue];
  83. }
  84. return code;
  85. }
  86. private static String AES_encrypt(String Input, byte[] Iv, byte[] Key)
  87. {
  88. var aes = new RijndaelManaged();
  89. //秘钥的大小,以位为单位
  90. aes.KeySize = 256;
  91. //支持的块大小
  92. aes.BlockSize = 128;
  93. //填充模式
  94. aes.Padding = PaddingMode.PKCS7;
  95. aes.Mode = CipherMode.CBC;
  96. aes.Key = Key;
  97. aes.IV = Iv;
  98. var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
  99. byte[] xBuff = null;
  100. using (var ms = new MemoryStream())
  101. {
  102. using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
  103. {
  104. byte[] xXml = Encoding.UTF8.GetBytes(Input);
  105. cs.Write(xXml, 0, xXml.Length);
  106. }
  107. xBuff = ms.ToArray();
  108. }
  109. String Output = Convert.ToBase64String(xBuff);
  110. return Output;
  111. }
  112. private static String AES_encrypt(byte[] Input, byte[] Iv, byte[] Key)
  113. {
  114. var aes = new RijndaelManaged();
  115. //秘钥的大小,以位为单位
  116. aes.KeySize = 256;
  117. //支持的块大小
  118. aes.BlockSize = 128;
  119. //填充模式
  120. //aes.Padding = PaddingMode.PKCS7;
  121. aes.Padding = PaddingMode.None;
  122. aes.Mode = CipherMode.CBC;
  123. aes.Key = Key;
  124. aes.IV = Iv;
  125. var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
  126. byte[] xBuff = null;
  127. #region 自己进行PKCS7补位,用系统自己带的不行
  128. byte[] msg = new byte[Input.Length + 32 - Input.Length % 32];
  129. Array.Copy(Input, msg, Input.Length);
  130. byte[] pad = KCS7Encoder(Input.Length);
  131. Array.Copy(pad, 0, msg, Input.Length, pad.Length);
  132. #endregion
  133. #region 注释的也是一种方法,效果一样
  134. //ICryptoTransform transform = aes.CreateEncryptor();
  135. //byte[] xBuff = transform.TransformFinalBlock(msg, 0, msg.Length);
  136. #endregion
  137. using (var ms = new MemoryStream())
  138. {
  139. using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
  140. {
  141. cs.Write(msg, 0, msg.Length);
  142. }
  143. xBuff = ms.ToArray();
  144. }
  145. String Output = Convert.ToBase64String(xBuff);
  146. return Output;
  147. }
  148. private static byte[] KCS7Encoder(int text_length)
  149. {
  150. int block_size = 32;
  151. // 计算需要填充的位数
  152. int amount_to_pad = block_size - (text_length % block_size);
  153. if (amount_to_pad == 0)
  154. {
  155. amount_to_pad = block_size;
  156. }
  157. // 获得补位所用的字符
  158. char pad_chr = chr(amount_to_pad);
  159. string tmp = "";
  160. for (int index = 0; index < amount_to_pad; index++)
  161. {
  162. tmp += pad_chr;
  163. }
  164. return Encoding.UTF8.GetBytes(tmp);
  165. }
  166. /**
  167. * 将数字转化成ASCII码对应的字符,用于对明文进行补码
  168. *
  169. * @param a 需要转化的数字
  170. * @return 转化得到的字符
  171. */
  172. static char chr(int a)
  173. {
  174. byte target = (byte)(a & 0xFF);
  175. return (char)target;
  176. }
  177. private static byte[] AES_decrypt(String Input, byte[] Iv, byte[] Key)
  178. {
  179. RijndaelManaged aes = new RijndaelManaged();
  180. aes.KeySize = 256;
  181. aes.BlockSize = 128;
  182. aes.Mode = CipherMode.CBC;
  183. aes.Padding = PaddingMode.None;
  184. aes.Key = Key;
  185. aes.IV = Iv;
  186. var decrypt = aes.CreateDecryptor(aes.Key, aes.IV);
  187. byte[] xBuff = null;
  188. using (var ms = new MemoryStream())
  189. {
  190. using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
  191. {
  192. byte[] xXml = Convert.FromBase64String(Input);
  193. byte[] msg = new byte[xXml.Length + 32 - xXml.Length % 32];
  194. Array.Copy(xXml, msg, xXml.Length);
  195. cs.Write(xXml, 0, xXml.Length);
  196. }
  197. xBuff = decode2(ms.ToArray());
  198. }
  199. return xBuff;
  200. }
  201. private static byte[] decode2(byte[] decrypted)
  202. {
  203. int pad = (int)decrypted[decrypted.Length - 1];
  204. if (pad < 1 || pad > 32)
  205. {
  206. pad = 0;
  207. }
  208. byte[] res = new byte[decrypted.Length - pad];
  209. Array.Copy(decrypted, 0, res, 0, decrypted.Length - pad);
  210. return res;
  211. }
  212. }
  213. }