/HR-Test/Safe.cs

https://github.com/vampire1202/HSTest · C# · 108 lines · 67 code · 7 blank · 34 comment · 1 complexity · f7618dfb7fe803d49462e38da197e268 MD5 · raw file

  1. using System;
  2. using System.IO;
  3. using System.Security;
  4. using System.Security.Cryptography;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace HR_Test
  8. {
  9. class Safe
  10. {
  11. // Call this function to remove the key from memory after use for security
  12. [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]
  13. public static extern bool ZeroMemory(IntPtr Destination, int Length);
  14. // Function to Generate a 64 bits Key.
  15. /// <summary>
  16. /// 创建加密
  17. /// </summary>
  18. /// <returns></returns>
  19. public static string GenerateKey()
  20. {
  21. // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
  22. DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
  23. //string curveName = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\key.txt";
  24. //StreamWriter sw = new StreamWriter(curveName);
  25. if(string.IsNullOrEmpty( RWconfig.GetAppSettings("code")))
  26. {
  27. string ss = BitConverter.ToString(desCrypto.Key);
  28. RWconfig.SetAppSettings("code", ss.ToString());
  29. }
  30. //sw.WriteLine(BitConverter.ToString(desCrypto.Key));
  31. //sw.Close();
  32. //sw.Dispose();
  33. //RWconfig.SetAppSettings("code",BitConverter.ToString(desCrypto.Key));
  34. // Use the Automatically generated key for Encryption.
  35. return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
  36. }
  37. /// <summary>
  38. /// 加密文件
  39. /// </summary>
  40. /// <param name="sInputFilename"></param>
  41. /// <param name="sOutputFilename"></param>
  42. /// <param name="sKey"></param>
  43. public static void EncryptFile(string sInputFilename,
  44. string sOutputFilename,
  45. string sKey)
  46. {
  47. FileStream fsInput = new FileStream(sInputFilename,
  48. FileMode.Open,
  49. FileAccess.Read);
  50. FileStream fsEncrypted = new FileStream(sOutputFilename,
  51. FileMode.Create,
  52. FileAccess.Write);
  53. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  54. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  55. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  56. ICryptoTransform desencrypt = DES.CreateEncryptor();
  57. CryptoStream cryptostream = new CryptoStream(fsEncrypted,
  58. desencrypt,
  59. CryptoStreamMode.Write);
  60. byte[] bytearrayinput = new byte[fsInput.Length];
  61. fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
  62. cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
  63. cryptostream.Close();
  64. fsInput.Close();
  65. fsEncrypted.Close();
  66. }
  67. /// <summary>
  68. /// 解密文件
  69. /// </summary>
  70. /// <param name="sInputFilename"></param>
  71. /// <param name="sOutputFilename"></param>
  72. /// <param name="sKey"></param>
  73. public static void DecryptFile(string sInputFilename,
  74. string sOutputFilename,
  75. string sKey)
  76. {
  77. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  78. //A 64 bit key and IV is required for this provider.
  79. //Set secret key For DES algorithm.
  80. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  81. //Set initialization vector.
  82. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  83. //Create a file stream to read the encrypted file back.
  84. FileStream fsread = new FileStream(sInputFilename,
  85. FileMode.Open,
  86. FileAccess.Read);
  87. //Create a DES decryptor from the DES instance.
  88. ICryptoTransform desdecrypt = DES.CreateDecryptor();
  89. //Create crypto stream set to read and do a
  90. //DES decryption transform on incoming bytes.
  91. CryptoStream cryptostreamDecr = new CryptoStream(fsread,
  92. desdecrypt,
  93. CryptoStreamMode.Read);
  94. //Print the contents of the decrypted file.
  95. StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
  96. fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
  97. fsDecrypted.Flush();
  98. fsDecrypted.Close();
  99. }
  100. }
  101. }