Imports CryptoSysAPI

' $Id: DemoMain.vb $
'****************************************************************************
' Copyright 2007-9 DI Management Services Pty Limited, All Rights Reserved.
'****************************************************************************
' Distribution: You can freely use this code in your own applications, but
' you may not reproduce or publish this code on any web site, online service,
' or distribute as source on any media without express permission.
' Terms: Use at your own risk. Provided "as is" with no warranties.
' Contact: <www.di-mgt.com.au> <www.cryptosys.net>
'****************************************************************************
' This file last updated:
'   $Date: 2009-03-19 16:31:00 $
'****************************************************************************

Module DemoMain
    ' Main code to do the basic tasks - everything else is just for show

    Public Function SetNonceHex() As String
        ' Generate a random nonce the same size as the AES-128 block (128 bits)
        Return Rng.NonceHex(Aes128.BlockSize)
    End Function

    Public Function EncryptWithPassword(ByVal strPassword As String, ByVal strSaltHex As String, ByVal nCount As Integer, ByVal strPlain As String) As String
        ' Encrypts `ordinary' text given password, etc. and returns hex-encoded ciphertext
        Dim strKeyHex As String
        Dim strInputHex As String
        Dim strCipherHex As String

        ' 1. Form a 128-bit key from the password + salt + count
        strKeyHex = Pbe.Kdf2(Aes128.BlockSize, strPassword, strSaltHex, nCount)
        ' 2. Encode the plaintext input in hex format
        strInputHex = Cnv.ToHex(strPlain)
        ' 3. Pad the plaintext to an exact multiple of the encryption block size
        strInputHex = Aes128.Pad(strInputHex)
        ' 4. Encrypt this padded input using the key and the IV
        ' (we use the same value for the IV as we used for the salt above)
        strCipherHex = Aes128.Encrypt(strInputHex, strKeyHex, Mode.CBC, strSaltHex)

        ' Return the ciphertext in hex format
        Return strCipherHex
    End Function

    Public Function DecryptWithPassword(ByVal strPassword As String, ByVal strSaltHex As String, ByVal nCount As Integer, ByVal strCipherHex As String) As String
        ' Decrypts strCipherHex. Returns plaintext as an `ordinary' text string. 
        ' If fails, returns original hex-encoded ciphertext to indicate an error
        ' (this is because an empty string is a valid result).
        Dim strKeyHex As String
        Dim strPaddedHex As String
        Dim strPlainHex As String
        Dim strPlain As String

        ' 1. Form a 128-bit key from the password + salt + count
        strKeyHex = Pbe.Kdf2(Aes128.BlockSize, strPassword, strSaltHex, nCount)
        ' 2. Decrypt the ciphertext to get padded plaintext (IV = salt)
        strPaddedHex = Aes128.Decrypt(strCipherHex, strKeyHex, Mode.CBC, strSaltHex)
        ' 3. Check for error (i.e. an empty string)
        If strPaddedHex.Length = 0 Then
            Return strCipherHex
        End If
        ' 4. Unpad to retrieve the plaintext
        strPlainHex = Aes128.Unpad(strPaddedHex)
        ' 5. Check for error (this time strPlain *equal* to strPaddedHex)
        If strPlainHex.Length = strPaddedHex.Length Then
            Return strCipherHex
        End If
        ' 6. Encode the hex-encoded text into normal text
        strPlain = Cnv.StringFromHex(strPlainHex)

        ' Return the plaintext in hex format
        Return strPlain
    End Function


End Module