How to Add MD5 Encryption into the Web Service Connector Tool

Summary

Certain web services require you to send an encrypted value during requests in order to authenticate either the user or the application. In a lot of cases this is made up of a pre-shared secret key, a username, a password and a transaction ID (usually incremental). In our example we are connecting to Companies House which requires an authentication value compromising of:

Md5 (Username + Password + Transaction ID)

This article identifies how you could go about encrypting data using MD5 within the TaskCentre DTL component.

Instructions

Prerequisites: Provided the TaskCentre system prerequisites have been met then no further software is required.

The solution requires a call onto the MD5 .NET object which can be called from within a Run VB Script function in the mapping tab.

Mapper

In the mapping tab screenshot above you can see the three input values TransactionID, SenderID and Password mapped onto the run VB Script function. The output of that function is mapped to the Authentication value element.

The screenshot below shows where the script is held. Access is by double clicking on the run VB Script function icon. The script forms a function which takes an Input string and makes the MD5 Hashed value available as an output string through a parameter. In order to perform the encryption it must first convert the string to a Byte Array and then hash that array. The resulting output can then be used to authenticate the client in the web service.

Function

The full code is shown below and is offered as an example only.

Dim Asc, enc, bytes, InStr, outstr, pos

'*************************************************

'* READ ME *

'* The InStr variable is used to hold the string *

'* you wish to encrypt, it can be made up of any *

'* number of attributes

'* *

'* Eg. InStr = Input1 & Input2 & Input3 *

'* or *

'* InStr = Input *

'* *

'*************************************************

InStr = SenderID & Password & TransactionID

'Get objects from .NET (supported from 1.1 onwards)

Set Asc = CreateObject("System.Text.UTF8Encoding")

Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")'MD5CryptoServiceProvider

'Convert the string to a byte array and hash it

bytes = Asc.GetBytes_4(InStr)

bytes = enc.ComputeHash_2((bytes))

outstr = ""

'Convert the byte array to a hex string

For pos = 1 To Lenb(bytes)

outstr = outstr & LCase(Right("0" & Hex(Ascb(Midb(bytes, pos, 1))), 2))

Next

'*************************************************

'* READ ME *

'* *

'* The outstr variable is used to hold the hex value output by the function. *

'* Output represents the label you have defined for your function output parameter. *

'* *

***************************************************

Output = outstr

This script is then executed at runtime when the task step containing the function definition is run.

You can see from this example that a run VB Script function can host any algorithm or code as required.