_Encrypt.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Security.Cryptography;
  7. namespace IControls_FireManager
  8. {
  9. //
  10. // 파일 암호화
  11. //
  12. public static partial class _Encrypt
  13. {
  14. public static string AESKey = "12345678"; // 8byte
  15. public static bool EncryptFile(string ReadFilename, string WriteFilename)
  16. {
  17. try
  18. {
  19. FileStream fsInput = new FileStream(ReadFilename, FileMode.Open, FileAccess.Read, FileShare.None);
  20. FileStream fsEncrypted = new FileStream(WriteFilename, FileMode.Create, FileAccess.Write);
  21. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  22. DES.Key = ASCIIEncoding.ASCII.GetBytes(AESKey);
  23. DES.IV = ASCIIEncoding.ASCII.GetBytes(AESKey);
  24. ICryptoTransform Encrypt = DES.CreateEncryptor();
  25. CryptoStream cryptostream = new CryptoStream(fsEncrypted, Encrypt, CryptoStreamMode.Write);
  26. byte[] bytearrayinput = new byte[fsInput.Length];
  27. fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
  28. cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
  29. fsEncrypted.Flush();
  30. cryptostream.Close();
  31. fsInput.Close();
  32. fsEncrypted.Close();
  33. return true;
  34. }
  35. catch { return false; }
  36. }
  37. public static bool DecryptFile(string ReadFilename, string WriteFilename)
  38. {
  39. try
  40. {
  41. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  42. DES.Key = ASCIIEncoding.ASCII.GetBytes(AESKey);
  43. DES.IV = ASCIIEncoding.ASCII.GetBytes(AESKey);
  44. FileStream fsread = new FileStream(ReadFilename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  45. ICryptoTransform Decrypt = DES.CreateDecryptor();
  46. CryptoStream cryptostreamDecr = new CryptoStream(fsread, Decrypt, CryptoStreamMode.Read);
  47. StreamWriter fsDecrypted = new StreamWriter(WriteFilename);
  48. fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
  49. fsDecrypted.Flush();
  50. fsDecrypted.Close();
  51. return true;
  52. }
  53. catch { return false; }
  54. }
  55. }
  56. //
  57. // 패킷 암호화
  58. //
  59. public static partial class _Encrypt
  60. {
  61. private static byte[] AESKeys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
  62. public static string AES128Encode(string encryptString, string encryptKey)
  63. {
  64. encryptKey = encryptKey.PadRight(32, ' ');
  65. RijndaelManaged rijndaelProvider = new RijndaelManaged();
  66. rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
  67. rijndaelProvider.IV = AESKeys;
  68. ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();
  69. byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
  70. byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
  71. return Convert.ToBase64String(encryptedData);
  72. }
  73. public static string AES128Decode(string decryptString, string decryptKey)
  74. {
  75. try
  76. {
  77. decryptKey = decryptKey.PadRight(32, ' ');
  78. RijndaelManaged rijndaelProvider = new RijndaelManaged();
  79. rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey);
  80. rijndaelProvider.IV = AESKeys;
  81. ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();
  82. byte[] inputData = Convert.FromBase64String(decryptString);
  83. byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
  84. return Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length);
  85. }
  86. catch { return string.Empty; }
  87. }
  88. }
  89. }