Exercise
Encrypt A BMP File
Objective
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.
Write Your C# Exercise
C# Exercise Example
// 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
}
}
}
More C# Programming Exercises of File Management
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
This exercise involves creating a program that reads a CSV file with four data blocks (three text fields and one numeric) separated by commas, and generates a text file wher...
This exercise involves creating a C# program that determines if two files (of any kind) are identical, meaning they have the same content. The program should c...
This exercise involves creating a C# program to decode an image file in the Netpbm format (specifically, the P1 format, which is for black and white ima...
This exercise involves creating a C# program to check if a file is a PCX image and, if so, extract and display the image's dimensions (width and height). To do...
This exercise involves creating a C# program that extracts only the alphabetic characters contained in a binary file and dumps them to a separate file. The program sh...
This exercise involves creating a C# program that converts simple C# programs to their equivalent in the Pascal language. The program should read C# cod...