Complex Crypt function without VBscript

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
Snickers
Macro Veteran
Posts: 151
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Complex Crypt function without VBscript

Post by Snickers » Mon Aug 06, 2007 10:47 pm

The last few lines of my macro do the following things:

1. takes all of my variables and puts them into a single variable.
2. encrypts the variable
3. writes the variable to a textfile.

Code: Select all

Random>9999999,textfile
let>textfile=%textfile%+1
let>ID=username

Let>datafile=This variable is full of variables and text mixed %raining% @ %dogX%,%dogY%. It has a word count of approximately 308 words and variables mixed.

Crypt>abc,datafile,cryptvault
wait>3
WriteLn>C:\Temp\%ID%%textfile%.txt,r,cryptvault
I am unable to properly decrypt the textfile. It is usually cut short displaying only a small portion of the message.


Is it possible to encrypt a lot of variables, write it to a text file, and then convert the encrypted file into a readable text document without using VBscript?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Aug 07, 2007 6:12 am

You can use the secret undocumented Base64 function:

Crypt>abc,datafile,cryptvault
Base64>cryptvault,ENCODE,cryptvault
WriteLn>C:\Temp\%ID%%textfile%.txt,r,cryptvault

To decode:

ReadFile>textfile,cryptdata
Base64>cryptdata,DECODE,todecrypt
Crypt>abc,todecrypt,plaintext

Base64 was added recently but is as yet undocumented. It will be made official in v10. You'll notice it is not coloured by the syntax highlighter etc. Consider it's use a bonus extra.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Snickers
Macro Veteran
Posts: 151
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Post by Snickers » Tue Aug 07, 2007 12:29 pm

I am still using version 7.4, and it isn't working with my7.4 version. It seems to ignore the base64>.
Should I wait until version 10 comes out to purchase the upgraded version?
Last edited by Snickers on Tue Aug 07, 2007 12:34 pm, edited 1 time in total.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Aug 07, 2007 12:31 pm

It is up to you whether you upgrade to 9.1 now, or wait. You can always purchase upgrade protection to ensure you get the next version.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Snickers
Macro Veteran
Posts: 151
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Post by Snickers » Tue Aug 07, 2007 1:02 pm

So are you saying that the newest version, 9.1, will allow me to use this feature?

Here is what I am doing with my current version and it is not working:

I made two scripts to test this feature:

First script I named Crypt 1:

Code: Select all

Let>a_1=123321123321
let>b=789987789987
let>c=566556655665
Let>datafile=This is a test with string, characters, and variables mixed %a_1%,%b% @ %c%.

Crypt>abc,datafile,cryptvault
Base64>cryptvault,ENCODE,cryptvault
WriteLn>C:\Temp\text.txt,r,cryptvault

The second script I named Crypt 2:

Code: Select all

ReadFile>c:\Temp\text.txt,cryptdata
Base64>cryptdata,DECODE,todecrypt
Crypt>abc,todecrypt,plaintext

WriteLn>c:\temp\text2.txt,result,%plaintext%

The user will run my entire calibration script, then that script will output a textfile that the user sends to me.

I use my Crypt 2 script to decrypt what they sent to me and create a clean readable textfile.

Is this possible with version 9.1+?

Snickers
Macro Veteran
Posts: 151
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Post by Snickers » Tue Aug 07, 2007 1:24 pm

Alright, if that feature is there or on the way, I've gone ahead with an upgrade purchase of the 9.1 with the 2 years upgrade protection.

Snickers
Macro Veteran
Posts: 151
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Base64ToStr: illegal input length!

Post by Snickers » Tue Aug 07, 2007 4:03 pm

This is my encrypt script:

Code: Select all

Random>9999999,textfile
let>textfile=%textfile%+1
let>ID=testcryption
Let>open_yes=123123
let>topenX=yadayada
let>topenY=blahblah

Let>datafile=The  color %open_yes%  is at %topenX%,%topenY%.
Crypt>abc,datafile,cryptvault
Base64>cryptvault,ENCODE,cryptvault
WriteLn>c:\Temp\%ID%%textfile%.txt,r,cryptvault


This is my decrypt script (i have to manually type in the number after testcryption to match the file name) I will automate this process later:

Code: Select all

Readfile>c:\temp\testcryption1089514.txt,cryptdata
messageModal>cryptdata

//the following line generates an error message:Base64ToStr: illegal input length!
Base64>cryptdata,DECODE,todecrypt

messagemodal>todecrypt
Crypt>abc,todecrypt,plaintext

writeln>c:\Temp\text2.txt,result,%plaintext%
Is this normal?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Aug 07, 2007 4:25 pm

You are trying to decode a base 64 string but the string is not base64 encoded. You need to base64 ENCODE the string first BEFORE writing it to the file. Then next time you read it from the file you can base64 DECODE it.

If you try to base64 decode something that isn't base 64 you will get that error.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Aug 07, 2007 4:30 pm

Sorry, ignore my last post, I see the real problem.

ReadFile is returning the CRLF characters written with the WriteLn command. This is confusing it. Remove them before the DECODE with StringReplace:

Readfile>%TEMP_DIR%%ID%%textfile%.txt,cryptdata
StringReplace>cryptdata,CRLF,,cryptdata
Base64>cryptdata,DECODE,todecrypt
MessageModal>todecrypt
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Snickers
Macro Veteran
Posts: 151
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Post by Snickers » Tue Aug 07, 2007 6:48 pm

I am now running Macro Scheduler Pro 9.1.03 .

The following code is what I am using to test the crypt function. It is not working.

Code: Select all

Random>9999999,textfile
let>textfile=%textfile%+1
let>ID=testcryption
Let>open_yes=123123
let>topenX=yadayada
let>topenY=blahblah

Let>datafile=The  color %open_yes%  is at %topenX%,%topenY%.

Crypt>abc,datafile,cryptvault
Base64>cryptvault,ENCODE,cryptvault
WriteLn>%TEMP_DIR%%ID%%textfile%.txt,r,cryptvault


//wait 3 seconds to ensure text file has been created
wait>3

//readfile, decode, and store in variable
Readfile>%TEMP_DIR%%ID%%textfile%.txt,cryptdata
StringReplace>cryptdata,CRLF,,cryptdata
Base64>cryptdata,DECODE,todecrypt
MessageModal>todecrypt

The final MessageModal> displays encrypted data: ÃÂ

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Aug 07, 2007 7:03 pm

You've forgotten to deCRYPT it! Your code should be:

Random>9999999,textfile
let>textfile=%textfile%+1
let>ID=testcryption
Let>open_yes=123123
let>topenX=yadayada
let>topenY=blahblah

Let>datafile=The color %open_yes% is at %topenX%,%topenY%.

Crypt>abc,datafile,cryptvault
Base64>cryptvault,ENCODE,cryptvault
WriteLn>%TEMP_DIR%%ID%%textfile%.txt,r,cryptvault


//wait 3 seconds to ensure text file has been created
wait>3

//readfile, decode, and store in variable
Readfile>%TEMP_DIR%%ID%%textfile%.txt,cryptdata
StringReplace>cryptdata,CRLF,,cryptdata
Base64>cryptdata,DECODE,todecrypt
//Decrypt it
Crypt>abc,todecrypt,plaintext


MessageModal>plaintext
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

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

Post by JRL » Tue Aug 07, 2007 7:09 pm

With no documentation I don't know what base64> is supposed to accomplish but the following works for me without using base64>.

Code: Select all

Random>9999999,textfile
let>textfile=%textfile%+1
let>ID=testcryption
Let>open_yes=123123
let>topenX=yadayada
let>topenY=blahblah

Let>datafile=The  color %open_yes%  is at %topenX%,%topenY%.

Crypt>abc,datafile,cryptvault
//Base64>cryptvault,ENCODE,cryptvault
//Added the following line to prevent writeln from adding %CRLF%
Let>WLN_NOCRLF=1
WriteLn>%TEMP_DIR%%ID%%textfile%.txt,r,cryptvault


//wait 3 seconds to ensure text file has been created
wait>3

//readfile, decode, and store in variable
Readfile>%TEMP_DIR%%ID%%textfile%.txt,cryptdata
//Added the following line to decrypt the encrypted text
Crypt>abc,cryptdata,cryptval
//StringReplace>cryptdata,CRLF,,cryptdata
//Base64>cryptdata,DECODE,todecrypt
MessageModal>cryptval

Snickers
Macro Veteran
Posts: 151
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Post by Snickers » Tue Aug 07, 2007 7:10 pm

I was just about to send you a message telling you what was happening. Then I noticed you had replied. Awesome.

That is beautiful. It looks as though it is crypting it, and then base64 crypting it. then it de-base64crypts it and decrypts it into a textfile again.

This is great. Works perfectly. Thank you

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Aug 07, 2007 7:13 pm

Base 64:
http://en.wikipedia.org/wiki/Base64

A useful way to encode binary data to plain ascii

Depending on what is being encrypted, Crypt can end up with binary characters. This can be problematic if you want to write that crypted data to a text file and read it back again - the effect often being that data is missing.

The solution is to convert the crypted string to an ascii format before writing it to the text file (and decoding it before decrypting it). Base 64 is an ideal way to do this.

So we do:

//Encrypt, encode, store
Crypt>secret,data,crypted
Base64>crypted,ENCODE,encoded
WriteLn>file,r,encoded

//Read, decode, decrypt
ReadFile>file,encoded
StringReplace>encoded,CRLF,,encoded
Base64>encoded,DECODE,decoded
Crypt>secret,decoded,data
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

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

Post by jpuziano » Fri Aug 31, 2007 10:24 pm

mtettmar wrote:Base64 was added recently but is as yet undocumented. It will be made official in v10. You'll notice it is not coloured by the syntax highlighter etc. Consider it's use a bonus extra.
I love it when secret/undocumented functionality becomes not-so-secret and appearing now at version 9.1.04, that's a whole version level ahead of schedule... nice.

If anyone reading this hasn't yet seen JRL's excellant example script using Base64> to create animation in a Dialog, check this out, you'll be glad you did: Base64 sample

Make you wonder what else might be lurking under the hood, doesn't it?

Any hints Marcus?
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 - :-)

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