/*
* Util.Decrypt.cs
* Authors: Mike Dapiran, Adam Nabinger
* based on code by August Zinsser
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Util
{
///
/// This function is based on the Microsoft tutorial http://support.microsoft.com/kb/307010
///
public static class Decrypt
{
#region Constants
// Key string constant to encode with
private const string key = "theautis";
#endregion
#region Fields
// Allows access to the Data Encryption Standard algorithm
private static readonly DESCryptoServiceProvider DES;
#endregion
#region Creation
///
/// Constructor
///
static Decrypt()
{
//A 64 bit key and IV is required for this provider.
DES = new DESCryptoServiceProvider();
//Set secret key For DES algorithm.
DES.Key = Encoding.ASCII.GetBytes(key);
//Set initialization vector.
DES.IV = Encoding.ASCII.GetBytes(key);
}
#endregion
#region Decryption/Encryption
///
/// Decrypts the given config file assuming that tAC_Encrypt.exe encrypted it. This is done solely to
/// hide the config file from the user.
///
/// Encrypted File
/// The ascii-formatted decrypted contents of the input file
[DebuggerHidden]
public static string DecryptFile(string inputFileName)
{
//Create a file stream to read the encrypted file back.
using (FileStream fsread = new FileStream(inputFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
//Create a DES decryptor from the DES instance.
using (ICryptoTransform desdecrypt = DES.CreateDecryptor())
//Create crypto stream set to read and do a DES decryption transform on incoming bytes.
using (CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read))
using (StreamReader sr = new StreamReader(cryptostreamDecr))
{
return sr.ReadToEnd();
}
}
///
/// Encrypts the given file and saves it into a new file
///
/// The name of the unencrypted file being loaded
/// The name of the encrypted file being saved
[DebuggerHidden]
public static void EncryptFile(string sInputFilename, string sOutputFilename)
{
using (FileStream stream = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read))
using (FileStream stream2 = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write))
using (ICryptoTransform transform = DES.CreateEncryptor())
using (CryptoStream stream3 = new CryptoStream(stream2, transform, CryptoStreamMode.Write))
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream3.Write(buffer, 0, buffer.Length);
}
}
#endregion
}
}