Dialog to encrypt a text string

General Macro Scheduler discussion

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Dialog to encrypt a text string

Post by JRL » Mon Apr 03, 2006 9:45 pm

Hello All,

I had a need to encrypt passwords going into an .INI file stored on the network and available to everyone. I was doing 120 of them so I wrote this to make the task less tedious. It is not commented but I thought I'd put it out here for anyone else who might be able to use it. It also consolidates sample code for various dialog techniques that have previously been discussed separately on the forum. This script requires version 8.0 or greater to work. It could be modified to work in version 7.4 by removing or not using the encryption levels.

Basically, it works like this. After firing up the script... enter an encryption key, (any alpha numeric string up to 4 characters.) Then start typing the text to be encrypted. You should see the encrypted string show up beneath the label "Encryption". You can select from any of the 3 supported encryption strengths, 32, 64 or 96 bit. You can select or deselect use of Visual Basic's escape function. (For information about the VB escape function see the help for Crypt>.) When you like what you've typed and selected, click the "Update" button. This puts the encryted text onto the clipboard. Do not close the window before pasting the text or you will lose it.

Remember that when you decrypt, you must use the same settings for the three parameters used to encrypt.

-- The encryption key must be the same.
-- The encryption level must be the same.
-- If you used the VB escape function to create the encrypted string you must use the VB unescape function to decrypt the string.

If you try it and you like it, let me know.
If you try it and there's a problem, let me know.
If you alter and improve it, post your new code.

I thought about adding a checkbox to hide the text being typed and require the text to be typed a second time to comfirm before processing. But I don't need that functionality and it seemed like more work than I wanted to do.

Hope someone finds this useful,
Dick



VBSTART
VBEND
Let>VAREXPLICIT=1

Let>encdata=%space%
Let>cbdata=%space%
GCB>memories
Dialog>Dialog1
Caption=Encrypt Password into Memory
Width=445
Height=338
Top=CENTER
Left=CENTER
Close=0
Label=Key (4 Character Limit),16,112
Label=Text to Encrypt,240,112
Label=%encdata%,16,192
Label=Encryption,48,168
Label=Current Clipboard,48,216
Label=%cbdata%,16,240
RadioGroup=msRadioGroup1,Encryption Level,56,8,105,81,32 Bit%CRLF%64 Bit%CRLF%96 Bit,-1
CheckBox=msCheckBox1,Use VBScript Esc Function,224,24,153,True
//CheckBox=msCheckBox2,Hide password,224,56,97,False
Edit=msEdit1,16,128,41,
Edit=msEdit2,216,128,201,
Button=UpDate,32,272,75,25,3
Button=Cancel,328,272,75,25,2
Default=UpDate
EndDialog>Dialog1

Show>Dialog1

Let>k=0
Label>ActionLoop
GetDialogAction>Dialog1,r
IF>%Dialog1.msRadioGroup1.ItemIndex%=0
Let>CRYPT_LEVEL=1
EndIf
IF>%Dialog1.msRadioGroup1.ItemIndex%=1
Let>CRYPT_LEVEL=2
EndIf
IF>%Dialog1.msRadioGroup1.ItemIndex%=2
Let>CRYPT_LEVEL=3
EndIf
Len>%Dialog1.msEdit1%,keylen
If>%keylen%>4
Midstr>%Dialog1.msEdit1%,1,4,Dialog1.msEdit1
ResetDialogAction>Dialog1
Press End
Goto>ActionLoop
EndIf
Len>%Dialog1.msEdit2%,chars
If>{%k%%chars%}
If>%Dialog1.msCheckBox1%=True
Let>k=%chars%
Crypt>%Dialog1.msEdit1%,%Dialog1.msEdit2%,encdata
StringReplace>%encdata%,","",encdata
VBEval>Escape("%encdata%"),encdata
Let>Dialog1.msLabel2=%encdata%
ResetDialogAction>Dialog1
Else
Let>k=%chars%
Crypt>%Dialog1.msEdit1%,%Dialog1.msEdit2%,encdata
Let>Dialog1.msLabel2=%encdata%
ResetDialogAction>Dialog1
EndIf
EndIf
Wait>0.001
if>%r%=3,Update
if>%r%=2,exit
Goto>ActionLoop


SRT>Update
Let>k=%k%-1
If>{%k%%chars%}
If>%Dialog1.msCheckBox1%=True
Let>k=%chars%
Crypt>%Dialog1.msEdit1%,%Dialog1.msEdit2%,encdata
StringReplace>%encdata%,","",encdata
VBEval>Escape("%encdata%"),encdata
Let>Dialog1.msLabel2=%encdata%
ResetDialogAction>Dialog1
Else
Let>k=%chars%
Crypt>%Dialog1.msEdit1%,%Dialog1.msEdit2%,encdata
Let>Dialog1.msLabel2=%encdata%
ResetDialogAction>Dialog1
EndIf
EndIf
Put>%encdata%
Let>Dialog1.msLabel5=%encdata%
ResetDialogAction>Dialog1
END>Update

Label>exit

Put>%memories%
Last edited by JRL on Wed Apr 05, 2006 4:38 pm, edited 1 time in total.

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Tue Apr 04, 2006 12:17 am

Nice contribution Dick.

I will try to look at this over the next weekend.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Wed Apr 05, 2006 2:11 am

Very nice, thanks for posting that. :)
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Apr 05, 2006 4:37 pm

Just realized this morning that the four character limit I imposed on the encryption key in the script was limiting the 64 and 96 bit encryptions. The 32 bit key has a 4 character limit, the 64 bit key has an 8 character limit and the 96 bit key has a 12 character limit. That is now reflected in the new script I've posted below.

Made another discovery that I don't have the skills to deal with. The Visual Basic escape function fails if one of the characters in the text encrypted by the Crypt> function is either a line feed (ascii character 10) or a return (ascii character 13). In that situation a message pops up that says:

=============================
Microsoft VBScript compilation error :1033
Unterminated string constant.
Line 3, Column xxx

[OK] [Abort]
=============================

If you pick OK the process continues but you must either turn off the VBScript escape function or change the key or the text. If one of these is not changed the error re-occurs. This is a problem since the text is probably a given entity, the key perhaps is also being used for a particular reason and the encryption with a line feed or carriage return buried in it is not going to be accessable from any text based application which is the reason for using the VBScript escape function.

Any Visual Basic experts out there have an answer for this?

Or perhaps my assessment is incorrect.

In either case, I'd like to hear from you.

Later,
Dick

Reworked script, enabling longer key strings for higher encryption.

Code: Select all

VBSTART
VBEND
Let>VAREXPLICIT=1
Let>CRYPT_LEVEL=1
Let>keylim=4
Let>encdata=%space%
Let>cbdata=%space%
GCB>memories
Dialog>Dialog1
   Caption=Encrypt Text into Memory
   Width=445
   Height=338
   Top=CENTER
   Left=CENTER
   Close=0
   Label=Key (4 Character Limit),16,112
   Label=Text to Encrypt,240,112
   Label=%encdata%,16,192
   Label=Encryption,48,168
   Label=Current Clipboard,48,216
   Label=%cbdata%,16,240
   RadioGroup=msRadioGroup1,Encryption Level,56,8,105,81,32 Bit%CRLF%64 Bit%CRLF%96 Bit,-1
   CheckBox=msCheckBox1,Use VBScript Esc Function,224,24,153,True
   //CheckBox=msCheckBox2,Hide password,224,56,97,False
   Edit=msEdit1,16,128,120,
   Edit=msEdit2,216,128,201,
   Button=UpDate,32,272,75,25,3
   Button=Cancel,328,272,75,25,2
   Default=UpDate
EndDialog>Dialog1

Show>Dialog1

Let>k=0
Let>kk=0
Label>ActionLoop
GetDialogAction>Dialog1,r
IF>{(%Dialog1.msRadioGroup1.ItemIndex%=0)and(%CRYPT_LEVEL%<1>CRYPT_LEVEL=1
  Let>Dialog1.msLabel0=Key (4 Character Limit)
  Let>keylim=4
  Gosub>Update
EndIf
IF>{(%Dialog1.msRadioGroup1.ItemIndex%=1)and(%CRYPT_LEVEL%<2>CRYPT_LEVEL=2
  Let>Dialog1.msLabel0=Key (8 Character Limit)
  Let>keylim=8
  Gosub>Update
EndIf
IF>{(%Dialog1.msRadioGroup1.ItemIndex%=2)and(%CRYPT_LEVEL%<3>CRYPT_LEVEL=3
  Let>Dialog1.msLabel0=Key (12 Character Limit)
  Let>keylim=12
  Gosub>Update
EndIf
Len>%Dialog1.msEdit1%,keylen
If>%keylen%>%keylim%
  Midstr>%Dialog1.msEdit1%,1,%keylim%,Dialog1.msEdit1
  ResetDialogAction>Dialog1
  Press End
  Goto>ActionLoop
EndIf
Len>%Dialog1.msEdit2%,chars
If>{(%k%<>%chars%)or(%kk%<keylen>%Dialog1.msCheckBox1%=True
    Let>k=%chars%
	Let>kk=%keylen%
    Crypt>%Dialog1.msEdit1%,%Dialog1.msEdit2%,encdata
	StringReplace>%encdata%,","",encdata
    VBEval>Escape("%encdata%"),encdata
    Let>Dialog1.msLabel2=%encdata%
    ResetDialogAction>Dialog1
  Else
    Let>k=%chars%
	Let>kk=%keylen%
    Crypt>%Dialog1.msEdit1%,%Dialog1.msEdit2%,encdata
	Let>Dialog1.msLabel2=%encdata%
    ResetDialogAction>Dialog1
  EndIf
EndIf
Wait>0.001
if>%r%=3,Update
if>%r%=2,exit
Goto>ActionLoop


SRT>Update
//Write to memory
If>{%k%<0>k=%k%-1
  If>{%k%<chars>%Dialog1.msCheckBox1%=True
      Let>k=%chars%
      Crypt>%Dialog1.msEdit1%,%Dialog1.msEdit2%,encdata
      StringReplace>%encdata%,","",encdata
      VBEval>Escape("%encdata%"),encdata
      Let>Dialog1.msLabel2=%encdata%
      ResetDialogAction>Dialog1
    Else
      Let>k=%chars%
      Crypt>%Dialog1.msEdit1%,%Dialog1.msEdit2%,encdata
	  Let>Dialog1.msLabel2=%encdata%
      ResetDialogAction>Dialog1
    EndIf
  EndIf
EndIf
Put>%encdata%
Let>Dialog1.msLabel5=%encdata%
ResetDialogAction>Dialog1
END>Update

Label>exit

Put>%memories%


Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts