12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- using System.IO;
- using System.Security.Cryptography;
- namespace IControls_FireManager
- {
- //
- // 파일 암호화
- //
- public static partial class _Encrypt
- {
- public static string AESKey = "12345678"; // 8byte
- public static bool EncryptFile(string ReadFilename, string WriteFilename)
- {
- try
- {
- FileStream fsInput = new FileStream(ReadFilename, FileMode.Open, FileAccess.Read, FileShare.None);
- FileStream fsEncrypted = new FileStream(WriteFilename, FileMode.Create, FileAccess.Write);
- DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
- DES.Key = ASCIIEncoding.ASCII.GetBytes(AESKey);
- DES.IV = ASCIIEncoding.ASCII.GetBytes(AESKey);
- ICryptoTransform Encrypt = DES.CreateEncryptor();
- CryptoStream cryptostream = new CryptoStream(fsEncrypted, Encrypt, CryptoStreamMode.Write);
- byte[] bytearrayinput = new byte[fsInput.Length];
- fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
- cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
- fsEncrypted.Flush();
- cryptostream.Close();
- fsInput.Close();
- fsEncrypted.Close();
- return true;
- }
- catch { return false; }
- }
- public static bool DecryptFile(string ReadFilename, string WriteFilename)
- {
- try
- {
- DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
- DES.Key = ASCIIEncoding.ASCII.GetBytes(AESKey);
- DES.IV = ASCIIEncoding.ASCII.GetBytes(AESKey);
- FileStream fsread = new FileStream(ReadFilename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
- ICryptoTransform Decrypt = DES.CreateDecryptor();
- CryptoStream cryptostreamDecr = new CryptoStream(fsread, Decrypt, CryptoStreamMode.Read);
- StreamWriter fsDecrypted = new StreamWriter(WriteFilename);
- fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
- fsDecrypted.Flush();
- fsDecrypted.Close();
- return true;
- }
- catch { return false; }
- }
- }
- //
- // 패킷 암호화
- //
- public static partial class _Encrypt
- {
- private static byte[] AESKeys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
- public static string AES128Encode(string encryptString, string encryptKey)
- {
- encryptKey = encryptKey.PadRight(32, ' ');
- RijndaelManaged rijndaelProvider = new RijndaelManaged();
- rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
- rijndaelProvider.IV = AESKeys;
- ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();
- byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
- byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
- return Convert.ToBase64String(encryptedData);
- }
- public static string AES128Decode(string decryptString, string decryptKey)
- {
- try
- {
- decryptKey = decryptKey.PadRight(32, ' ');
- RijndaelManaged rijndaelProvider = new RijndaelManaged();
- rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey);
- rijndaelProvider.IV = AESKeys;
- ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();
- byte[] inputData = Convert.FromBase64String(decryptString);
- byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
- return Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length);
- }
- catch { return string.Empty; }
- }
- }
- }
|