Thursday, July 07, 2011

Decypt QTP Encrypted Passwords

Here's another code snippet that I've used extensively during my automation career. There are times in which you want to save a password with your test but don't want to expose that password for everyone to see. This can be done by using QTP's encryption utility. The problem here is that the decrypt functionality is built into QTP native objects only like WebEdit, VbEdit, etc. I've had to come up with my own solution to solve the problem of using an encrypted password on non native objects.

My solution is below. It's build off of a little trick that I got from another blog in which you can create an instance of IE that you then can use the WebEdit SetSecure method on. You then have the decrypted password you can use elsewhere in your application.





'''
''' Decrypts an string which has been encrypted using the Crypt.Encrypt() method.
'''

''' Code Source: http://quicktestprofessional.wordpress.com/2008/02/15/54/
''' The Encrypted String To decrypt
''' The specified string in un-encrypted form. Empty string will be returned if a problem was encountered.
Public Function Decrypt(StringToDecrypt)
Dim strRetVal
strRetVal = ""

Dim objIE
'need to launch a NEW instance of IE here...
Set objIE = CreateObject("InternetExplorer.Application")

' specify the IE settings
objIE.Navigate "about:blank"
objIE.Document.Title = "Decrypt"
objIE.ToolBar = False
objIE.Resizable = False
objIE.StatusBar = False
objIE.Width = 200
objIE.Height = 150
' Center the dialog window on the screen
With objIE.Document.ParentWindow.Screen
objIE.Left = (.AvailWidth - objIE.Width ) \ 2
objIE.Top = (.Availheight - objIE.Height) \ 2
End With

' Insert the HTML code for password box
objIE.Document.Body.InnerHTML = "

" & "Decrypt" & "

" _
& "

"

' Make the window visible
objIE.Visible = True

Dim objPasswordBox
Set objPasswordBox = Browser("name:=Decrypt").Page("title:=Decrypt").WebEdit("name:=txtDecrypt")
If Not (objPasswordBox Is Nothing) Then
objPasswordBox.SetSecure StringToDecrypt
strRetVal = objPasswordBox.getROProperty("value")
Browser("name:=Decrypt").Close
Else
Reporter.ReportEvent micFail, "Could not find a WebEdit of type password in the page."
End If

Set objPasswordBox = Nothing
Set objIE = Nothing

Decrypt = strRetVal
End Function

2 comments:

Meir Bar-Tal said...

Great job! I came up independently with a similar solution, but with an invisible browser. You can view it at AdvancedQTP.com.

Meir Bar-Tal said...

It would be nice if you took the courtesy of providing your source to give credit to the original author.