Page 1 of 1

Encryption error! (SOLVED)

Posted: Mon Jan 23, 2012 2:13 pm
by tiaofw
Good day!

Intermittently I get an error in the encryption / decryption of a given string here on my system.

Here the piece of code:

Code: Select all


cChave: = 'TI040505' / / fictional

retorno_cbx: = 'UR010801752' / / actual
seriec: = 'UR010801752' / / actual

cChave: = ALLTRIM (cChave)

replace serie with space(len(serie))
replace seriec with space(len(seriec))
dbcommit ()

replace serie with ALLTRIM(retorno_cbx)
replace seriec with Encrypt(ALLTRIM(retorno_cbx), cChave)

retorno_cbx: = Decrypt(ALLTRIM(seriec), cChave))

 
Decryption is done when the string is returned a string that is not the same as it was recorded.

Is there any solution for this?


Thank you!

Re: Encryption error!

Posted: Fri Jan 27, 2012 8:40 am
by MarcoBoschi
Have you tried these other functions?

HB_CRYPT( cBufferIn, cKey ) )

HB_DECRYPT( cBufferIn, cKey ) )

maybe you can fix...

Re: Encryption error!

Posted: Fri Jan 27, 2012 10:17 am
by Antonio Linares
Try this and check if it works fine for you:

MsgInfo( Decrypt( Encrypt( "Hello world", "password" ), "password" ) )

Here it is working fine and shows "Hello world"

Re: Encryption error!

Posted: Fri Jan 27, 2012 11:18 am
by tiaofw
Good day!

Marco, by using the HB_Crypt the error is more constant!

I tested using the example of Antonio and worked with my data, the routine will check again to try to locate the problem!

Post here the result.

Thank you!

Re: Encryption error!

Posted: Fri Jan 27, 2012 12:00 pm
by tiaofw
Please test the code below:

Code: Select all


cChave := 'TIAO'

cTeste1 := 'UR010801733' // FIELD OF THE FOURTH The DBF THAT WITH ENCRYPTION SIZE 40 CHARACTER

MSGINFO(cTeste1)

cTeste2 := Encrypt(cTeste1, cChave)

msginfo(cTeste2)

cteste3 := Decrypt(alltrim(cTeste2),cChave)

msginfo(cteste3)


 
Test done using FWH / xHarbour 11.12

Thank you!

Re: Encryption error!

Posted: Fri Jan 27, 2012 8:00 pm
by Rossine
Olá Tião,

Teu erro está nesta linha:

cteste3 := Decrypt(alltrim(cTeste2),cChave)

tem que ser assim:

cteste3 := alltrim(hb_Decrypt(cTeste2,cChave)) // olha o alltrim()

segue exemplo compilado em harbour puro:

Code: Select all


function MAIN

cChave := 'TIAO'

cTeste1 := 'UR010801733' // FIELD OF THE FOURTH The DBF THAT WITH ENCRYPTION SIZE 40 CHARACTER

? cTeste1

cTeste2 := hb_crypt(cTeste1, cChave)

? cTeste2

cteste3 := alltrim(hb_Decrypt(cTeste2,cChave))

? cTeste3

return NIL

 
Veja se isto funciona aí pra você,

Abraços,

Rossine.

Re: Encryption error!

Posted: Fri Jan 27, 2012 8:43 pm
by tiaofw
Rossine wrote:Olá Tião,

Teu erro está nesta linha:

cteste3 := Decrypt(alltrim(cTeste2),cChave)

tem que ser assim:

cteste3 := alltrim(hb_Decrypt(cTeste2,cChave)) // olha o alltrim()

segue exemplo compilado em harbour puro:

Code: Select all


function MAIN

cChave := 'TIAO'

cTeste1 := 'UR010801733' // FIELD OF THE FOURTH The DBF THAT WITH ENCRYPTION SIZE 40 CHARACTER

? cTeste1

cTeste2 := hb_crypt(cTeste1, cChave)

? cTeste2

cteste3 := alltrim(hb_Decrypt(cTeste2,cChave))

? cTeste3

return NIL

 
Veja se isto funciona aí pra você,

Abraços,

Rossine.
Oi rossini, ja tentei usar a função HB_Crypt, mas ela tambem apresenta problema com determinadas strings, como o Fivewin possui a função própria e é mais facil pedir o auxílio ao Linhares preferi ver se consigo solução atraves da CRYPT/DECRYPT.

Mesmo assim obrigado!

Re: Encryption error!

Posted: Fri Jan 27, 2012 9:41 pm
by Daniel Garcia-Gil
Hello


you can not use Alltrim in encrypted string, because some character converted maybe there are useful, in your case the first char is a CHR(10) and you are deleting this with alltrim function

try to understand my point

cteste3 := Decrypt(chr(10)+alltrim( cTeste2 ),cChave)

i personally do it...
convert the encrypted text to base64 encode using hb_base64encode/ hb_base64decode, using base64 i can save to table, dbf, ini file without problem

it's a test

#include "fivewin.ch"

Code: Select all

function main()
local cChave, cTeste1, cTeste2
cChave := 'TIAO'

cTeste1 := 'UR010801733' // FIELD OF THE FOURTH The DBF THAT WITH ENCRYPTION SIZE 40 CHARACTER

MSGINFO(cTeste1)

cTeste2 := hb_Base64Encode( Encrypt(cTeste1, cChave) )

Msginfo( cTeste2 )

cteste3 := Decrypt( hb_Base64Decode( cTeste2 ),cChave)

msginfo(cteste3)

return nil

Re: Encryption error!

Posted: Mon Jan 30, 2012 1:17 pm
by tiaofw
Daniel Garcia-Gil wrote:Hello


you can not use Alltrim in encrypted string, because some character converted maybe there are useful, in your case the first char is a CHR(10) and you are deleting this with alltrim function

try to understand my point

cteste3 := Decrypt(chr(10)+alltrim( cTeste2 ),cChave)

i personally do it...
convert the encrypted text to base64 encode using hb_base64encode/ hb_base64decode, using base64 i can save to table, dbf, ini file without problem

it's a test

#include "fivewin.ch"

Code: Select all

function main()
local cChave, cTeste1, cTeste2
cChave := 'TIAO'

cTeste1 := 'UR010801733' // FIELD OF THE FOURTH The DBF THAT WITH ENCRYPTION SIZE 40 CHARACTER

MSGINFO(cTeste1)

cTeste2 := hb_Base64Encode( Encrypt(cTeste1, cChave) )

Msginfo( cTeste2 )

cteste3 := Decrypt( hb_Base64Decode( cTeste2 ),cChave)

msginfo(cteste3)

return nil


Hello Daniel,

Thanks for the reply!

Can you tell me what is the lib that contains the function hb_base64decode and where do I get?.

I use xHarbour!

Great week!

Re: Encryption error!

Posted: Mon Jan 30, 2012 2:21 pm
by Daniel Garcia-Gil
Hello

sorry, this function is not added in xHarbour, the code exist, but is not added to lib

i'll test to try added it

Re: Encryption error!

Posted: Mon Jan 30, 2012 2:29 pm
by tiaofw
Daniel Garcia-Gil wrote:Hello

sorry, this function is not added in xHarbour, the code exist, but is not added to lib

i'll test to try added it

Thank you!

Re: Encryption error!

Posted: Mon Jan 30, 2012 2:35 pm
by Daniel Garcia-Gil
Hello

the current base64 code from xharbour not work properly, i modified the code and rebuilt the lib affected

please download the new lib http://www.sitasoft.net/fivewin/files/tip.zip

Re: Encryption error!

Posted: Mon Jan 30, 2012 10:58 pm
by tiaofw
"e="Daniel Garcia-Gil"]Hello

the current base64 code from xharbour not work properly, i modified the code and rebuilt the lib affected

please download the new lib http://www.sitasoft.net/fivewin/files/tip.zip


Thank you Daniel!

Doing some testing here with the new tip.lib, I noticed a strange behavior apparently function hb_base64encode/hb_base64decode.

Please test the following code:

Code: Select all


for x := 1 to 10

    cChave := 'TIAO'

    cTeste1 := 'EMULADOR' // 'UR010801733' // CAMPO DO DBF QUE QUARTA A CRIPTOGRAFIA COM TAMANHO DE 40 CARACTERES

    MSGINFO(cTeste1)

    cTeste2 := hb_Base64Encode(Encrypt(cTeste1, cChave))

    msginfo(cTeste2)

    cteste3 := Decrypt(hb_Base64Decode(cTeste2),cChave)

    msginfo(cteste3)

next

 
In some steps the return of function is different, you see!

Any idea what may be motivating the error?"

Re: Encryption error!

Posted: Mon Jan 30, 2012 11:18 pm
by Daniel Garcia-Gil

Re: Encryption error!

Posted: Tue Jan 31, 2012 12:57 am
by tiaofw
Daniel Garcia-Gil wrote:Hello

download the new lib http://www.sitasoft.net/fivewin/files/tip.zip

Thank you Daniel!

Perfect!

I'll try more!

Any problems you know!