Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Encrypt / Decrypt Data in a ini file


  • Please log in to reply
2 replies to this topic

#1 pinkfoyd

pinkfoyd

    Wayfarer

  • Active Members
  • Pip
  • 79 posts

Posted 28 March 2011 - 01:15 PM

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, 28 March 2011 - 01:17 PM.








#2 pinkfoyd

pinkfoyd

    Wayfarer

  • Active Members
  • Pip
  • 79 posts

Posted 28 March 2011 - 01:36 PM

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, 28 March 2011 - 01:56 PM.


#3 wolf9228

wolf9228

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 496 posts

Posted 28 March 2011 - 02:28 PM

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
AutoIt         
#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, 28 March 2011 - 02:57 PM.

  • triodz likes this

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

 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users