[SOLVED] SDKBOX AdMob conflicts (to my surprise) with xxtea encryption algorithm

Hello, guys!
Literally 2 days ago I have discovered that problem that haunted me was in AdMob conflicts with data encryption algorithm xxtea. It was very strange behaviour for me, because I tried to fix this issue about 1 month and I was thinking that reason it’s ad plugin itslef. But after MANY debug builds and tries to find bug I just realized next:
My game uses xxtea + base64 process to save game data on device. When the app writes some metadata in storage(about 4 booleans), that’s OK. But when I try to init admob after that, game just stops with no respond error.
Encryption algorithm is: encode to xxtea, encode result with base64, add some fake characters. Decryption is just reversed encryption :slight_smile:
In test project all works fine: algorithm encode number and could successfuly decode it adterwards.

And now
UserDefault writes 1 const char* string - OK
UserDefault writes 1 encoded string - app stops with no respond

In both cases last logcat output is:

22:50:01.695 D EgretLoader: EgretLoader(Context context): com.xxx.xxx
22:50:01.697 D EgretLoader: The context is not activity: com.xxx.xxx
22:50:01.774 E libEGL : validate_display:255 error 3008 (EGL_BAD_DISPLAY): com.xxx.xxx
22:50:01.842 W cr_CrashFileManager: /data/user/0/com.xxx.xxx/cache/WebView/Crash Reports does not exist or is not a directory: com.xxx.xxx
22:50:01.863 W AudioCapabilities: Unsupported mime audio/evrc: com.xxx.xxx
22:50:01.863 W AudioCapabilities: Unsupported mime audio/qcelp: com.xxx.xxx
22:50:01.865 W VideoCapabilities: Unrecognized profile 2130706433 for video/avc: com.xxx.xxx
22:50:01.869 W Utils : could not parse long range ‘175-174’: com.xxx.xxx
22:50:01.870 W AudioCapabilities: Unsupported mime audio/qcelp: com.xxx.xxx
22:50:01.870 W AudioCapabilities: Unsupported mime audio/evrc: com.xxx.xxx
22:50:01.873 W VideoCapabilities: Unrecognized profile/level 0/3 for video/mpeg2: com.xxx.xxx
22:50:01.874 W VideoCapabilities: Unrecognized profile/level 0/3 for video/mpeg2: com.xxx.xxx
22:50:01.881 W VideoCapabilities: Unsupported mime video/mp4v-esdp: com.xxx.xxx

But in last one at the end here is additional lines:

23:21:05.768 I ExoPlayerImpl: Init 1.3.1: com.xxx.xxx
23:21:05.823 W chromium: [WARNING:render_frame_host_impl.cc(2750)] OnDidStopLoading was called twice.: com.xxx.xxx

For test was used devices: Meizu M5, Meizu M5 Note and Xiaomi Redmi Note 3 Pro, but that issue was raised only on Xiaomi! Very soon we plan to release game and I very scared of this behaviour because we want best maintenance on all devices. Will be very disappointing if whole game will not work cause of ad plugin :frowning:

I will be grateful for any help, guys, just don’t know what to do next, beacuse I can’t find any information about this!
Thank you very much! Best regards
@nite

Would you plz share the test project with me?
What about GitHub ?

I’ll check this issue with Xiaomi device.

Hello, @yinjimmy !
Yes, sure, test project just consists of part which use UserDefault to save encrypted data and encrypt engine part(sources here)
xxtea.cpp (4.8 KB)
xxtea.h (1.5 KB)
EncryptEngine.h (805 Bytes)
EncryptEngine.cpp (2.2 KB)
Used last cocos2d-x version

Also, tomorrow I noticed that this issue belongs to UnityAds sdkbox plugin too. Maybe this fault of sdkboxads?

need a test code snip, thanks.

some code review

1

#include <sstream>

	//creating string with value data
	string tempStr;
	sprintf((char*) tempStr.c_str(), "%d", number);

	xxtea_long dataLen = strlen(tempStr.c_str());

	//create pointer to uchar for xxtea algorithm
	unsigned char* data = new unsigned char[dataLen];

	//copy data from temp string to uchar pointer
	strcpy((char*) data, tempStr.c_str());

// ====>

stringstream ss;
ss << number;
// get string
ss.str()

2

unsigned char* data = new unsigned char[dataLen];

you did not free the buffer.

@yinjimmy
Oh, ok, here is test code

About 1, stringstream much slower than sprintf method so I use him in purpose of performance
And thanks for 2, just didn’t saw! :open_mouth:

i checked with Xiaomi device, but without crash.

D/cocos2d-x debug info(32097): create rendererRecreatedListener for GLProgramState
D/cocos2d-x debug info(32097): >>> z3YrjSoJSPk=222222
D/cocos2d-x debug info(32097):

Damn, I wrote init of sdkboxplay. Of course its admob
In AppDelegate.cpp

//admob
#include "pluginadmob/PluginAdMob.h"

and

/* INIT ADMOB*/
	sdkbox::PluginAdMob::init();

And recently I tested on Xiaomi Redmi 4X, all was fine… What does it mean? I can’t say that my Redmi Note 3 Pro are low-end device and another apps with admob ads works perfectly

I checked with Redmi Note 2 ;(

Note that issue are happening only when I enable encryption part. I don’t think that it belongs to device itself :confused:
Its last step to release the game. I am so confused :frowning:

UPD:
I found very useful information. It’s not fault of xxtea or base64. I tried to off they(save primitive strings) and nothing changed. Still not respond after some time. So on I think that problem in that code snippet:

//DataManager

cocos2d::UserDefault::getInstance()->setStringForKey(saveKeys[SaveKey::PGAMES].c_str(),
			EncryptEngine::getInstance()->encrypt(-1, 1));

//Encrypt

std::string offsets[7] = { "11111", "222222", "333333", "444444", "555555", "666666", 
    "777777" };

//creating string with value data
string tempStr;
sprintf((char*) tempStr.c_str(), "%d", number);

xxtea_long dataLen = strlen(tempStr.c_str());

//create pointer to uchar for xxtea algorithm
unsigned char* data = new unsigned char[dataLen + 1];

//copy data from temp string to uchar pointer
strcpy((char*) data, tempStr.c_str());

//length of returned result from xxtea_encrypt
xxtea_long retLen = 0;

//encrypt data with key
unsigned char* result = (unsigned char*)"123";//xxtea_encrypt(data, dataLen, key, keyLen, &retLen);

//free buffuer
delete[] data;
data = 0;

//encode data with base64 encoding for avoid special-characters
char* b64enc = nullptr;
//retLen = cocos2d::base64Encode(result, retLen, &b64enc);
b64enc = "123";

std::string finalResult = std::string(b64enc) + offsets[(int)type];

//return result in string
return finalResult;

http://www.cplusplus.com/reference/string/string/c_str/

const char* c_str() const;

plz use:

char buf[512];
sprintf(buf, "%d", number);

Hello, @yinjimmy
I found that app stops earlier!
Here!

bool EncryptEngine::init()
{
	instance = new EncryptEngine();

	strcpy((char*)instance->key, "9N6Wzr3P");

	return true;
}

Definition of EncryptEngine:

class EncryptEngine
{
public:
	//encrypt value of [DataType] type to string
	std::string encrypt(int number, Jumpeleon::DataType type);

	//decrypt value with [Save] type
	int decrypt(std::string tempStr, Jumpeleon::DataType type);

	//singleton getInstance
	static EncryptEngine* getInstance();
	static void destroyInstance();

private:
	const xxtea_long keyLen = 8;
	unsigned char key[8];

	static bool init();

	static EncryptEngine* instance;
	//disallow for singleton
	EncryptEngine() {};
	~EncryptEngine() { if (instance) destroyInstance(); }
};

And result…he just again “not respond”
Now I am totally confused :confused:

Maybe class name intersects with some other system name? (EncryptEngine singleton)

Ok, finally I solved it :smiley:
The problem was in memory allocation: on initialize encrypt singleton key was copied in wrong declared arrray of uchars

unsigned char key[keyLen];
unsigned char* key;

instance->key = new unsigned char[instance->keyLen+1];
strcpy((char*)instance->key, “123”);

It was confused with ads plugin of sdkbox

Thank you very much, @yinjimmy, for helping me!!! :slight_smile: