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.
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);
}
}
}
Subscribe to
Posts [Atom]