颐和api

DESEncrypt.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace FileServerApi.Common
  10. {
  11. public class DESEncrypt
  12. {
  13. #region 加密解密法
  14. //默认密钥向量
  15. private static byte[] Keys = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
  16. /// <summary>
  17. /// DES加密字符串
  18. /// </summary>
  19. /// <param name="encryptString">待加密的字符串</param>
  20. /// <param name="encryptKey">加密密钥,要求为16位</param>
  21. /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
  22. public static string EncryptDES(string encryptString, string encryptKey = "upload#20180319#down")
  23. {
  24. try
  25. {
  26. byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 16));
  27. byte[] rgbIV = Keys;
  28. byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  29. var DCSP = Aes.Create();
  30. MemoryStream mStream = new MemoryStream();
  31. CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  32. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  33. cStream.FlushFinalBlock();
  34. //return Convert.ToBase64String(mStream.ToArray());
  35. return BytesToHex(mStream.ToArray());
  36. }
  37. catch (Exception ex)
  38. {
  39. return ex.Message + encryptString;
  40. }
  41. }
  42. /// <summary>
  43. /// DES解密字符串
  44. /// </summary>
  45. /// <param name="decryptString">待解密的字符串</param>
  46. /// <param name="decryptKey">解密密钥,要求为16位,和加密密钥相同</param>
  47. /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
  48. public static string DecryptDES(string decryptString, string decryptKey = "upload#20180319#down")
  49. {
  50. try
  51. {
  52. byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 16));
  53. byte[] rgbIV = Keys;
  54. //byte[] inputByteArray = Convert.FromBase64String(decryptString);
  55. byte[] inputByteArray = HexToBytes(decryptString);
  56. var DCSP = Aes.Create();
  57. MemoryStream mStream = new MemoryStream();
  58. CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  59. Byte[] inputByteArrays = new byte[inputByteArray.Length];
  60. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  61. cStream.FlushFinalBlock();
  62. return Encoding.UTF8.GetString(mStream.ToArray());
  63. }
  64. catch (Exception ex)
  65. {
  66. return ex.Message + decryptString;
  67. }
  68. }
  69. public static byte[] HexToBytes(string Hex)
  70. {
  71. int num = (int)Math.Round((double)(((double)Hex.Length) / 2));
  72. byte[] buffer = new byte[(num - 1) + 1];
  73. int num3 = num - 1;
  74. for (int i = 0; i <= num3; i++)
  75. {
  76. string s = Hex.Substring(i * 2, 2);
  77. buffer[i] = (byte)int.Parse(s, NumberStyles.HexNumber);
  78. }
  79. return buffer;
  80. }
  81. public static string BytesToHex(byte[] bytes)
  82. {
  83. StringBuilder builder = new StringBuilder();
  84. int num2 = bytes.Length - 1;
  85. for (int i = 0; i <= num2; i++)
  86. {
  87. builder.AppendFormat("{0:X2}", bytes[i]);
  88. }
  89. return builder.ToString();
  90. }
  91. #endregion
  92. }
  93. }