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.
Code
Imports System
Imports System.IO
Namespace Bmp
Class Program
Private Shared Sub Main(ByVal args As String())
Dim myFile As FileStream
Console.Write("Enter the name of file: ")
Dim fileName As String = Console.ReadLine()
If Not File.Exists(fileName) Then
Console.WriteLine("The file not exists!!!")
Return
End If
Try
myFile = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite)
Dim b1 As Byte = CByte(myFile.ReadByte())
Dim b2 As Byte = CByte(myFile.ReadByte())
If (Convert.ToChar(b1) <> "B"c) OrElse (Convert.ToChar(b2) <> "M"c) Then
Console.WriteLine("This File is NOT a BMP file")
Else
myFile.Seek(0, SeekOrigin.Begin)
myFile.WriteByte(CByte("M"c))
myFile.WriteByte(CByte("B"c))
End If
myFile.Close()
Catch e As Exception
Console.WriteLine("Error: {0}!!!", e.message())
End Try
End Sub
End Class
End Namespace