Exercise
Encrypt a BMP file
Objetive
Create a program to encrypt/decrypt a BMP image file by changing the "BM" mark in the first two bytes to "MB" and vice versa.
Use the advanced FileStream constructor to enable simultaneous reading and writing.
Example Code
// Import necessary namespaces for file handling
using System; // Basic input/output operations
using System.IO; // FileStream for file reading and writing
class BMPEncryptDecrypt // Main class for the BMP encryption/decryption program
{
static void Main(string[] args) // Entry point of the program
{
// Check if the correct number of arguments (file name) is provided
if (args.Length != 1) // If no file argument is provided
{
Console.WriteLine("Usage: BMPEncryptDecrypt "); // Show usage instructions
return; // Exit the program if the arguments are incorrect
}
string fileName = args[0]; // Get the file name from the command line argument
// Check if the file exists
if (!File.Exists(fileName)) // If the file does not exist
{
Console.WriteLine("Error: The file does not exist."); // Inform the user about the missing file
return; // Exit the program if the file is missing
}
try
{
// Open the BMP file for both reading and writing using FileStream
using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite)) // Open the file in read-write mode
{
byte[] buffer = new byte[2]; // Buffer to store the first two bytes of the BMP file
// Read the first two bytes to check if they are "BM" (0x42, 0x4D)
int bytesRead = fileStream.Read(buffer, 0, 2); // Read the first two bytes
// If the file does not have enough bytes, show an error
if (bytesRead < 2)
{
Console.WriteLine("Error: The file is too small to be a valid BMP file.");
return; // Exit the program if the file is too small
}
// Check if the first two bytes are "BM" (0x42, 0x4D)
if (buffer[0] == 0x42 && buffer[1] == 0x4D) // If the file starts with "BM"
{
Console.WriteLine("Encrypting file..."); // Inform the user that the file will be encrypted
// Change the first two bytes from "BM" to "MB" (0x4D, 0x42)
buffer[0] = 0x4D; // Change 'B' to 'M'
buffer[1] = 0x42; // Change 'M' to 'B'
fileStream.Seek(0, SeekOrigin.Begin); // Move the file pointer to the start of the file
fileStream.Write(buffer, 0, 2); // Write the modified bytes back to the file
Console.WriteLine("File encrypted."); // Notify the user that the encryption is done
}
else if (buffer[0] == 0x4D && buffer[1] == 0x42) // If the file starts with "MB"
{
Console.WriteLine("Decrypting file..."); // Inform the user that the file will be decrypted
// Change the first two bytes from "MB" to "BM" (0x42, 0x4D)
buffer[0] = 0x42; // Change 'M' to 'B'
buffer[1] = 0x4D; // Change 'B' to 'M'
fileStream.Seek(0, SeekOrigin.Begin); // Move the file pointer to the start of the file
fileStream.Write(buffer, 0, 2); // Write the modified bytes back to the file
Console.WriteLine("File decrypted."); // Notify the user that the decryption is done
}
else
{
Console.WriteLine("Error: The file does not appear to be a valid BMP file."); // Inform the user if the file is not a valid BMP
}
}
}
catch (Exception ex) // Catch any exceptions that occur during file handling
{
Console.WriteLine($"An error occurred: {ex.Message}"); // Display the error message
}
}
}