AES encrypt

Hello all,
i’m making a solution to generate QRcode for a Pepsi drink machine, the machine read the code and fills the glass with the requested drink.
I’ve made all with this library:

It works all flawless, but i have to buy it after 30 days of testing.
I’ve found that also MonkeyBread plugins has crypto function, and i’ve got a developer license.
but i can’t figure out how to achieve the same result with mbs, here is my original code, someone can show me the path:

Dim uts,bevanda As String

Dim crypt As New Chilkat.Crypt2
crypt.CryptAlgorithm = "aes"
crypt.CipherMode = "cbc"

crypt.KeyLength = 128
crypt.PaddingScheme = 0
crypt.EncodingMode = "hex"

// An initialization vector is required if using CBC mode.
// ECB mode does not use an IV.
// The length of the IV is equal to the algorithm's block size.
// It is NOT equal to the length of the key.
Dim ivHex As String
ivHex = "6d5a7134743777217a25432a462d4a61"
crypt.SetEncodedIV ivHex,"hex"

// The secret key must equal the size of the key.  For
// 256-bit encryption, the binary secret key is 32 bytes.
// For 128-bit encryption, the binary secret key is 16 bytes.
Dim keyHex As String
keyHex = "34753778214125442a472d4b61506453"
crypt.SetEncodedKey keyHex,"hex"

// Encrypt a string...
// The input string is 44 ANSI characters (i.e. 44 bytes), so
// the output should be 48 bytes (a multiple of 16).
// Because the output is a hex string, it should
// be 96 characters long (2 chars per byte).
Dim encStr As String
uts = unixts()

bevanda = "0000" + bev.text
encStr = crypt.EncryptStringENC("00099MEX020091" + uts + bic.Text + bevanda.Right(4))
System.DebugLog("QRCODE:" + " 0"  + encStr)

thanks so much and happy new year

1 Like

Note that Xojo has native AES in the Crypto module now as a possible option: Crypto.AESEncrypt - Xojo Documentation

1 Like

Are you asking how NOT to pay for a software?

If you use MBS Xojo Plugins, you can also check CipherMBS class.

Absolutly not, i pay for all software that i use, as said i’ve got a developer license of MBS,
I asked for an alternative solution, if available.

1 Like

thanks @Matthew_Dinmore1, i will make a check.

thanks @Christian_Schmitz, i didn’t see that class, i will try it.

Hi @Christian_Schmitz ,
i’ve tryed the CipherMBS class, but i think i’m missing something.
Here is my code:

dim textToCrypt,back AS string
textToCrypt =  "00023MAX010491" + uts + bic.Text + bevanda.Right(4)
dim c as CipherMBS = CipherMBS.aes_128_cbc
dim CKey as MemoryBlock = "4u7x!A!D*G-AwXdS"
dim CIV as MemoryBlock = "mZf4t6w!z%C*F-Ja"

call c.EncryptInit Ckey, CIV

dim output1 as string = c.ProcessString(textToCrypt)
output1 = output1 + c.FinalizeAsString

dim a as new AESMBS

call a.SetKey(CKey, 128)
dim output2 as MemoryBlock
a.EncryptCBC(textToCrypt, 16, CIV, output2)

i need a CBC encryption, but my output2 memoryblock is nil, what’s wrong? (output1 is taken for your sample documentation, but i don’t need it)

thanks so much

Don’t use AESMBS for this. CipherMBS is newer can do much more.

And since output2 is nil, it will use the input buffer. But that one is temporary since you pass a string, so your result is lost.

I fixed your code a bit:

Dim textToCrypt,back As String
textToCrypt =  "00023MAX010491xx" '+ uts + bic.Text + bevanda.Right(4)
Dim c As CipherMBS = CipherMBS.aes_128_cbc
Dim CKey As MemoryBlock = "4u7x!A!D*G-AwXdS"
Dim CIV As MemoryBlock = "mZf4t6w!z%C*F-Ja"

Call c.EncryptInit Ckey, CIV

Dim output1 As String = c.ProcessString(textToCrypt)
output1 = output1 + c.FinalizeAsString

Dim a As New AESMBS

Call a.SetKey(CKey, 128)
Dim output2 As New MemoryBlock(16)
Dim inputm As MemoryBlock = textToCrypt

a.EncryptCBC(textToCrypt, 16, CIV, output2)

which outputs:


so CipherMBS does padding and extended the block, while we only encrypted first block with AESMBS class.

thanks @Christian_Schmitz , more clear now and it works. :smiley:
Thanks so much.

Oh, OK. I misunderstood your question. Sorry about that