Encryption.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Web;
  8. namespace UAS_MES.PublicMethod
  9. {
  10. class Encryption
  11. {
  12. private static string encryptKey = "MUPSWORD";
  13. public static string EncryptStr(string pToEncrypt)
  14. {
  15. using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
  16. {
  17. byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
  18. des.Key = ASCIIEncoding.ASCII.GetBytes(encryptKey);
  19. des.IV = ASCIIEncoding.ASCII.GetBytes(encryptKey);
  20. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  21. using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
  22. {
  23. cs.Write(inputByteArray, 0, inputByteArray.Length);
  24. cs.FlushFinalBlock();
  25. cs.Close();
  26. }
  27. string str = Convert.ToBase64String(ms.ToArray());
  28. ms.Close();
  29. return str;
  30. }
  31. }
  32. public static string GETMD5(string password)
  33. {
  34. //初始化MD5对象
  35. MD5 md5 = MD5.Create();
  36. //将源字符串转化为byte数组
  37. Byte[] soucebyte = Encoding.Default.GetBytes(password);
  38. //soucebyte转化为mf5的byte数组
  39. Byte[] md5bytes = md5.ComputeHash(soucebyte);
  40. //将md5的byte数组再转化为MD5数组
  41. StringBuilder sb = new StringBuilder();
  42. foreach (Byte b in md5bytes)
  43. {
  44. //x表示16进制,2表示2位
  45. sb.Append(b.ToString("x2"));
  46. }
  47. return sb.ToString();
  48. }
  49. public static string DecryptStr(string pToDecrypt)
  50. {
  51. byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
  52. using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
  53. {
  54. des.Key = ASCIIEncoding.ASCII.GetBytes(encryptKey);
  55. des.IV = ASCIIEncoding.ASCII.GetBytes(encryptKey);
  56. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  57. using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
  58. {
  59. cs.Write(inputByteArray, 0, inputByteArray.Length);
  60. cs.FlushFinalBlock();
  61. cs.Close();
  62. }
  63. string str = Encoding.UTF8.GetString(ms.ToArray());
  64. ms.Close();
  65. return str;
  66. }
  67. }
  68. }
  69. }