Page 1 of 1

hb_crypt and hb_decrypt problem

Posted: Wed Aug 11, 2021 1:12 am
by ricbarraes
Hey everybody!

I'm encrypting some data on my application with a random salt and I found out a problem like this:

Code: Select all | Expand

hb_crypt("@Selva123","LVLVDTWRQHVYCDTP")

hb_decrypt(hb_crypt("@Selva123","LVLVDTWRQHVYCDTP"),"LVLVDTWRQHVYCDTP")
 


it encrypts the data but it was not able to decrypt using the same key...

that's how I'm generating the key:

Code: Select all | Expand

FOR N=1 TO 16
      nChar:=HB_RandomInt( 65, 90 )      
      cSalt+=Chr(nChar)
   NEXT N


Does anybody know what is wrong?

Re: hb_crypt and hb_decrypt problem

Posted: Wed Aug 11, 2021 4:18 am
by anserkk
I am getting the desired output as per your sample ie when I decrypt the result is @Selva123

Code: Select all | Expand

#Include "Fivewin.ch"
Function Main()

    ? hb_crypt("@Selva123","LVLVDTWRQHVYCDTP")

    ? hb_decrypt(hb_crypt("@Selva123","LVLVDTWRQHVYCDTP"),"LVLVDTWRQHVYCDTP")

Return NIL

If you are writing the encrypted value to some file and then later trying to decrypt the value then please try StrToHex() and HexToStr()

Re: hb_crypt and hb_decrypt problem

Posted: Wed Aug 11, 2021 12:35 pm
by ricbarraes
anserkk wrote:I am getting the desired output as per your sample ie when I decrypt the result is @Selva123

Code: Select all | Expand

#Include "Fivewin.ch"
Function Main()

    ? hb_crypt("@Selva123","LVLVDTWRQHVYCDTP")

    ? hb_decrypt(hb_crypt("@Selva123","LVLVDTWRQHVYCDTP"),"LVLVDTWRQHVYCDTP")

Return NIL

If you are writing the encrypted value to some file and then later trying to decrypt the value then please try StrToHex() and HexToStr()
Thank you my friend!

In fact I’m saving it in a MariaDB table as a LONGBLOB column. Do I need to use strtohex and hextostr to store it properly? And which one do I have to use to save and to retrieve the content?


Enviado do meu iPhone usando Tapatalk

Re: hb_crypt and hb_decrypt problem

Posted: Thu Aug 12, 2021 1:42 am
by nageswaragunupudi
In case you are using built-in FWH MariaDB/MySql library, you need not worry about it. The library automatically takes care of the
conversions.

Example Usage:

Code: Select all | Expand


oRs:photo := MEMOREAD( "photo.jpg" )
oRs:Save()
 


Without opening the table as rowset:

Code: Select all | Expand


oCn:Insert( cTable, "NAME,PHOTO", { "Albert", MEMOREAD( "albert.jpg" ) } )
// OR
oCn:Update( cTable, "PHOTO", { MEMOREAD( "newphoto.jpg" ) }, "ID=230" )
 


If you use any other library or want to write your own SQL statements to insert or update a table use
"0x" + STRTOHEX( MEMOREAD( cFile ) ), without enclosing in quotes.

You can also use FW_ValToSQL( cBinaryText, .t., "MYSQL" )
Note: The conversion is different for different databases. FW_ValToSQL takes care of this.