Jump to content

Encrypt / Decrypt Data in a ini file


Recommended Posts

Hi all,

I successfully encode and decode a string, but i don't succeed with a ini file.

What i want :

Read a data in INI file, encode it, then replace it in the ini_file

After that, i want to read the ini file and decrypt the data

But this does'nt work, i think it's a problem of format (Binary/String ) but i don't succeed

$password     = "123456"

$adresse_ftp          = IniRead($path_directive,"FTP","url","[NOT_FOUND]")
$adresse_ftp_crypted  =_Crypt_EncryptData($adresse_ftp  ,$password,$CALG_AES_256)

IniWrite($fichier_ini ,"FTP","url",$adresse_ftp_crypted)


$adresse_ftp    = IniRead($path_directive,"FTP","url","[NOT_FOUND]")

_FileWriteLog($log_file,$adresse_ftp)

if StringLeft($adresse_ftp,2) = "0x" then $adresse_ftp = BinaryToString(_Crypt_DecryptData($adresse_ftp , $password, $CALG_AES_256))

_FileWriteLog($log_file,$adresse_ftp)

This give me this result ( before decrypt / after decrypt ):

2011-03-28 15:04:46 : 0x0AE9B9CBDEE341E4313D442CC8CBF58A
2011-03-28 15:04:46 : ÿÿÿÿ

Again if i do the same code, but without passthrought an ini file, all works...

Any help would be apprecied...

Edited by pinkfoyd
Link to comment
Share on other sites

hum, lol

while i am trying to write you a better example, it's work ! trying to check what i m wrong in my first code

EDIT : SHAME ON ME : somewhere in my script i delete the variable wich contain the encryption key, so stupid... :)

Edited by pinkfoyd
Link to comment
Share on other sites

Hi all,

I successfully encode and decode a string, but i don't succeed with a ini file.

What i want :

Read a data in INI file, encode it, then replace it in the ini_file

After that, i want to read the ini file and decrypt the data

But this does'nt work, i think it's a problem of format (Binary/String ) but i don't succeed

$password     = "123456"

$adresse_ftp          = IniRead($path_directive,"FTP","url","[NOT_FOUND]")
$adresse_ftp_crypted  =_Crypt_EncryptData($adresse_ftp  ,$password,$CALG_AES_256)

IniWrite($fichier_ini ,"FTP","url",$adresse_ftp_crypted)


$adresse_ftp    = IniRead($path_directive,"FTP","url","[NOT_FOUND]")

_FileWriteLog($log_file,$adresse_ftp)

if StringLeft($adresse_ftp,2) = "0x" then $adresse_ftp = BinaryToString(_Crypt_DecryptData($adresse_ftp , $password, $CALG_AES_256))

_FileWriteLog($log_file,$adresse_ftp)

This give me this result ( before decrypt / after decrypt ):

2011-03-28 15:04:46 : 0x0AE9B9CBDEE341E4313D442CC8CBF58A
2011-03-28 15:04:46 : ÿÿÿÿ

Again if i do the same code, but without passthrought an ini file, all works...

Any help would be apprecied...

Try this code

#include <Crypt.au3>

Ini_Write("Password","Filename.ini","Section","Key","Value")

$Value = Ini_Read("Password","Filename.ini","Section","Key")
if Not @error Then _
MsgBox(0,"MSG",$Value)

Func Ini_Write($Password,$Filename,$Section,$Key,$Value)
_Crypt_Startup()
if @error Then Return SetError(1,@error,False)
$hKey =_Crypt_DeriveKey($Password,$CALG_RC4)
if @error Then Return SetError(2,@error,False)
$ByteStruct =  DllStructCreate("BYTE[" & StringLen($Value) & "]")
DllStructSetData($ByteStruct,1,$Value)
$StrByte = String(DllStructGetData($ByteStruct,1))
$EncryptedValue = _Crypt_EncryptData($StrByte,$hKey,$CALG_USERKEY)
if @error Then Return SetError(3,@error,False)
IniWrite($Filename,$Section,$Key,$EncryptedValue)
if @error Then Return SetError(4,@error,False)
_Crypt_DestroyKey($hKey)
_Crypt_Shutdown()
Return SetError(0,0,True)
EndFunc


Func Ini_Read($Password,$Filename,$Section,$Key,$Default = "NotFound")
$EncryptedValue = IniRead($Filename,$Section,$Key,$Default)
if $EncryptedValue == $Default Then Return SetError(1,0,$Default)
_Crypt_Startup()
if @error Then Return SetError(2,@error,$Default)
$hKey =_Crypt_DeriveKey($Password,$CALG_RC4)
if @error Then Return SetError(3,@error,$Default)
$Binary = _Crypt_DecryptData($EncryptedValue,$hKey,$CALG_USERKEY)
if @error Then Return SetError(4,@error,$Default)
$StrByte = BinaryToString($Binary)
if @error Then Return SetError(5,@error,$Default)
$ByteStruct =  DllStructCreate("BYTE[" & BinaryLen($StrByte) & "]")
DllStructSetData($ByteStruct,1,$StrByte)
$CharStruct = _
DllStructCreate("CHAR[" & DllStructGetSize($ByteStruct) & "]",DllStructGetPtr($ByteStruct))
_Crypt_DestroyKey($hKey)
_Crypt_Shutdown()
Return SetError(0,0,DllStructGetData($CharStruct,1))
EndFunc
Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...