I have improved my source code snippet.
See if any user can improve this one below.
private void Begin_Encryption_Button_Click(object sender, EventArgs e)
{
try
{
File_For_Reading = File_Name_For_Reading_Text_Box.Text;
File_For_Writing = File_Name_For_Writing_Text_Box.Text;
Encrypt_Beginning_Point = int.Parse(Beginning_Part_of_Encryption_Text_Box.Text);
Encrypt_End_Point = int.Parse(Ending_Part_of_Encryption_Text_Box.Text);
using (RijndaelManaged aes = new RijndaelManaged())
{
byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);
using (FileStream xCrypt = new FileStream(File_For_Writing, FileMode.Create))
{
using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
{
using (CryptoStream cX = new CryptoStream(xCrypt, encryptor, CryptoStreamMode.Write))
{
using (FileStream XIn = new FileStream(File_For_Reading, FileMode.Open))
{
int data;
while (((data = XIn.ReadByte()) >=Encrypt_Beginning_Point) && (data = XIn.ReadByte()) <= Encrypt_End_Point)
{
cX.WriteByte((byte)data);
MessageBox.Show("Success! Part of file has been encrypted.");
}
}
}
}
}
}
}
catch (Exception)
{
MessageBox.Show("Failed to encrypt file.");
}
}