question

John-1477 avatar image
0 Votes"
John-1477 asked DanielZhang-MSFT commented

Partial encryption of a binary file

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?



dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @John-1477,
May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.
Best Regards,
Daniel Zhang

0 Votes 0 ·
DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT edited

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

John-1477 avatar image
0 Votes"
John-1477 answered John-1477 commented

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.");
    
             }
         }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Note that I have tried to paste the code as part of the comment and not part of the answer. My apologies.
Can anyone improve this source code snippet above?

0 Votes 0 ·