public void DecryptFile() { #region Code for decrypting a file byte[] PasswordBytes = new UnicodeEncoding().GetBytes(Password.ToCharArray()); byte[] KeyBytes; RijndaelManaged Crypter = new RijndaelManaged(); KeyBytes = new GenerateIV(Password).Get256bitIV(); Crypter.BlockSize = KeyBytes.Length * 8; FileStream ReadSBN = new FileStream(string.Format("{0}{1}", ConfigurationSettings.AppSettings["AllEntityLocation"] + ConfigurationSettings.AppSettings["DecryptionFolder"], FileName + "_2." + ConfigurationSettings.AppSettings["SecureExtension"]), FileMode.Open, FileAccess.Read); FileStream WriteSTF = new FileStream(string.Format("{0}{1}", ConfigurationSettings.AppSettings["AllEntityLocation"] + ConfigurationSettings.AppSettings["DecryptionFolder"], FileName + "_3." + ConfigurationSettings.AppSettings["SecureBinTemporaryExtension"]), FileMode.Create, FileAccess.Write); CryptoStream WriteDecrypted = new CryptoStream(WriteSTF, Crypter.CreateDecryptor(PasswordBytes, KeyBytes), CryptoStreamMode.Write); for (int index = 0; index < ReadSBN.Length; ++index) { byte data = (byte)ReadSBN.ReadByte(); WriteDecrypted.WriteByte(data); } WriteDecrypted.Close(); WriteSTF.Close(); ReadSBN.Close(); #endregion }
public void ExecuteEncrypt(string FileName, string password) { byte[] passwordBytes = new UnicodeEncoding().GetBytes(password); byte[] keyBytes; FileStream ReadSTF = new FileStream(ConfigurationSettings.AppSettings["AllEntityLocation"] + ConfigurationSettings.AppSettings["EncryptionFolder"] + FileName + "_2" + "." + ConfigurationSettings.AppSettings["SecureBinTemporaryExtension"], FileMode.Open, FileAccess.Read); RijndaelManaged Encrypter = new RijndaelManaged(); GenerateIV generateIV = new GenerateIV(password); keyBytes = generateIV.Get256bitIV(); Encrypter.BlockSize = keyBytes.Length * 8; FileStream WriteSBN = new FileStream(ConfigurationSettings.AppSettings["AllEntityLocation"] + ConfigurationSettings.AppSettings["EncryptionFolder"] + FileName + "_Enc." + ConfigurationSettings.AppSettings["SecureExtension"], FileMode.Create, FileAccess.Write); CryptoStream EncryptStream = new CryptoStream(WriteSBN, Encrypter.CreateEncryptor(passwordBytes, keyBytes), CryptoStreamMode.Write); for (int index = 0; index < ReadSTF.Length; ++index) { byte data = (byte)ReadSTF.ReadByte(); EncryptStream.WriteByte(data); } EncryptStream.Close(); WriteSBN.Close(); ReadSTF.Close(); }