AESCrypt.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace TEAMModelOS.SDK.Helper.Security.AESCrypt
  5. {
  6. /// <summary>
  7. /// Describe:AES 加密算法
  8. /// </summary>
  9. public class AESCrypt
  10. {
  11. public const string RET_ERROR = "x07x07x07x07x07";
  12. private byte[] _IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  13. private byte[] _Key = { 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
  14. private const string CRYPTO_KEY = "ENCRYPTIONALGORITHMBYYUANGANG";
  15. private int CRYPTO_KEY_LENGTH = 32;
  16. private Aes m_aesCryptoServiceProvider;
  17. private string m_message;
  18. public string Message
  19. {
  20. get { return m_message; }
  21. set { m_message = value; }
  22. }
  23. private bool m_containKey;
  24. /// <summary>
  25. /// True:密文中包含密钥
  26. /// False:密文中不包含密钥
  27. /// </summary>
  28. public bool ContainKey
  29. {
  30. get { return m_containKey; }
  31. set { m_containKey = value; }
  32. }
  33. public AESCrypt()
  34. {
  35. m_aesCryptoServiceProvider = Aes.Create();
  36. m_containKey = true;
  37. m_message = string.Empty;
  38. }
  39. public AESCrypt(bool containKey)
  40. : this()
  41. {
  42. m_containKey = containKey;
  43. }
  44. private string Encrypt(string s_crypto, byte[] key)
  45. {
  46. string s_encryped = string.Empty;
  47. byte[] crypto, encrypted;
  48. ICryptoTransform ct;
  49. try
  50. {
  51. crypto = string2Byte(s_crypto);
  52. m_aesCryptoServiceProvider.Key = key;
  53. m_aesCryptoServiceProvider.IV = _IV;
  54. ct = m_aesCryptoServiceProvider.CreateEncryptor();
  55. encrypted = ct.TransformFinalBlock(crypto, 0, crypto.Length);
  56. if (m_containKey)
  57. {
  58. s_encryped += byte2HexString(key);
  59. }
  60. s_encryped += byte2HexString(encrypted);
  61. return s_encryped;
  62. }
  63. catch (Exception ex)
  64. {
  65. m_message = ex.ToString();
  66. return RET_ERROR;
  67. }
  68. }
  69. private string Decrypt(string s_encrypted, byte[] key)
  70. {
  71. string s_decrypted = string.Empty;
  72. byte[] encrypted, decrypted;
  73. ICryptoTransform ct;
  74. try
  75. {
  76. encrypted = hexString2Byte(s_encrypted);
  77. m_aesCryptoServiceProvider.Key = key;
  78. m_aesCryptoServiceProvider.IV = _IV;
  79. ct = m_aesCryptoServiceProvider.CreateDecryptor();
  80. decrypted = ct.TransformFinalBlock(encrypted, 0, encrypted.Length);
  81. s_decrypted += byte2String(decrypted);
  82. return s_decrypted;
  83. }
  84. catch (Exception ex)
  85. {
  86. m_message = ex.ToString();
  87. m_message = "Decrypt fail.";
  88. return RET_ERROR;
  89. }
  90. }
  91. #region 加密
  92. /// <summary>
  93. /// 指定密钥对明文进行AES加密
  94. /// </summary>
  95. /// <param name="s_crypto">明文</param>
  96. /// <param name="s_key">加密密钥</param>
  97. /// <returns></returns>
  98. public string Encrypt(string s_crypto, string s_key)
  99. {
  100. byte[] key = new byte[CRYPTO_KEY_LENGTH];
  101. byte[] temp = string2Byte(s_key);
  102. if (temp.Length > key.Length)
  103. {
  104. m_message = "Key too long,need less than 32 Bytes key.";
  105. return RET_ERROR;
  106. }
  107. key = string2Byte(s_key.PadRight(key.Length));
  108. return Encrypt(s_crypto, key);
  109. }
  110. /// <summary>
  111. /// 动态生成密钥,并对明文进行AES加密
  112. /// </summary>
  113. /// <param name="s_crypto">明文</param>
  114. /// <returns></returns>
  115. public string Encrypt(string s_crypto)
  116. {
  117. byte[] key = new byte[CRYPTO_KEY_LENGTH];
  118. m_aesCryptoServiceProvider.GenerateKey();
  119. key = m_aesCryptoServiceProvider.Key;
  120. return Encrypt(s_crypto, key);
  121. }
  122. #endregion
  123. #region 解密
  124. /// <summary>
  125. /// 从密文中解析出密钥,并对密文进行解密
  126. /// </summary>
  127. /// <param name="s_encrypted">密文</param>
  128. /// <returns></returns>
  129. public string Decrypt(string s_encrypted)
  130. {
  131. string s_key = string.Empty;
  132. byte[] key = new byte[CRYPTO_KEY_LENGTH];
  133. if (s_encrypted.Length <= CRYPTO_KEY_LENGTH * 2)
  134. {
  135. m_message = "Encrypted string invalid.";
  136. return RET_ERROR;
  137. }
  138. if (m_containKey)
  139. {
  140. s_key = s_encrypted.Substring(0, CRYPTO_KEY_LENGTH * 2);
  141. s_encrypted = s_encrypted.Substring(CRYPTO_KEY_LENGTH * 2);
  142. }
  143. key = hexString2Byte(s_key);
  144. return Decrypt(s_encrypted, key);
  145. }
  146. /// <summary>
  147. /// 指定密钥,并对密文进行解密
  148. /// </summary>
  149. /// <param name="s_encrypted">密文</param>
  150. /// <param name="s_key">密钥</param>
  151. /// <returns></returns>
  152. public string Decrypt(string s_encrypted, string s_key)
  153. {
  154. byte[] key = new byte[CRYPTO_KEY_LENGTH];
  155. byte[] temp = string2Byte(s_key);
  156. if (temp.Length > key.Length)
  157. {
  158. m_message = "Key invalid.too long,need less than 32 Bytes";
  159. return RET_ERROR;
  160. }
  161. key = string2Byte(s_key.PadRight(key.Length));
  162. if (m_containKey)
  163. {
  164. s_encrypted = s_encrypted.Substring(CRYPTO_KEY_LENGTH * 2);
  165. }
  166. return Decrypt(s_encrypted, key);
  167. }
  168. #endregion
  169. #region 私有方法
  170. private string byte2HexString(byte[] bytes)
  171. {
  172. StringBuilder sb = new StringBuilder();
  173. foreach (byte b in bytes)
  174. {
  175. sb.AppendFormat("{0:X2}", b);
  176. }
  177. return sb.ToString();
  178. }
  179. private byte[] hexString2Byte(string hex)
  180. {
  181. int len = hex.Length / 2;
  182. byte[] bytes = new byte[len];
  183. for (int i = 0; i < len; i++)
  184. {
  185. bytes[i] = (byte)(Convert.ToInt32(hex.Substring(i * 2, 2), 16));
  186. }
  187. return bytes;
  188. }
  189. private byte[] string2Byte(string str)
  190. {
  191. return Encoding.UTF8.GetBytes(str);
  192. }
  193. private string byte2String(byte[] bytes)
  194. {
  195. return Encoding.UTF8.GetString(bytes);
  196. }
  197. #endregion
  198. }
  199. }