public static string DoEncrypt(string dataToEncrypt, string FileNames, string _entropy) { if (string.IsNullOrWhiteSpace(_entropy)) { _entropy = EntropyGenerator.GetIPForMachine(); } if (string.IsNullOrWhiteSpace(FileNames)) { FileName = Application.StartupPath + @"\Data.txt"; } else { FileName = FileNames; } //Write to file if (File.Exists(Application.StartupPath + @"\dataToEncrypt.txt")) { System.IO.File.Delete(Application.StartupPath + @"\dataToEncrypt.txt"); } System.IO.File.WriteAllText(Application.StartupPath + @"\dataToEncrypt.txt", dataToEncrypt); EncDec.Encrypt(Application.StartupPath + @"\dataToEncrypt.txt", FileName, _entropy); System.IO.File.Delete(Application.StartupPath + @"\dataToEncrypt.txt"); //EncDec.Decrypt(@"C:\Users\224702\Desktop\Sample1.txt", @"C:\Users\224702\Desktop\Sample2.txt", @"127.0.0.1"); /* * entropy = UnicodeEncoding.ASCII.GetBytes(_entropy.ToCharArray(), 0, 16); * IV = UnicodeEncoding.ASCII.GetBytes(Reverse(_entropy).ToCharArray(), 0, 16); * * // Create a new instance of the Aes * // class. This generates a new key and initialization * // vector (IV). * encryptedData = dataToEncrypt; * FileName = FileNames; * * FileStream stream = new FileStream(FileName, * FileMode.OpenOrCreate,FileAccess.Write); * * Aes cryptic = Aes.Create(); * //cryptic.Padding = PaddingMode.None; * cryptic.Key = entropy; * cryptic.IV = IV; * * // Create a decrytor to perform the stream transform. * ICryptoTransform decryptor = cryptic.CreateEncryptor(entropy, IV); * * CryptoStream crStream = new CryptoStream(stream, * decryptor, CryptoStreamMode.Write); * * * byte[] data = ASCIIEncoding.ASCII.GetBytes(dataToEncrypt); * * crStream.Write(data,0,data.Length); * * crStream.Close(); * stream.Close(); * * * // Encrypt the string to an array of bytes. * // byte[] encrypted = EncryptStringToBytes_Aes(dataToEncrypt, * //entropy,IV); * * // string roundtrip = DecryptStringFromBytes_Aes(encrypted, * //entropy, IV); * * * * * * * * * * //if(_entropy != null) * // entropy = UnicodeEncoding.ASCII.GetBytes(_entropy); * //DoEncrypt();*/ return(FileName); }
public static void DoDecrypt(string FileNames, string _entropy) { if (string.IsNullOrWhiteSpace(_entropy)) { _entropy = EntropyGenerator.GetIPForMachine(); } /*if (_entropy != null) * { * _entropy = _entropy + "test this app for exam app"; * } * else * _entropy = EntropyGenerator.GetIPForMachine() + "test this app for exam app"; * * entropy = UnicodeEncoding.ASCII.GetBytes(_entropy.ToCharArray(), 0, 16); * IV = UnicodeEncoding.ASCII.GetBytes(Reverse(_entropy).ToCharArray(), 0, 16); */ decryptedData = null; // Open the file. if (string.IsNullOrWhiteSpace(FileNames)) { FileName = Application.StartupPath + @"\Data.txt"; } else { FileName = FileNames; } //if (FileName.EndsWith(".xml")) //{ // EncDec.Decrypt(FileName, Application.StartupPath + @"\dataToDecrypt.xml", _entropy); // decryptedData = System.IO.File.ReadAllText(Application.StartupPath + @"\dataToDecrypt.xml"); //} //else //{ if (File.Exists(Application.StartupPath + @"\dataToDecrypt.txt")) { System.IO.File.Delete(Application.StartupPath + @"\dataToDecrypt.txt"); } try { EncDec.Decrypt(FileName, Application.StartupPath + @"\dataToDecrypt.txt", _entropy); decryptedData = System.IO.File.ReadAllText(Application.StartupPath + @"\dataToDecrypt.txt"); System.IO.File.Delete(Application.StartupPath + @"\dataToDecrypt.txt"); } catch (Exception ex) { LogEntry.WriteLog(ex, "Thread Exception"); } //} /* * fStream = new FileStream("Data.dat", FileMode.Open); * } * * * * * * Aes aesAlg = Aes.Create(); * * aesAlg.Key = entropy; * aesAlg.IV = IV; * // aesAlg.Padding = PaddingMode.None; * // Create a decrytor to perform the stream transform. * ICryptoTransform encryptor = aesAlg.CreateDecryptor(entropy, IV); * CryptoStream crStream = new CryptoStream(fStream, * encryptor, CryptoStreamMode.Read); * * StreamReader reader = new StreamReader(crStream); * * decryptedData = reader.ReadToEnd(); * * reader.Close(); * fStream.Close(); * * * /*CODE * int bytesWritten = Convert.ToInt32(f.Length); * * byte[] inBuffer = new byte[bytesWritten]; * * * // Read the encrypted data from a stream. * if (fStream.CanRead) * { * fStream.Read(inBuffer, 0, bytesWritten); * } * * /* byte[] buffer = new byte[bytesWritten]; * using (MemoryStream ms = new MemoryStream()) * { * int read; * while ((read = fStream.Read(buffer, 0, buffer.Length)) > 0) * { * ms.Write(buffer, 0, read); * } * * * * // Decrypt the bytes to a string. * string roundtrip = DecryptStringFromBytes_Aes(inBuffer, * entropy, IV); */ //} /*if (_entropy != null) * entropy = UnicodeEncoding.ASCII.GetBytes(_entropy); * DoDecrypt();*/ }