Page 1 of 1

AESencoder, AESdecoder and Base64

Posted: Fri Dec 28, 2012 3:03 pm
by Maxter
Hi team

I have a very strange result using AESencoder with Base64encoder.

This routine work fine in my project for a long time. In summary, what i do is take a MD5 string, cipher it and enconde with Base 64 to send it to an URL in a web server.
The MD5 string i get is a unique hash created using hardware infomation and id's. This work Ok and always create a 32 bytes string.
As i say, i use this routine in a couple of computers and the hash is always ciphered/encoded and then decoded/deciphered perfect.

Now i need to run this routine in a computer with a hash "2a48f1983b65eb6237cd3617b890998e"
Only using this string, when i try to decipher it a get a strange result:

Code: Select all

2a48f1983b65eb6237cd3617b890998e
2a48f1983b65eb62ýÕTZN/VÙmÑTƒÁvä
The second part of the ciphered string can't be deciphered correctly
But if you change the hash in the var MyString$, with at least one char only, it's work again.
Base64 routine seems to work well, since the encrypted string look similar to it after using the base64decode function.

I'm using PB 4.60 x86 on Windows XP SP3.
If anybody can try the next example, please let me know your results.

Code: Select all

 MyString$ = "2a48f1983b65eb6237cd3617b890998e"
;MyString$ = "2a48f1983b65eb6237cd3617b890998d"     ;<-- Work fine, change the last 'e' with 'd'
;MyString$ = "c760c312c5c28fd311d07ef5c14a0bf0"     ;<-- Work fine, different hash

inputKey.s = "12345678"
key.s = LSet(inputKey.s,16,"x")

*CipheredString   = AllocateMemory(StringByteLength(MyString$,#PB_UTF8)+1)
*DecipheredString = AllocateMemory(StringByteLength(MyString$,#PB_UTF8)+1)
Encoded64$ = Space(1024) 
Decoded64$ = Space(1024)

Debug "Original string: "+MyString$

If AESEncoder(@MyString$, *CipheredString, StringByteLength(MyString$,#PB_UTF8), @key.s, 128, ?InitializationVector, #PB_Cipher_CBC)
  Debug "My Ciphered String: "+PeekS(*CipheredString)
  Debug " "
  
  encLenB64 = Base64Encoder(*CipheredString, MemoryStringLength(*CipheredString,#PB_UTF8), @Encoded64$, 1024)
  Debug "My Ciphered Base64 String: " + Encoded64$
  Debug "bytes encoded: " + Str(encLenB64)
  Debug " "
  
  decLenB64 = Base64Decoder(@Encoded64$, StringByteLength(Encoded64$,#PB_UTF8), @Decoded64$, 1024)
  Debug "My Ciphered String (decoded from Base64): "+Decoded64$
  Debug "bytes decoded: " + Str(decLenB64)
  Debug " "
  
  AESDecoder(@Decoded64$, *DecipheredString, StringByteLength(MyString$,#PB_UTF8), @key.s, 128, ?InitializationVector, #PB_Cipher_CBC)
  Debug "Final String (deciphered): "+PeekS(*DecipheredString)
    
Else
  Debug "AES error"
EndIf



DataSection
  Key:
    Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06

  InitializationVector:
    Data.b $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $41
  
EndDataSection 
Thanks!
Max.

Re: AESencoder, AESdecoder and Base64

Posted: Fri Dec 28, 2012 3:17 pm
by STARGÅTE
the output of AESEncoder is no string, so you can not use MemoryStringLength(*CipheredString,#PB_UTF8)!!
in you example, AESEncoder() create a NUL-character.

Use MemorySize() for *CipheredString and use only AllocateMemory(StringByteLength(MyString$,#PB_UTF8))

Code: Select all

 MyString$ = "2a48f1983b65eb6237cd3617b890998e"
;MyString$ = "2a48f1983b65eb6237cd3617b890998d"     ;<-- Work fine, change the last 'e' with 'd'
;MyString$ = "c760c312c5c28fd311d07ef5c14a0bf0"     ;<-- Work fine, different hash

inputKey.s = "12345678"
key.s = LSet(inputKey.s,16,"x")

*CipheredString   = AllocateMemory(StringByteLength(MyString$,#PB_UTF8))
*DecipheredString = AllocateMemory(StringByteLength(MyString$,#PB_UTF8)+1)
Encoded64$ = Space(1024) 
Decoded64$ = Space(1024)

Debug "Original string: "+MyString$

If AESEncoder(@MyString$, *CipheredString, StringByteLength(MyString$,#PB_UTF8), @key.s, 128, ?InitializationVector, #PB_Cipher_CBC)
  Debug "My Ciphered String: "+PeekS(*CipheredString)
  Debug " "
  
  encLenB64 = Base64Encoder(*CipheredString, MemorySize(*CipheredString), @Encoded64$, 1024)
  Debug "My Ciphered Base64 String: " + Encoded64$
  Debug "bytes encoded: " + Str(encLenB64)
  Debug " "
  
  decLenB64 = Base64Decoder(@Encoded64$, encLenB64, @Decoded64$, 1024)
  Debug "My Ciphered String (decoded from Base64): "+Decoded64$
  Debug "bytes decoded: " + Str(decLenB64)
  Debug " "
  
  AESDecoder(@Decoded64$, *DecipheredString, StringByteLength(MyString$,#PB_UTF8), @key.s, 128, ?InitializationVector, #PB_Cipher_CBC)
  Debug "Final String (deciphered): "+PeekS(*DecipheredString)
    
Else
  Debug "AES error"
EndIf



DataSection
  Key:
    Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06

  InitializationVector:
    Data.b $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $41
  
EndDataSection 
so no bug!

but you code do not run with unicode!

Re: AESencoder, AESdecoder and Base64

Posted: Fri Dec 28, 2012 6:00 pm
by Maxter
Thank you Stargate!

Now i understand how it is working.

I will be looking in the forum for examples to make it Unicode compatible and post it when possible.


Thanks again, i wish a happy new year for all the PB community!

Max.