Encryption.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*******************************************************************************
  2. * Copyright(c) 2012 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:Encryption.cs
  5. * 2.功能描述:系统各种字符串加密算法类
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 欧阳涛 2013/07/14 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Security.Cryptography;
  12. namespace Dongke.IBOSS.PRD.Basics.Library
  13. {
  14. /// <summary>
  15. /// 系统各种字符串加密算法类
  16. /// </summary>
  17. public class Encryption
  18. {
  19. #region RSA公钥加密算法
  20. /// <summary>
  21. /// RSA算法产生公共密钥和私有密钥
  22. /// </summary>
  23. /// <param name="publicKey">公共密钥</param>
  24. /// <param name="privateKey">私有密钥</param>
  25. public static void CreateRSAKeys(out string publicKey, out string privateKey)
  26. {
  27. System.Security.Cryptography.RSACryptoServiceProvider rsa =
  28. CreateRSACryptoServiceProvider();
  29. publicKey = rsa.ToXmlString(false);
  30. privateKey = rsa.ToXmlString(true);
  31. }
  32. public static string EncryptRSA(string value, string publicKey)
  33. {
  34. System.Security.Cryptography.RSACryptoServiceProvider rsa =
  35. CreateRSACryptoServiceProvider();
  36. rsa.FromXmlString(publicKey);
  37. byte[] data = System.Text.Encoding.UTF8.GetBytes(value);
  38. byte[] encryptedData = rsa.Encrypt(data, false);
  39. return System.Convert.ToBase64String(encryptedData);
  40. }
  41. public static string DecryptRSA(string value, string privateKey)
  42. {
  43. System.Security.Cryptography.RSACryptoServiceProvider rsa =
  44. CreateRSACryptoServiceProvider();
  45. rsa.FromXmlString(privateKey);
  46. byte[] data = System.Convert.FromBase64String(value);
  47. byte[] decryptedData = rsa.Decrypt(data, false);
  48. return System.Text.Encoding.UTF8.GetString(decryptedData);
  49. }
  50. protected static RSACryptoServiceProvider CreateRSACryptoServiceProvider()
  51. {
  52. System.Security.Cryptography.CspParameters cspParameters =
  53. new System.Security.Cryptography.CspParameters();
  54. cspParameters.Flags = System.Security.Cryptography.CspProviderFlags.UseMachineKeyStore;
  55. return new System.Security.Cryptography.RSACryptoServiceProvider(cspParameters);
  56. }
  57. #endregion
  58. #region DES加密算法
  59. public static string EncryptDES(string value, string key)
  60. {
  61. System.Security.Cryptography.DESCryptoServiceProvider des =
  62. CreateDESCryptoServiceProvider(key);
  63. byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(value);
  64. System.IO.MemoryStream stream = new System.IO.MemoryStream();
  65. System.Security.Cryptography.ICryptoTransform desdecrypt =
  66. des.CreateEncryptor();
  67. System.Security.Cryptography.CryptoStream cryptStream =
  68. new System.Security.Cryptography.CryptoStream(stream,
  69. desdecrypt,
  70. CryptoStreamMode.Write);
  71. cryptStream.Write(bytesIn, 0, bytesIn.Length);
  72. cryptStream.FlushFinalBlock();
  73. byte[] bytesOut = stream.ToArray();
  74. cryptStream.Close();
  75. stream.Close();
  76. return System.Convert.ToBase64String(bytesOut);
  77. }
  78. public static string DecryptDES(string value, string key)
  79. {
  80. System.Security.Cryptography.DESCryptoServiceProvider des =
  81. CreateDESCryptoServiceProvider(key);
  82. byte[] bytesIn = System.Convert.FromBase64String(value);
  83. System.IO.MemoryStream stream = new System.IO.MemoryStream(bytesIn);
  84. System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateDecryptor();
  85. System.Security.Cryptography.CryptoStream cryptStream =
  86. new System.Security.Cryptography.CryptoStream(stream,
  87. desdecrypt,
  88. CryptoStreamMode.Read);
  89. System.IO.StreamReader streamReader =
  90. new System.IO.StreamReader(cryptStream, System.Text.Encoding.UTF8);
  91. string result = streamReader.ReadToEnd();
  92. streamReader.Close();
  93. cryptStream.Close();
  94. stream.Close();
  95. return result;
  96. }
  97. protected static DESCryptoServiceProvider CreateDESCryptoServiceProvider(string key)
  98. {
  99. System.Security.Cryptography.DESCryptoServiceProvider des =
  100. new System.Security.Cryptography.DESCryptoServiceProvider();
  101. byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key);
  102. des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
  103. des.IV = ResizeBytesArray(bytesKey, des.IV.Length);
  104. return des;
  105. }
  106. protected static byte[] ResizeBytesArray(byte[] bytes, int newSize)
  107. {
  108. byte[] newBytes = new byte[newSize];
  109. if (bytes.Length <= newSize)
  110. {
  111. for (int i = 0; i < bytes.Length; i++)
  112. {
  113. newBytes[i] = bytes[i];
  114. }
  115. }
  116. else
  117. {
  118. int pos = 0;
  119. for (int i = 0; i < bytes.Length; i++)
  120. {
  121. newBytes[pos++] ^= bytes[i];
  122. if (pos >= newBytes.Length)
  123. {
  124. pos = 0;
  125. }
  126. }
  127. }
  128. return newBytes;
  129. }
  130. #endregion
  131. #region MD5加密算法
  132. public static string GetMD5String(string value)
  133. {
  134. return GetMD5String(System.Text.Encoding.UTF8.GetBytes(value));
  135. }
  136. public static string GetMD5String(byte[] value)
  137. {
  138. if (value == null)
  139. {
  140. return null;
  141. }
  142. System.Security.Cryptography.MD5CryptoServiceProvider md5 =
  143. new System.Security.Cryptography.MD5CryptoServiceProvider();
  144. byte[] bytes = md5.ComputeHash(value);
  145. return BitConverter.ToString(bytes).Replace("-", string.Empty);
  146. }
  147. #endregion
  148. #region SHA1加密算法
  149. public static string GetSHA1String(string value)
  150. {
  151. return GetSHA1String(System.Text.Encoding.UTF8.GetBytes(value));
  152. }
  153. public static string GetSHA1String(byte[] value)
  154. {
  155. if (value == null)
  156. {
  157. return null;
  158. }
  159. System.Security.Cryptography.SHA1CryptoServiceProvider sha1 =
  160. new System.Security.Cryptography.SHA1CryptoServiceProvider();
  161. byte[] bytes = sha1.ComputeHash(value);
  162. return BitConverter.ToString(bytes).Replace("-", string.Empty);
  163. }
  164. #endregion
  165. #region SHA256加密算法
  166. public static string GetSHA256String(string value)
  167. {
  168. return GetSHA256String(System.Text.Encoding.UTF8.GetBytes(value));
  169. }
  170. public static string GetSHA256String(byte[] value)
  171. {
  172. if (value == null)
  173. {
  174. return null;
  175. }
  176. System.Security.Cryptography.SHA256 sha256 =
  177. new System.Security.Cryptography.SHA256Managed();
  178. byte[] bytes = sha256.ComputeHash(value);
  179. return BitConverter.ToString(bytes).Replace("-", string.Empty);
  180. }
  181. #endregion
  182. }
  183. }