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.
Encrypt/Decrypt File
Moderators: Dorian (MJT support), JRL
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Re: Encrypt/Decrypt File
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:
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Re: Encrypt/Decrypt File
Thanks Marcus, I modify the code and it works as below
EncryptFile
DecryptFile
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
Code: Select all
[Setting]
Parameter1=test.txt
Test1=C:\Program Files\Test\notepad.exe
Test2=C:\Program Files\Test\calc.exe
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
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
A simple example of it please?
Thanks
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Re: Encrypt/Decrypt File
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:
Then the following will decrypt and uses the GetVar subroutine to get the variables you want.
color and age become variables set to their associated values. Step through the code to see what I mean.
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
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
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?