FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour hb_crypt and hb_decrypt problem
Posts: 55
Joined: Tue Jun 30, 2015 02:26 AM
hb_crypt and hb_decrypt problem
Posted: Wed Aug 11, 2021 01:12 AM
Hey everybody!

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

Code (fw): Select all Collapse
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 (fw): Select all Collapse
FOR N=1 TO 16
      nChar:=HB_RandomInt( 65, 90 )      
      cSalt+=Chr(nChar)
   NEXT N


Does anybody know what is wrong?
Sds,
Ricardo Arraes
ricardo@vfatec.com.br
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: hb_crypt and hb_decrypt problem
Posted: Wed Aug 11, 2021 04:18 AM
I am getting the desired output as per your sample ie when I decrypt the result is @Selva123
Code (fw): Select all Collapse
#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()
Posts: 55
Joined: Tue Jun 30, 2015 02:26 AM
Re: hb_crypt and hb_decrypt problem
Posted: Wed Aug 11, 2021 12:35 PM
anserkk wrote:I am getting the desired output as per your sample ie when I decrypt the result is @Selva123
Code (fw): Select all Collapse
#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
Sds,
Ricardo Arraes
ricardo@vfatec.com.br
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: hb_crypt and hb_decrypt problem
Posted: Thu Aug 12, 2021 01:42 AM
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 (fw): Select all Collapse
oRs:photo := MEMOREAD( "photo.jpg" )
oRs:Save()


Without opening the table as rowset:
Code (fw): Select all Collapse
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.
Regards



G. N. Rao.

Hyderabad, India

Continue the discussion