Encoding and Decoding Data with ScriptEZ.API Component

 

When ScriptEZ.API based script file is running, an identification area is appended

to the system menu of the console windows as following:

 

 

'//////////////////////////////////////////////////////////////////////////////

' Author: SY

' Copyright 2011

' Demo ScriptEZ.API based application which uses facilities from RAD API:

' . EncryptBSTR() to crypt and to uncrypt simple string data (symetric algo)

' . Echo() method usage which displays always messages into a console window

'   instead of WScript.Echo() with 2 behaviours for cscript.exe or wscript.exe

' . ScriptEZ.API Remoting Control Interface usage by NPTelnet.exe program

'   (Connect to ScriptEZ.API process with <processid (PID)> as connection name)

'

' Purpose: Simple script showing how to crypt and uncrypt string data

'//////////////////////////////////////////////////////////////////////////////

 

  const SCRIPTEZ_API = "ScriptEZ.API" ' Component name

  Dim g_onExit

 

  Set WshShell = CreateObject("WScript.Shell")

  Set fso      = CreateObject("Scripting.FileSystemObject")

 

  Dim Windir

  Dim FullQualifiedEngineName

  ' if Win64, restart with WScript.exe/CScript.exe 32 bits

  On Error Resume Next

  Windir = WshShell.ExpandEnvironmentStrings("%WinDir%")

  FullQualifiedEngineName = Windir+"\SysWOW64" + Mid(WScript.FullName,InStrRev(WScript.FullName,"\"),Len(WScript.FullName))

  If fso.FolderExists(Windir+"\SysWOW64") And _

     LCase(WScript.FullName) <>  LCase(FullQualifiedEngineName) And _

     fso.FileExists(FullQualifiedEngineName) = True Then

    WshShell.Run FullQualifiedEngineName + " " + Chr(34) + CStr(WScript.ScriptFullName) + Chr(34)

    WScript.Quit

  End If

 

  ' instantiate component object with callback interface

  Set ScriptEZ = WScript.CreateObject(SCRIPTEZ_API,"ScriptEZ_")

 

  ' set affinity to Core1

  ScriptEZ.SetCPUCoreAffinity "0"

 

  r = ScriptEZ.CenterWindow(-1)

  Randomize   ' Initialize random-number generator

 

  ' Register App for receiving AppEvent(s) with onAppEvent(EventID) callback

  ScriptEZ.AppRegister

 

  g_onExit = 0

 

  good = 0

  bad  = 0

  ' Endless loop for demo-ing

  Do while(g_onExit = 0)

 

   ' Random key for cryting data

   crypt_key = CStr(Int((6666 * Rnd) + 1024)) + "#$!&~" + _

               CStr(Second(Now)) + _

               CStr(Int((66 * Rnd) + 10))

 

   Wscript.Sleep 1000

 

   texte     = chr(34) + "Aujourd'hui avec ce hashtag" + " #" + CStr(Int((1234 * Rnd))) + _

               ", nous sommes le " + CStr(Date) + " à " + CStr(Time) + chr(34)

 

   l         = " (" + CStr(Len(texte)) + ") "

   ' Applying crypting and Displaying crypted data

   crypted   = ScriptEZ.EncryptBSTR(texte,crypt_key)

 

   ' Applying uncrypting

   uncrypted = ScriptEZ.EncryptBSTR(crypted,crypt_key)

  

   ' If corrupted data then make a bip sound

   If texte <> uncrypted Then

     ScriptEZ.Echo "" ' make a bip

     ScriptEZ.Echo ". Clé de cryptage aléatoire: " + crypt_key + vbLF

     ScriptEZ.Echo texte   +  l  + "->Texte original<-" + vbLF

     ScriptEZ.Echo crypted + vbLF

     ScriptEZ.Echo uncrypted + " (failed) ->Texte décrypté<-" + vbLF  + vbLF

     bad = bad + 1

   Else

     ScriptEZ.Echo ". Clé de cryptage aléatoire: "   + crypt_key + vbLF

     ScriptEZ.Echo texte     + l + "->Texte original<-" + vbLF

     ScriptEZ.Echo crypted + vbLF

     ScriptEZ.Echo uncrypted + " (ok) ->Texte décrypté<-" + vbLF  + vbLF

     good = good + 1

   End If

   ScriptEZ.SetConsoleTitle "Demoing Crypting and Uncrypting Data: sucess(" + CStr(good) + _

                            "), failure(" + CStr(bad) + "), used_memory(" +  _

                            CStr(ScriptEZ.ProbeMemoryStatus(7)) + " KBytes)"

 

   ScriptEZ.FreeBSTR crypted

   ScriptEZ.FreeBSTR uncrypted

  Loop

 

 

' Callbacks for App Events notification and fetching help text

'/////////////////////////////////////////////////////////////

Sub ScriptEZ_onAppEvent(EventID)

  Select Case EventID

   Case  3 

        g_onExit = 3

        ScriptEZ.Echo g_exit

  End Select

End Sub

 

Function ScriptEZ_AppEventHelp()

  ScriptEZ_AppEventHelp = "AppEventID = 3 to exit gracefully"

End Function