mime.unb64 returning nil

I’m getting a problem with certain encoded string when I use mime.unb64 function. It is returning nil which do not understand at all. If I change 1 character in the string it returns data like expected.

[lua]

local mime      = require(“mime”)

local xx =  “AIvsQo6wlonsdE1zqV/czDMWh+SvXRz5CIR0Fl/l70byyhjyV5fSXtX/BJqJWrg4ebnWIGm9/Q0zwARkcYCzJZA==”

print((mime.unb64(xx)))

– Output: nil

[/lua]

Spent my morning on this, not much closer really, I need some ninja help.

I’m encoding and decoding strings excatly like described here: http://www.coronalabs.com/blog/2013/06/11/tutorial-using-the-openssl-plugin/

Code below produces error

Runtime error: main.lua:15: bad argument #1 to ‘decrypt’ (string expected, got nil) 

The interesting thing is if you change the key or str code works fine. So the the combination of that key and string is fatal and causes an error in about 1/200 games for me. Not good!

[lua]local openssl   = require(“plugin.openssl”)

local cipher    = openssl.get_cipher ( “aes-256-cbc” )

local mime      = require(“mime”)

local key       = “abc” 

local str = ‘AHTSTD4H7S2C8S2D7H’

local encrypted = cipher:encrypt(str, key)

print(“encrypted”, encrypted)

local encryptedB64 = mime.b64(encrypted)

print(“encryptedB64”, encryptedB64)

– Now decrypt

local unpacked64 = mime.unb64(encryptedB64)

local decrypted = cipher:decrypt(unpacked64, key)

print(“decrypted”, decrypted)

[/lua]

One interesting thing is that the “print(“encrypted”, encrypted)” line does not print anything in above case, while it prints some characters if you change the str variable. 

After some investigation this turned out to be a bug in the mime.unb64() function (it didn’t like NULs in the data). The test case now emits the expected output:

Corona Simulator[41508:303] encrypted Corona Simulator[41508:303] encryptedB64 ACa8Pgj4fGX4i2KDzRnd4NxGVJJyO3+ECO+3PUaEiQw= Corona Simulator[41508:303] type(unpacked64):  string Corona Simulator[41508:303] decrypted AHTSTD4H7S2C8S2D7H

“encrypted” is blank because it starts with a NUL.

It’ll be fixed in the next Daily Build.

It’ll be fixed in the next Daily Build.

FYI it is still not fixed.

Sorry about that … I’ve found the issue and am starting a new build.

Now it really is fixed in Corona SDK 2013.1230:

2013-10-08 14:01:50.605 Corona Simulator[80799:707]  Build: 2013.1230
2013-10-08 14:01:50.608 Corona Simulator[80799:707] The file sandbox for this project is located at the following folder:
(/Users/perry/Library/Application Support/Corona Simulator/bug-encrypt-6E6212A77AA42BA78D657EF3028C8244)
2013-10-08 14:01:50.621 Corona Simulator[80799:707] – This is module “extern” version 1.0 –
2013-10-08 14:01:50.637 Corona Simulator[80799:707] encrypted
2013-10-08 14:01:50.637 Corona Simulator[80799:707] encryptedB64 ACa8Pgj4fGX4i2KDzRnd4NxGVJJyO3+ECO+3PUaEiQw=
2013-10-08 14:01:50.638 Corona Simulator[80799:707] type(unpacked64):  string
2013-10-08 14:01:50.638 Corona Simulator[80799:707] decrypted AHTSTD4H7S2C8S2D7H

Spent my morning on this, not much closer really, I need some ninja help.

I’m encoding and decoding strings excatly like described here: http://www.coronalabs.com/blog/2013/06/11/tutorial-using-the-openssl-plugin/

Code below produces error

Runtime error: main.lua:15: bad argument #1 to ‘decrypt’ (string expected, got nil) 

The interesting thing is if you change the key or str code works fine. So the the combination of that key and string is fatal and causes an error in about 1/200 games for me. Not good!

[lua]local openssl   = require(“plugin.openssl”)

local cipher    = openssl.get_cipher ( “aes-256-cbc” )

local mime      = require(“mime”)

local key       = “abc” 

local str = ‘AHTSTD4H7S2C8S2D7H’

local encrypted = cipher:encrypt(str, key)

print(“encrypted”, encrypted)

local encryptedB64 = mime.b64(encrypted)

print(“encryptedB64”, encryptedB64)

– Now decrypt

local unpacked64 = mime.unb64(encryptedB64)

local decrypted = cipher:decrypt(unpacked64, key)

print(“decrypted”, decrypted)

[/lua]

One interesting thing is that the “print(“encrypted”, encrypted)” line does not print anything in above case, while it prints some characters if you change the str variable. 

After some investigation this turned out to be a bug in the mime.unb64() function (it didn’t like NULs in the data). The test case now emits the expected output:

Corona Simulator[41508:303] encrypted Corona Simulator[41508:303] encryptedB64 ACa8Pgj4fGX4i2KDzRnd4NxGVJJyO3+ECO+3PUaEiQw= Corona Simulator[41508:303] type(unpacked64):  string Corona Simulator[41508:303] decrypted AHTSTD4H7S2C8S2D7H

“encrypted” is blank because it starts with a NUL.

It’ll be fixed in the next Daily Build.

It’ll be fixed in the next Daily Build.

FYI it is still not fixed.

Sorry about that … I’ve found the issue and am starting a new build.

Now it really is fixed in Corona SDK 2013.1230:

2013-10-08 14:01:50.605 Corona Simulator[80799:707]  Build: 2013.1230
2013-10-08 14:01:50.608 Corona Simulator[80799:707] The file sandbox for this project is located at the following folder:
(/Users/perry/Library/Application Support/Corona Simulator/bug-encrypt-6E6212A77AA42BA78D657EF3028C8244)
2013-10-08 14:01:50.621 Corona Simulator[80799:707] – This is module “extern” version 1.0 –
2013-10-08 14:01:50.637 Corona Simulator[80799:707] encrypted
2013-10-08 14:01:50.637 Corona Simulator[80799:707] encryptedB64 ACa8Pgj4fGX4i2KDzRnd4NxGVJJyO3+ECO+3PUaEiQw=
2013-10-08 14:01:50.638 Corona Simulator[80799:707] type(unpacked64):  string
2013-10-08 14:01:50.638 Corona Simulator[80799:707] decrypted AHTSTD4H7S2C8S2D7H

I’m using build .1247 and still getting ‘nil’ on most strings, noticably shorter strings as string that are longer or contain a blank space seem to work fine.

It seems more likely the error occurs during encrypting as I see that the strings that do correctly encrypt and decrypt are much longer(always 43char long encrypted string) then the ones that are returning nil (always 22char long encrypted string)

Is there anything else to use other then aes-256-cbc, as i notice also that strings longer then 16 characters get truncated, not sure if that is a limit of mime.b64 or aes-256-cbc.

Also, could it be that I’m encrypting a bunch of strings from within a loop? Does the encrypt function require a bit of time to encrypt properly?

I’ve been using aes-256-ebc and my Galaxy Note 2 is no longer encrypting/decrypting properly, while the Simulator and iOS seems to do fine. I’ve tried reverting my build as far back as June (when I know it was working) but to no avail.

I’m using build .1247 and still getting ‘nil’ on most strings, noticably shorter strings as string that are longer or contain a blank space seem to work fine.

It seems more likely the error occurs during encrypting as I see that the strings that do correctly encrypt and decrypt are much longer(always 43char long encrypted string) then the ones that are returning nil (always 22char long encrypted string)

Is there anything else to use other then aes-256-cbc, as i notice also that strings longer then 16 characters get truncated, not sure if that is a limit of mime.b64 or aes-256-cbc.

Also, could it be that I’m encrypting a bunch of strings from within a loop? Does the encrypt function require a bit of time to encrypt properly?

I’ve been using aes-256-ebc and my Galaxy Note 2 is no longer encrypting/decrypting properly, while the Simulator and iOS seems to do fine. I’ve tried reverting my build as far back as June (when I know it was working) but to no avail.