'  
'   Rebex Sample Code License
' 
'   Copyright 2026, REBEX CR s.r.o.
'   All rights reserved.
'   https://www.rebex.net/
' 
'   Permission to use, copy, modify, and/or distribute this software for any
'   purpose with or without fee is hereby granted.
' 
'   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'   OTHER DEALINGS IN THE SOFTWARE.
' 

Imports System
Imports System.IO
Imports Rebex.Security

Class Program
    Friend Shared Sub Main(args As String())
            ' set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key


        Try
            ' parse arguments
            Dim arguments As New Arguments(args)

            ' initialize FileEncryption object
            Dim encryption As New FileEncryption()
            encryption.SetPassword(arguments.Password)
            encryption.OverwriteExistingFile = arguments.Overwrite

            ' perform specified operation
            If arguments.Operation = Operation.Encrypt Then
                encryption.Encrypt(arguments.SourceFile, arguments.DestinationFile)
                Console.WriteLine("File successfully encrypted.")
            Else
                encryption.Decrypt(arguments.SourceFile, arguments.DestinationFile)
                Console.WriteLine("File successfully decrypted.")
            End If
        Catch e As SampleException
            ' display an error reported by arguments parser class
            ShowHelp()
            Console.WriteLine()
            Console.WriteLine("Error: {0}", e.Message)
        Catch ex As IOException
            ' display IO error
            Console.WriteLine(ex.Message)
        Catch ex As InvalidOperationException
            ' display invalid operation error
            Console.WriteLine(ex.Message)
        Catch ex As Exception
            ' display all other errors
            Console.WriteLine("Error: {0}", ex)
        End Try
    End Sub

    Private Shared Sub ShowHelp()
        Dim encrypt As String = Operation.Encrypt.ToString().ToLower()
        Dim decrypt As String = Operation.Decrypt.ToString().ToLower()
        Dim applicationName As String = AppDomain.CurrentDomain.FriendlyName

        Console.WriteLine("=====================================================================")
        Console.WriteLine(" Rebex {0} - Encrypts or decrypts files", applicationName)
        Console.WriteLine("=====================================================================")
        Console.WriteLine()
        Console.WriteLine("The program is a sample for Rebex Security for .NET component.")
        Console.WriteLine("For more info, see http://www.rebex.net/security/")
        Console.WriteLine()
        Console.WriteLine("Syntax: {0} {1}|{2} <source file> <destination file> password [overwrite]", applicationName, encrypt, decrypt)
        Console.WriteLine("Example: {0} {1} C:\file.txt C:\file.enc secret-password overwrite", applicationName, encrypt)
        Console.WriteLine("Example: {0} {1} C:\file.enc C:\file.txt secret-password", applicationName, decrypt)
    End Sub
End Class
