Wednesday, June 20, 2012

how to encrypt a file in c#

Encryption is a secure process ,which is used to transfer the data into code by appending a key .The resultant of the process is the encryption information. Here i will show how to encrypt a file in csharp.To do this process we have to add a name space "System.Security.Cryptography" .In this function i have used a key "pkey" to encrypt the data and the "outputpath" is resultant encrypted data
FileStream fs= new FileStream(Outputpath,FileMode.Create,FileAccess.Write);
DESCryptoServiceProvider DEScs = new DESCryptoServiceProvider();
DEScs.Key = ASCIIEncoding.ASCII.GetBytes(pKey);
DEScs.IV = ASCIIEncoding.ASCII.GetBytes(pKey);
ICryptoTransform desencrypt = DEScs.CreateEncryptor();
CryptoStream cstream = new CryptoStream(fs,desencrypt,CryptoStreamMode.Write);
FileStream fsInput = new FileStream(Inputpath,FileMode.Open,FileAccess.Read)
byte[] ba = new byte[fsInput.Length];
fsInput.Read(ba, 0, ba.Length);
cstream.Write(ba, 0, ba.Length);
Post a Comment