[go: up one dir, main page]

0% found this document useful (0 votes)
84 views2 pages

Encryption and Decryption

This document contains code to encrypt and decrypt files using the Rijndael encryption algorithm. It defines private methods to encrypt and decrypt files that take input and output file paths as parameters. The encryption method reads the input file byte by byte, encrypts it using a RijndaelManaged object, and writes it to the output file. The decryption method does the reverse, decrypting the encrypted input file and writing it to the output file. Both methods use the same password and Unicode encoding to generate the encryption key.

Uploaded by

Gebeyaw Negn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views2 pages

Encryption and Decryption

This document contains code to encrypt and decrypt files using the Rijndael encryption algorithm. It defines private methods to encrypt and decrypt files that take input and output file paths as parameters. The encryption method reads the input file byte by byte, encrypts it using a RijndaelManaged object, and writes it to the output file. The decryption method does the reverse, decrypting the encrypted input file and writing it to the output file. Both methods use the same password and Unicode encoding to generate the encryption key.

Uploaded by

Gebeyaw Negn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C#

Shrink ▲   Copy Code


///<summary>
/// Steve Lydford - 12/05/2008.
///
/// Encrypts a file using Rijndael algorithm.
///</summary>
///<param name="inputFile"></param>
///<param name="outputFile"></param>
private void EncryptFile(string inputFile, string outputFile)
{

try
{
string password = @"myKey123"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);

string cryptFile = outputFile;


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

RijndaelManaged RMCrypto = new RijndaelManaged();

CryptoStream cs = new CryptoStream(fsCrypt,


RMCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write);

FileStream fsIn = new FileStream(inputFile, FileMode.Open);

int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);

fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch
{
MessageBox.Show("Encryption failed!", "Error");
}
}
///<summary>
/// Steve Lydford - 12/05/2008.
///
/// Decrypts a file using Rijndael algorithm.
///</summary>
///<param name="inputFile"></param>
///<param name="outputFile"></param>
private void DecryptFile(string inputFile, string outputFile)
{

{
string password = @"myKey123"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);

FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

RijndaelManaged RMCrypto = new RijndaelManaged();

CryptoStream cs = new CryptoStream(fsCrypt,


RMCrypto.CreateDecryptor(key, key),
CryptoStreamMode.Read);

FileStream fsOut = new FileStream(outputFile, FileMode.Create);

int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);

fsOut.Close();
cs.Close();
fsCrypt.Close();

}
}

You might also like