HB_DECRYPT, HB_READINI and GETPVPROFSTRING

HB_DECRYPT, HB_READINI and GETPVPROFSTRING

Postby MarcoBoschi » Tue Oct 06, 2015 3:32 pm

Hi,
I cannot understand this problem.
Please I need explanation.

I store a password created using hb_crypt in a .ini file
the password is "charlybrown" crypted using hb_crypt()
The Key is "12345678"
cPassword := HB_CRYPT( "charlybrown" , "12345678" )
If I read ini files using HB_READINI and than decrypt I obtain "charlybrown" If I use GETPVPROFSTRING not.
The difference between cVar1 and cVar2 is the first character that compromoses the crypted string.


Thanks in advance
Marco

Code: Select all  Expand view

#include "fivewin.ch"

FUNCTION MAIN()
LOCAL cKeyCry := "12345678"

LOCAL cCri1
LOCAL cCri2
LOCAL cVar1
LOCAL cVar2
LOCAL hIniData


hIniData := HB_READINI( "critt.ini" )

cCri1  := hIniData["Main"]["Password"]
cVar1 := HB_DECRYPT( cCri1 , cKeyCry )

cCri2  := GETPVPROFSTRING( "Main" , "Password" , "" , ".\critt.ini" )

cVar2 := HB_DECRYPT( cCri2 , cKeyCry )
? cCri1
? cCri2
? cVar1
? cVar2

RETURN NIL

/*
[Main]
Password=Öó|^4@L×0W
N.B. obviously this string was created with HB_CRYPT( "charlybrown", "12345678" )
*/

 
User avatar
MarcoBoschi
 
Posts: 1018
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: HB_DECRYPT, HB_READINI and GETPVPROFSTRING

Postby Antonio Linares » Thu Oct 08, 2015 8:52 am

Marco,

No idea where the problem may come from, sorry
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41362
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: HB_DECRYPT, HB_READINI and GETPVPROFSTRING

Postby Enrico Maria Giordano » Thu Oct 08, 2015 9:45 am

Marco,

the sample is not complete. Please show a complete sample of the problem.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8356
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: HB_DECRYPT, HB_READINI and GETPVPROFSTRING

Postby MarcoBoschi » Fri Oct 09, 2015 9:27 am

Enrico,
you have to create by hand critt.ini

[Main]
Password=Öó|^4@L×0W

then compile and you can see that there are a problem

Another problem/difference is this

=======prova.ini============
characters accidentallly typed[Numeri]
var1=0000001
========================

#include "Fivewin.ch"

Code: Select all  Expand view
FUNCTION MAIN()
LOCAL cVar1, cVar2
LOCAL hIniData

? memoread( ".\prova.ini" )

cVar1  := GETPVPROFSTRING( "Numeri" , "var1" , "NOT READ" , ".\prova.ini" )

hIniData := HB_ReadIni( ".\prova.ini" )

cVar2  := hIniData["Numeri"]["var1"]

? cVar1   // "inierr"
? cVar2   // "0000001"

RETURN NIL

 


HB_READINI read even although if are present characters accidentally typed before section name
User avatar
MarcoBoschi
 
Posts: 1018
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: HB_DECRYPT, HB_READINI and GETPVPROFSTRING

Postby Enrico Maria Giordano » Fri Oct 09, 2015 10:09 am

Marco,

it looks as a standard Windows API behaviour. It seems that GetPrivateProfileString() API removes leading spaces, that has to be considered the right behaviour, of course.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8356
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: HB_DECRYPT, HB_READINI and GETPVPROFSTRING

Postby MarcoBoschi » Fri Oct 09, 2015 10:11 am

Enrico,
here the complete sample for crypt problem (the first of this post)

Code: Select all  Expand view

#include "fivewin.ch"
#define crlf CHR(13)+CHR(10)



FUNCTION MAIN()
LOCAL cStringIni := ""
LOCAL cKeyCry    := "12345678"

LOCAL cCri1
LOCAL cCri2

LOCAL cNoCri1
LOCAL cNoCri2

LOCAL cVar1
LOCAL cVar2
LOCAL cCrypted  := HB_CRYPT( "charlybrown"  , cKeyCry )

LOCAL hIniData
// create "critta".ini file
MsgInfo( cCrypted , "charlybrown crypted using 12345678 as key" )

cStringIni  := cStringIni + "[Variabili]"                                        + crlf
cStringIni  := cStringIni + "UserId="     + "lucy Van Pelt"                      + crlf
cStringIni  := cStringIni + "Password="   + cCrypted  + crlf

creafile( "critta.ini" , cStringIni )

MsgInfo( memoread( "critta.ini" ) , "content of critta.ini file" )

ferase( "critta.log" )

// I read critta.ini file   using two different functions, hb_readini and  getpvprofstring
hIniData := HB_READINI( "critt.ini" )

cNoCri1 := hIniData[ "Main" ][ "UserId"   ]
cCri1   := hIniData[ "Main" ][ "Password" ]

cVar1   := HB_DECRYPT( cCri1  , cKeyCry )

cNoCri2 := GETPVPROFSTRING( "Main" , "UserId"   , "" , ".\critt.ini" )
cCri2   := GETPVPROFSTRING( "Main" , "Password" , "" , ".\critt.ini" )

cVar2 := HB_DECRYPT( cCri2 , cKeyCry )

logfile( "critta.log" , { "normal variables" }                             )
logfile( "critta.log" , { "hb_readini"      , "cNoCri1" , cNoCri1    } )
logfile( "critta.log" , { "GETPVPROFSTRING" , "cNoCri2" , cNoCri2    } )

logfile( "critta.log" , { "crypted variables" }                            )
logfile( "critta.log" , { "hb_readini"      , "cCri1" , cCri1      , "cCrypted variable" , cCrypted          } )
logfile( "critta.log" , { "GETPVPROFSTRING" , "cri2"  , cCri2      , "cCrypted variable" , cCrypted          } )

logfile( "critta.log" , { "de-crypted variables" }                            )
logfile( "critta.log" , { "hb_readini"      , "cVar1"  , cVar1            } )
logfile( "critta.log" , { "GETPVPROFSTRING" , "cVar2"  , cVar2            } )

SHELLEXECUTE( 0, 0, "critta.log" , 0, 0, 1 )

RETURN NIL

FUNCTION Creafile( cFile, cStringa )
  LOCAL nHandle := FCreate( cFile )
  LOCAL cLeggiTest
  LOCAL lRitorna

  IF FError() <> 0
     RETURN .F.
   ENDIF
   FWrite( nHandle, cStringa, Len(cStringa) )
   FClose( nHandle )

   cLeggiTest := MEMOREAD( cFile )

   lRitorna := ( FError() == 0 ) .AND. ( cStringa = cLeggiTest )

 RETURN lRitorna
 
User avatar
MarcoBoschi
 
Posts: 1018
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: HB_DECRYPT, HB_READINI and GETPVPROFSTRING

Postby Enrico Maria Giordano » Fri Oct 09, 2015 10:21 am

Marco,

the problem is the leading space of the crypted string. You can't store it in an INI file.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8356
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: HB_DECRYPT, HB_READINI and GETPVPROFSTRING

Postby hua » Fri Oct 09, 2015 10:30 am

Marco,
If you still want to save value with leading spaces in an ini, maybe you can try to mime-encode it first.
Code: Select all  Expand view
cStringIni  := cStringIni + "Password="   + cMimeEnc(cCrypted)  + crlf


and later you have to remember to
Code: Select all  Expand view
cVar2 := HB_DECRYPT( cMimeDec(cCri2) , cKeyCry )
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1047
Joined: Fri Oct 28, 2005 2:27 am

Re: HB_DECRYPT, HB_READINI and GETPVPROFSTRING

Postby MarcoBoschi » Sat Oct 10, 2015 4:51 pm

Many thanks!
User avatar
MarcoBoschi
 
Posts: 1018
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot], Willi Quintana and 34 guests