C#.NET 加密解密:AES/DES/Base64/RSA/MD5/SHA256

    1. using System;
    2. using System.Globalization;
    3. using System.IO;
    4. using System.Security.Cryptography;
    5. using System.Text;
    6. namespace Pub.Class
    7. {
    8. public static class EncryptExtensions
    9. {
    10. private static readonly byte[] AESKeys = {0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F};
    11. private static readonly byte[] DESKeys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
    12. public static string AESEncode(this string encryptString, string encryptKey)
    13. {
    14. encryptKey = encryptKey.SubString(32, "");
    15. encryptKey = encryptKey.PadRight(32, ' ');
    16. var rijndaelProvider = new RijndaelManaged();
    17. rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
    18. rijndaelProvider.IV = AESKeys;
    19. ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();
    20. byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
    21. byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
    22. return Convert.ToBase64String(encryptedData);
    23. }
    24. public static string AESDecode(this string decryptString, string decryptKey)
    25. {
    26. try
    27. {
    28. decryptKey = decryptKey.SubString(32, "");
    29. decryptKey = decryptKey.PadRight(32, ' ');
    30. var rijndaelProvider = new RijndaelManaged();
    31. rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey);
    32. rijndaelProvider.IV = AESKeys;
    33. ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();
    34. byte[] inputData = Convert.FromBase64String(decryptString);
    35. byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
    36. return Encoding.UTF8.GetString(decryptedData);
    37. }
    38. catch
    39. {
    40. return string.Empty;
    41. }
    42. }
    43. public static string DESEncode(this string encryptString, string encryptKey)
    44. {
    45. encryptKey = encryptKey.SubString(8, "");
    46. encryptKey = encryptKey.PadRight(8, ' ');
    47. byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
    48. byte[] rgbIV = DESKeys;
    49. byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
    50. var dCSP = new DESCryptoServiceProvider();
    51. var mStream = new MemoryStream();
    52. var cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
    53. cStream.Write(inputByteArray, 0, inputByteArray.Length);
    54. cStream.FlushFinalBlock();
    55. return Convert.ToBase64String(mStream.ToArray());
    56. }
    57. public static string DESDecode(this string decryptString, string decryptKey)
    58. {
    59. try
    60. {
    61. decryptKey = decryptKey.SubString(8, "");
    62. decryptKey = decryptKey.PadRight(8, ' ');
    63. byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
    64. byte[] rgbIV = DESKeys;
    65. byte[] inputByteArray = Convert.FromBase64String(decryptString);
    66. var DCSP = new DESCryptoServiceProvider();
    67. var mStream = new MemoryStream();
    68. var cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
    69. cStream.Write(inputByteArray, 0, inputByteArray.Length);
    70. cStream.FlushFinalBlock();
    71. return Encoding.UTF8.GetString(mStream.ToArray());
    72. }
    73. catch
    74. {
    75. return string.Empty;
    76. }
    77. }
    78. public static string Base64Encode(this string encryptString)
    79. {
    80. byte[] encbuff = Encoding.UTF8.GetBytes(encryptString);
    81. return Convert.ToBase64String(encbuff);
    82. }
    83. public static string Base64Decode(this string decryptString)
    84. {
    85. byte[] decbuff = Convert.FromBase64String(decryptString);
    86. return Encoding.UTF8.GetString(decbuff);
    87. }
    88. public static string RSADecrypt(this string s, string key)
    89. {
    90. string result = null;
    91. if (string.IsNullOrEmpty(s)) throw new ArgumentException("An empty string value cannot be encrypted.");
    92. if (string.IsNullOrEmpty(key)) throw new ArgumentException("Cannot decrypt using an empty key. Please supply a decryption key.");
    93. var cspp = new CspParameters();
    94. cspp.KeyContainerName = key;
    95. var rsa = new RSACryptoServiceProvider(cspp);
    96. rsa.PersistKeyInCsp = true;
    97. string[] decryptArray = s.Split(new[] {"-"}, StringSplitOptions.None);
    98. byte[] decryptByteArray = Array.ConvertAll(decryptArray, (a => Convert.ToByte(byte.Parse(a, NumberStyles.HexNumber))));
    99. byte[] bytes = rsa.Decrypt(decryptByteArray, true);
    100. result = Encoding.UTF8.GetString(bytes);
    101. return result;
    102. }
    103. public static string RSAEncrypt(this string s, string key)
    104. {
    105. if (string.IsNullOrEmpty(s)) throw new ArgumentException("An empty string value cannot be encrypted.");
    106. if (string.IsNullOrEmpty(key)) throw new ArgumentException("Cannot encrypt using an empty key. Please supply an encryption key.");
    107. var cspp = new CspParameters();
    108. cspp.KeyContainerName = key;
    109. var rsa = new RSACryptoServiceProvider(cspp);
    110. rsa.PersistKeyInCsp = true;
    111. byte[] bytes = rsa.Encrypt(Encoding.UTF8.GetBytes(s), true);
    112. return BitConverter.ToString(bytes);
    113. }
    114. public static string MD5(this string str)
    115. {
    116. string cl1 = str;
    117. string pwd = "";
    118. MD5 md5 = System.Security.Cryptography.MD5.Create(); // 加密后是一个字节类型的数组
    119. byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(cl1));
    120. for (int i = 0; i < s.Length; i++)
    121. {
    122. // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
    123. pwd = pwd + s[i].ToString("x"); // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
    124. }
    125. return pwd;
    126. }
    127. public static string MD5CSP(this string encypStr)
    128. {
    129. string retStr;
    130. var m5 = new MD5CryptoServiceProvider();
    131. //创建md5对象
    132. byte[] inputBye;
    133. byte[] outputBye;
    134. //使用GB2312编码方式把字符串转化为字节数组.
    135. inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
    136. outputBye = m5.ComputeHash(inputBye);
    137. retStr = BitConverter.ToString(outputBye);
    138. retStr = retStr.Replace("-", "").ToLower();
    139. return retStr;
    140. }
    141. public static string SHA256(this string str)
    142. {
    143. byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
    144. var Sha256 = new SHA256Managed();
    145. byte[] Result = Sha256.ComputeHash(SHA256Data);
    146. return Convert.ToBase64String(Result); //返回长度为44字节的字符串
    147. }
    148. }
    149. }