Partial encryption of a binary file

John 406 Reputation points
2021-07-02T04:54:39.34+00:00

Can users provide me some steps in how to encrypt part of a binary file?

Here I have this code snippet below:

      private void Encrypt_Region_Button_Click(object sender, EventArgs e)
        {
            try
            {
                string FileName = @"C:\Users\...\Documents\Binary Image 001.img";

                MessageBox.Show("Encrypting " + FileName);

                // Encrypt the file.
                AddEncryption(FileName);

                MessageBox.Show("Done");

            }
            catch (Exception)
            {
                MessageBox.Show("Unable to encrypt file.");
            }

        }

What I want to do now, but I don't know how to perform this is to encrypt part of a binary file.
Can users provide me step by step?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,271 questions
{count} votes

2 answers

Sort by: Most helpful
  1. John 406 Reputation points
    2021-07-02T05:59:09.163+00:00

    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.");
    
                }
            }
    

  2. Daniel Zhang-MSFT 9,611 Reputation points
    2021-07-05T06:56:14.673+00:00

    Hi John-1477,
    You can initialize a buffer and will be processing the file in chunks.

    using (FileStream XIn = new FileStream(File_For_Reading, FileMode.Open))  
    {  
            int bufferLen = Encrypt_End_Point - Encrypt_Beginning_Point + 1;   
            byte[] buffer = new byte[bufferLen];   
            int bytesRead;   
      
            do {   
                // read a chunk of data from the  file   
                bytesRead = XIn.Read(buffer, 0, bufferLen);   
      
                // Decrypt it   
                cX.Write(buffer, 0, bytesRead);   
      
            } while(bytesRead != 0);   
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments