Paul Mendoza C# blog
Wednesday, December 06, 2006
  C# file encryption class using Rijndael method

I’ve written quite a few encryption classes over the last month for File Phantom and I wrote this one while doing some testing that I thought was pretty simple to use if you need to do a quick encryption in C# using the Rijndael method. It takes a password that’s basically going to become the encryption key, creates the encryption key from the password and then creates the encrypted file and the last parameter sets whether the file that is being encrypted should become the encrypted file. If so, I replace the original and delete the newly created encrypted file. This last step might not be the most efficient but it seems safer than actually deleting one of the copies entirely and leaving the other in memory.

Feel free to use this code wherever you want.

The part of the code where I assemble the key I got from here.








using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Timers;


namespace FileEncrypterC
{
/// <summary>
/// This class is used to encrypt files.
/// </summary>
public class EncryptFileTools
{

/// <summary>
/// Encrypts a file. Takes time.
/// </summary>
/// <param name="password">The password key. This is what determines the encryption method used.</param>
/// <param name="_path">Path of the file that is to be encrypted.</param>
/// <param name="_outputFile">Path of the encrypted file.</param>
public static void Encrypt(String password, String _path, String _outputFile, bool _saveToSameLocationAsPath)
{
// This code came from another guide online.
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
}

UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);

FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);


RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);

FileStream fsIn = new FileStream(_path, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);

fsIn.Close();
cs.Close();
fsCrypt.Close();
// The GC will collect the old objects faster
cs = null;
fsIn = null;
//fsCrypt = null;

// Now that the encrypted file is created, we'll replace the original file file with the output file
// and delete the old file.
if (_saveToSameLocationAsPath)
{
File.Replace(_outputFile, _path, _outputFile + ".backup");
File.Delete(_outputFile + ".backup");
}
}
/// <summary>
/// Decrypt a file and save it to an outputed file.
/// </summary>
/// <param name="password">Password key</param>
/// <param name="_path">File that's already encrypted.</param>
/// <param name="_outputFilePath">File that results from decryption</param>
public static void Decrypt(String password, String _path, String _tempOutputFilePath, bool _saveToSameLocationAsPath)
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);

// Filestream of the new file that will be decrypted.
FileStream fsCrypt = new FileStream(_tempOutputFilePath, FileMode.Create);

RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Write);


// FileStream of the file that is currently encrypted.
FileStream fsIn = new FileStream(_path, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);

cs.Close();
fsIn.Close();
fsCrypt.Close();
// The GC will collect the old objects faster
cs = null;
fsIn = null;
fsCrypt = null;

// Now that the encrypted file is created, we'll replace the original file file with the output file
// and delete the old file.
if (_saveToSameLocationAsPath)
{
File.Replace(_tempOutputFilePath, _path, _tempOutputFilePath + ".backup");
File.Delete(_tempOutputFilePath + ".backup");
}
}
}

/// <summary>
/// My little program to encrypt and decrypt a file using the

// Rijndael RijndaelManaged class in the .NET System.Security.Cryptography library

/// </summary>
public class Program
{
static void Main(string[] args)
{


String _path = @"C:\Documents and Settings\Paul Mendoza\My Documents\TOOL.mp3";
String _outputPathForDecrypedFile = @"C:\Folder.jpg";
String _encodedFilePath = @"C:\newFolder.jpg";

Console.WriteLine("Press any key to encrypt the file: " + _outputPathForDecrypedFile);
Console.ReadKey();

EncryptFileTools.Encrypt("paulmendoza", _outputPathForDecrypedFile, _encodedFilePath, true);

Console.WriteLine("File encrypted.");
Console.WriteLine(Environment.NewLine + "Press a key to decrypt: " + _outputPathForDecrypedFile);
Console.ReadKey();


EncryptFileTools.Decrypt("paulmendoza", _outputPathForDecrypedFile, _encodedFilePath, true);

Console.WriteLine("File decrypted...." + Environment.NewLine);


}
}

}
 
Comments:
You need some exception handling for your file IO. buzz me (you have my email) and I'll explain.

- Martin ([760]farrowz)
 
Post a Comment





<< Home
I am currently an ASP.NET, C# developer working on MangosteenNation.com, a XanGo website for helping people build their businesses. I am also pursuing a degree at CSU San Marcos in Southern California.

XanGo at Mangosteen Nation

Archives
October 2005 / November 2005 / December 2005 / January 2006 / February 2006 / March 2006 / April 2006 / May 2006 / June 2006 / July 2006 / August 2006 / September 2006 / October 2006 / November 2006 / December 2006 / January 2007 / April 2007 / May 2007 / June 2007 / August 2007 / February 2008 / August 2008 /


Powered by Blogger

Subscribe to
Posts [Atom]