File Encryption
Introduction
The classes in the .Net Framework cryptographynamespace manage many details of cryptography for you. Some arewrappers for the unmanaged Microsoft CryptoAPI, while others are purelymanaged implementations. Cryptography protects data from being viewedor modified and provides secure channels of communication overotherwise insecure channels. For example, data can be encrypted using acryptographic algorithm, transmitted in an encrypted state, and laterdecrypted by the intended party. If a third party intercepts theencrypted data, it will be difficult to decipher the data. We use acombination of algorithms and practices known as cryptographicprimitives to create a cryptographic scheme. Those primitives are:private-key encryption, public-key encryption, cryptographic signingand cryptographic hashes.
Private-key encryption (symmetric cryptography)
In this article and code example I've usedprivate-key encryption to encrypt files. Private-key encryptionalgorithms use a single private key to encrypt and decrypt data so italso referred to as symmetric encryption because the same key is usedfor encryption and decryption. Thus, we need a key and aninitialization vector (IV) to encrypt and decrypt data. Without an IVthe same input block of plaintext will encrypt to same output block ofciphertext, but with IV the output of two identical plaintext blocksare different and it is hard for unauthorized user to recover the key.The disadvantage of private-key encryption is that it presumes twoparties have agreed on a key and IV and communicated their values.Also, the key must be kept secret from unauthorized users. Because ofthese problems, private-key encryption is often used in conjunctionwith public-key encryption to privately communicate the values of thekey and IV.
The .NET Framework provides the following classes that implement private-key encryption algorithms:
� DESCryptoServiceProvider (DES algorithm)
� RC2CryptoServiceProvider (RC2 algorithm)
� RijndaelManaged (Rijndael algorithm)
� TrippleDESCryptoServiceProvider (TrippleDES algorithm)
Code explanation
In this simple example I use a Rijndaelalgorithm to encrypt files. First, to encrypt file, we have to make akey and IV (16 bytes each). Below is shown how to compose a key and anIV (key and IV have the same value) from password entered by user(Form1.EncryptFile() function):
if (password.Length > A key and an IV have to be byte[] type.Because the key and IV have to be exactly 16 bytes long by default, wehave to add some characters if the password is less then 8 characters(1 character = 2 bytes) or we have to truncate password string if ithas have more then 8 characters. Next, we have to create a FileStream instance for crypted data (cryptFile is file where crypted data should be written): FileStream fsCrypt=new FileStream(cryptFile, FileMode.Create); Next, we create a instance of RijndaelManagedclass and an instance of special stream class called a CryptoStreamthat encrypts data read into the stream. The CryptoStream class isinitialized with managed stream class (FileStream), a class thatimplements the ICryptoTransform interface (created from a class thatimplements a cryptographic algorithm – RijndaelManaged) and aCryptoStreamMode enumeration that describes the type of accesspermitted to the CryptoStream: RijndaelManaged RMCrypto = new RijndaelManaged(); After the previous code is executed, any data written to the CryptoStream object is encrypted using the Rijndael algorithm: FileStream fsIn=new FileStream(textBox1.Text,FileMode.Open); Decryption is very similar to encryption. Download FileCrypotgraphy.zip
password=password.Substring(0, 8);
else if (password.Length <
{
int add=8-password.Length;
for (int i=0; i < add; i++)
password=password+i;
}
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
int data;
while ((data=fsIn.ReadByte())!=-1)
cs.WriteByte((byte) data);
Most Commented Articles :




13. Sep, 2006 by 







No comments yet... Be the first to leave a reply!