public void ExecuteEncrypt(string FileName, string password) { byte[] passwordBytes = new UnicodeEncoding().GetBytes(password); byte[] keyBytes; FileStream ReadFile = new FileStream(FileName, FileMode.Open, FileAccess.Read); RijndaelManaged Encrypter = new RijndaelManaged(); GenerateIV generateIV = new GenerateIV(password); keyBytes = generateIV.Get256bitIV(); Encrypter.BlockSize = keyBytes.Length * 8; FileStream WriteEncrypted = new FileStream(GetWithoutExtension(FileName) + ".LCK", FileMode.Create, FileAccess.Write); CryptoStream EncryptStream = new CryptoStream(WriteEncrypted, Encrypter.CreateEncryptor(passwordBytes, keyBytes), CryptoStreamMode.Write); for (int index = 0; index < ReadFile.Length; ++index) { byte data = (byte)ReadFile.ReadByte(); EncryptStream.WriteByte(data); } EncryptStream.Close(); WriteEncrypted.Close(); ReadFile.Close(); }
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 ReadFile = new FileStream(GetWithoutExtension(FileName) + ".LCK", FileMode.Open, FileAccess.Read); FileStream WriteFile = new FileStream(GetWithoutExtension(FileName) + "_DEC.LCK", FileMode.Create, FileAccess.Write); try { CryptoStream WriteDecrypted = new CryptoStream(WriteFile, Crypter.CreateDecryptor(PasswordBytes, KeyBytes), CryptoStreamMode.Write); for (int index = 0; index < ReadFile.Length; ++index) { byte data = (byte)ReadFile.ReadByte(); WriteDecrypted.WriteByte(data); } WriteDecrypted.Close(); } catch (Exception LoginFailure) { throw new ApplicationException("Authentication Exception"); } finally { WriteFile.Close(); ReadFile.Close(); } #endregion }