Encrypt/Decrypt File

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
newuser
Pro Scripter
Posts: 64
Joined: Tue Jun 11, 2013 4:53 pm

Encrypt/Decrypt File

Post by newuser » Thu May 08, 2014 6:49 pm

Hi, anyone have a simple example of a encrypt file and decrypt file example using MS(not vbscript)?

Basically, I want to encrypt a file containing settings, so that when I need to read the settings, I just decrypt the file either in disk or in memory(I wonder if this is even possible?).
Thanks.

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

Re: Encrypt/Decrypt File

Post by Marcus Tettmar » Thu May 08, 2014 7:26 pm

Have a look at crypt in the help file. Use ReadFile and WriteLn to read the encrypted data and write it back.

E.g. Let's modify the help file example with code to write to and read from file:

Code: Select all

//Delete the text file we're writing to in case it already exists
DeleteFile>%temp_dir%file.txt

//Create a password
Let>mypassword=this is a secret

//Some text to encrypt
Let>strText=the quick brown fox jumped over the lazy dog

//encrypt the string
crypt>mypassword,strText,encrypted_data
 
//as encrypted data is binary use Base64 to encode it to a string
Base64>encrypted_data,ENCODE,encoded_encrypted_data

//Set WLN_NOCRLF=1 to prevent writing an extra carriage return / line feed.
Let>WLN_NOCRLF=1

//write it to file
WriteLn>%temp_dir%file.txt,result,encoded_encrypted_data
..
..
 
//decode and decrypt
ReadFile>%temp_dir%file.txt,encoded_encrypted_data2
Base64>encoded_encrypted_data2,DECODE,encrypted_data2
crypt>mypassword,encrypted_data2,strText2

MDL>strText2
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

newuser
Pro Scripter
Posts: 64
Joined: Tue Jun 11, 2013 4:53 pm

Re: Encrypt/Decrypt File

Post by newuser » Fri May 09, 2014 5:17 am

Thanks Marcus, I modify the code and it works as below

Code: Select all

[Setting]
Parameter1=test.txt
Test1=C:\Program Files\Test\notepad.exe
Test2=C:\Program Files\Test\calc.exe
EncryptFile

Code: Select all

//Create a password
Let>mypassword=this is a secret

//Some text to encrypt
ReadFile>c:\file.txt,strText

//encrypt the string
crypt>mypassword,strText,encrypted_data
 
//as encrypted data is binary use Base64 to encode it to a string
Base64>encrypted_data,ENCODE,encoded_encrypted_data

//Delete the text file we're writing to in case it already exists
DeleteFile>C:\file.txt

//Set WLN_NOCRLF=1 to prevent writing an extra carriage return / line feed.
Let>WLN_NOCRLF=1
//write it to file
WriteLn>C:\file.txt,result,encoded_encrypted_data
DecryptFile

Code: Select all

//Create a password
Let>mypassword=this is a secret
 
//decode and decrypt
ReadFile>C:\file.txt,encoded_encrypted_data2
Base64>encoded_encrypted_data2,DECODE,encrypted_data2
crypt>mypassword,encrypted_data2,strText2
//MDL>strText2

//Delete the text file we're writing to in case it already exists
DeleteFile>C:\file.txt

//Set WLN_NOCRLF=1 to prevent writing an extra carriage return / line feed.
Let>WLN_NOCRLF=1
//write it to file
WriteLn>C:\file.txt,result,strText2
Instead of decrypting the file and write into disk, and use ReadIniFile to read the settings, can I decrypt it in memory and read the settings like ReadIniFile in memory?
A simple example of it please?

Thanks

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

Re: Encrypt/Decrypt File

Post by Marcus Tettmar » Fri May 09, 2014 10:19 am

You can't use ReadIniFile without it already being a file. So you'd have to parse the string a different way. I'd use RegEx.

Assuming you are using a simple INI file format where you have name=value pairs on each line then you could use this:

Let's assume the "INI" file looks like this:

Code: Select all

color=blue
age=18
size=10
Then the following will decrypt and uses the GetVar subroutine to get the variables you want.

Code: Select all

//Create a password
Let>mypassword=this is a secret
 
//decode and decrypt
ReadFile>c:\temp\file.txt,encoded_encrypted_data2
Base64>encoded_encrypted_data2,DECODE,encrypted_data2
crypt>mypassword,encrypted_data2,strText2

//get color value
GoSub>GetVar,color

//get age  value
GoSub>GetVar,age

SRT>GetVar
  RegEx>(?<=%GetVar_Var_1%=).*?(?=\n),%strText2%%CRLF%,0,matches,nm,0
  Let>%GetVar_Var_1%=matches_1
END>GetVar
color and age become variables set to their associated values. Step through the code to see what I mean.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

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