MBS Encryption Kit wrong result

Hello, I’m trying to convert this PHP function:

[code] #The key, it must be an hex encoded key
$key = pack(‘H*’, $_POST[‘key’]);

#The text that will be encrypted
$plaintext = $_POST['decrypted'];

#With this I get the encrypted value
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_ECB);

# encode into hex
$ciphertext_hex = bin2hex($ciphertext);[/code]

into XOJO using the MBS encryption kit.
I wrote this:

[code] dim ciph As CipherMBS
ciph = CipherMBS.aes_128_ecb

dim cKey As MemoryBlock = “mykey”
dim data As String = “mydata”

ciph.EncryptInit(cKey)

dim output As String
output = ciph.ProcessString(data) + ciph.FinalizeAsString
output = EncodeHex(output)
[/code]

In both cases I use

key="5350d786e90956e915fa5e9ea09d5346" data="grandemax"
But I get,

ouput="4b3cf39f8861d8c8f634eb26c68bfa1a" 'in the fisrt case ouput="4EFE8E8379F5F26233C866AE314161C8" 'in the second one.

Any of you have any idea in order to fix that?
In need the PHP result into XOJO too.
Thanks to everyone.

[code] dim ciph As CipherMBS
ciph = CipherMBS.aes_128_ecb

dim cKey As MemoryBlock = DecodeHex(“5350d786e90956e915fa5e9ea09d5346”)
dim data As String = “grandemax”

ciph.EncryptInit(cKey)

while lenb(data) mod ciph.BlockSize <> 0
data = data + chrb(0)
wend

dim output As String
output = ciph.ProcessString(data) + ciph.FinalizeAsString
output = EncodeHex(output)

Break[/code]

this gives “4B3CF39F8861D8C8F634EB26C68BFA1AE927159549699D73366B9966CB312104”

as you see I add padding to your data with zero bytes to make sure data is big enough.
mcrypt does that automatically.

I haven’t tested but an alternative that might be faster:

dim data as MemoryBlock = "grandemax"
…
data.Size = ciph.BlockSize

This should work since a MemoryBlock will automatically convert to a nil-encoded String, and a String will automatically convert to a MemoryBlock. Resizing the MemoryBlock upward will pad it with nulls.

that would cut away anything which is longer than 16 bytes.

I shouldn’t post right after waking up.

dim padding as integer = ciph.BlockSize - ( data.Size mod ciph.BlockSize )
if padding <> ciph.BlockSize then
  data.Size = data.Size + padding
end if

I think… :slight_smile:

that is common for most of us.

or very late at night.