Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
nemoryoliver
Posts: 97
Registered: ‎11-13-2012
My Device: BB10 Simulator
Accepted Solution

[HELP] AES CBC & ECB Encryption & Decryption

[ Edited ]

I am very new to QT and also with Encryption stuffs. Just a week I started fighting to learn encryption stuffs but I am really stuck and don't know what to do.

 

I just really need to decrypt a downloaded blob. using ECB and also there's another I need with CBC

 

Here's the working PHP functions I want to convert to QT

 

function decryptECB($data) 
{
	$BLOB_ENCRYPTION_KEY 	= 'M02cnQ51Ji97vwT4';
	return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $BLOB_ENCRYPTION_KEY, pad($data), MCRYPT_MODE_ECB);
}

function encryptECB($data) 
{
	$BLOB_ENCRYPTION_KEY 	= 'M02cnQ51Ji97vwT4';
	return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $BLOB_ENCRYPTION_KEY, pad($data), MCRYPT_MODE_ECB);
}

function pad($data, $blocksize = 16) 
{
	$pad = $blocksize - (strlen($data) % $blocksize);
	return $data . str_repeat(chr($pad), $pad);
}

// FOR STORIES
function decryptCBC($data, $key, $iv) 
{
	// Decode the key and IV.
	$iv = base64_decode($iv);
	$key = base64_decode($key);

	// Decrypt the data.
	$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);
	$padding = ord($data[strlen($data) - 1]);

	return substr($data, 0, -$padding);
}

 

I tried AESCryptoDemo  and I think it uses CBC which I saw in it's AESParams.cpp file. but no example for ECB

 

This is how I load downloaded the file in QT

 

QString filename = "data/files/blobs/storyphoto.jpg";

QFile* file = new QFile(filename);

if(file->open(QIODevice::ReadOnly))
{
	qDebug() << "BYTES: " + QString::fromAscii(file->readAll()) + ", SIZE: " + file->size();

	QString decryptedBlob = decrypt(file->readAll());

	qDebug() << "DECRYPTED BLOB: " + toHex(file->readAll());

	QFile* newFile = new QFile("data/files/blobs/NEW.jpg");

	if (!newFile->open(QIODevice::WriteOnly))
	{
		qDebug() << "PROBLEM OPENING FILE: " + filename;
	}
	else
	{
		newFile->write(decryptedBlob.toAscii());
	}

	newFile->close();
}
else
{
	qDebug() << "CANT OPEN: " + filename;
}

file->close();

 I have also modified some AESCryptoDemo functions - Just to return values

 

QString ApplicationUI::encrypt(QString data)
{
	QString encryptedData = "";

	QByteArray in(data.toUtf8());
	pad(in);
	QByteArray out(in.length(), 0);

	if (crypt(true, in, out))
	{
		encryptedData = toHex(out);
	}

	return encryptedData;
}

 

QString ApplicationUI::decrypt(QString data)
{
	QString decryptedBlob = "";

	QByteArray in;

	if (!fromHex(data, in))
	{
		qDebug() << "Cipher text is not valid hex";
		return "";
	}

	QByteArray out(in.length(), 0);

	if (crypt(false, in, out))
	{
		if (removePadding(out))
		{
			decryptedBlob = QString::fromUtf8(out.constData(), out.length());
		}
	}

	return decryptedBlob;
}

 

QString ApplicationUI::toHex(const QByteArray & in)
{
	static char hexChars[] = "0123456789abcdef";

	const char * c = in.constData();
	QString toReturn;

	for (int i = 0; i < in.length(); ++i)
	{
		toReturn += hexChars[(c[i] >> 4) & 0xf];
		toReturn += hexChars[(c[i]) & 0xf];
	}

	return toReturn;
}

 Here I am trying to encrypt and decrypt a simple string.. 

 

I also don't think if I need to do something with the  char "\" because when I qDebug it it is forgotten.

 

QString key = "i17ZJekcUf3J\/P\/EFk3fmXHSBnxfMvf6BFuN0kbI57Q=";
QString iv = "wmUlWE+EmUMguG\/ealoMcg==";

QString hexKey = toHex(key.toAscii());
QString hexIV = toHex(iv.toAscii());

setKey(hexKey);
setIV(hexIV);

qDebug() << "HEX KEY: " + hexKey + ", ORIG KEY: " + key;
qDebug() << "HEX IV: " + hexIV + ", ORIG IV: " + iv;

QString encrypted = encrypt("data");
QString decrypted = decrypt(encrypted);

qDebug() << "ENCRYPTED " + encrypted + ", DECRYPTED: " + decrypted;

 running the above code I get 

 

"HEX KEY: 6931375a4a656b635566334a2f502f45466b33666d584853426e78664d7666364246754e306b62493537513d, ORIG KEY: i17ZJekcUf3J/P/EFk3fmXHSBnxfMvf6BFuN0kbI57Q=" 

"HEX IV: 776d556c57452b456d554d6775472f65616c6f4d63673d3d, ORIG IV: wmUlWE+EmUMguG/ealoMcg==" 

FAILED "AESKey" AESKey 57616 "SB_ERR_BAD_KEY_LEN (57616)" 

"Could not create a key. SB_ERR_BAD_KEY_LEN (57616)" 

Cipher text is not valid hex 
"ENCRYPTED , DECRYPTED: " 

 Thanks so much! Hope someone can help.

Please use plain text.
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: [HELP] AES CBC & ECB Encryption & Decryption

Hi,

 

Looks like a conversion problem.

 

You've taken a base64 key:

i17ZJekcUf3J/P/EFk3fmXHSBnxfMvf6BFuN0kbI57Q=

and converted it to hex:

6931375a4a656b635566334a2f502f45466b33666d584853426e78664d7666364246754e306b62493537513d

this is a valid hexadecimal encoding of the text of the key - not of the value.

 

eg: 69 =>"i"  31=>"1" 37=>"7"

 

Also - this string is 88 chars long - which means 44 bytes - which is not a valid AES key size... so that's why you're seeing that error.

 

QByteArray does have base64 conversion functions (and hex functions - the sample should probably use those...)

 

http://qt-project.org/doc/qt-4.8/qbytearray.html#fromBase64

 

Actual hex you should expect is: 8b5e...

robwilliams@IT0000000061975 ~/Desktop
$ cat dat.b64 
i17ZJekcUf3J/P/EFk3fmXHSBnxfMvf6BFuN0kbI57Q=

robwilliams@IT0000000061975 ~/Desktop
$ openssl base64 -d -in dat.b64 -out dat.bin 

robwilliams@IT0000000061975 ~/Desktop
$ xxd dat.bin
0000000: 8b5e d925 e91c 51fd c9fc ffc4 164d df99  .^.%..Q......M..
0000010: 71d2 067c 5f32 f7fa 045b 8dd2 46c8 e7b4  q..|_2...[..F...

 Hope this helps...

 

Thanks!

--
Rob is no longer associated with BlackBerry.
Please use plain text.
Developer
nemoryoliver
Posts: 97
Registered: ‎11-13-2012
My Device: BB10 Simulator

Re: [HELP] AES CBC & ECB Encryption & Decryption

[ Edited ]

Thanks very much Rob!! It works!!!!!!!!!!!!!!! :Clap:

 

here's my new function.

 

void ApplicationUI::decryptCBC(QString filename, QString key, QString iv)
{
	_key 	= toHex(QByteArray::fromBase64(key.toUtf8()));
	_iv 	= toHex(QByteArray::fromBase64(iv.toUtf8()));

	QFile* file = new QFile(filename);

	if(!file->open(QIODevice::ReadOnly))
	{
		qDebug() << "CANT OPEN: " + filename;
	}

	QByteArray in(file->readAll());

	QByteArray out(in.length(), 0);

	if (crypt(false, in, out))
	{
		if (removePadding(out))
		{
			QString toUse(QString::fromUtf8(out.constData(), out.length()));
			setRecoveredPlainText(toUse);

			QFile* newFile = new QFile("data/NEW.jpg");

			if (!newFile->open(QIODevice::WriteOnly))
			{
				qDebug() << "PROBLEM OPENING FILE: data/NEW.jpg";
			}
			else
			{
				qDebug() << "SUCCESS OVERWRITTEN: " + filename;

				newFile->write(out);
			}

			newFile->close();
		}
	}

	file->close();
}

 But now I'm trying to do the ECB mode.. :Helpsmilie:

 

I am using a different key for ECB Mode: M02cnQ51Ji97vwT4

 

I tried changing the AESParams.cpp to

 

int rc = hu_AESParamsCreate(SB_AES_ECB, SB_AES_128_BLOCK_BITS, NULL, NULL, &_aesParams, _globalContext.ctx());
	maybeLog("AESParamsCreate", rc);

 and get this new error

 

FAILED "AESKey" AESKey 57616 "SB_ERR_BAD_KEY_LEN (57616)" 
"Could not create a key. SB_ERR_BAD_KEY_LEN (57616)" 

 I also tried temporarily changed the generate function to

 

QString ApplicationUI::generate()
{
	QByteArray buffer(16, 0);
	return toHex(buffer);
}

 since ECB doesn't require a dynamic iv like in the php decryptECB() code above

Please use plain text.
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: [HELP] AES CBC & ECB Encryption & Decryption

Hi,

 

Can you show me the length/bytes of the key you're using just prior to the error?

 

Thanks!

--
Rob is no longer associated with BlackBerry.
Please use plain text.
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: [HELP] AES CBC & ECB Encryption & Decryption

Thought of something else;

 

If you're running a valid hex key (as produced by generate()( through a base64 decode, you're going to get a short invalid key...

--
Rob is no longer associated with BlackBerry.
Please use plain text.
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: [HELP] AES CBC & ECB Encryption & Decryption

PS:

 

Don't use ECB for encrypting files. Look at the Tux picture in this for an example of why:

 

http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_.28ECB.29

 

--
Rob is no longer associated with BlackBerry.
Please use plain text.
Developer
nemoryoliver
Posts: 97
Registered: ‎11-13-2012
My Device: BB10 Simulator

Re: [HELP] AES CBC & ECB Encryption & Decryption

Thanks so much for the help. The length of the new key I am using that has the error is 12, and it multiplies by 8 in the AESParams so total of 96,

 

Also my app is Snap2Chat and I am.using the snapchat server, their blobs are encrypted using ECB so I have no.choice.

 

Please use plain text.
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: [HELP] AES CBC & ECB Encryption & Decryption

Hi,

 

12 bytes / 96 bits is not a valid AES key. Must be 128/192/256 bits...

 

Thanks!

--
Rob is no longer associated with BlackBerry.
Please use plain text.
Developer
nemoryoliver
Posts: 97
Registered: ‎11-13-2012
My Device: BB10 Simulator

Re: [HELP] AES CBC & ECB Encryption & Decryption

Also the keys I am.using are not of generate.. I am using what snapchat provides. Their blob encryption key is ‎M02cnQ51Ji97vwT4 and it is for ECB.‎

Please use plain text.
Developer
nemoryoliver
Posts: 97
Registered: ‎11-13-2012
My Device: BB10 Simulator

Re: [HELP] AES CBC & ECB Encryption & Decryption

Thanks very much. But now I maybe will.be stuck.to this since they key is M02cnQ51Ji97vwT4 and that's the only.key I have to use. :smileysad: the php decrypt ECB function above works fine though with that key.

Please use plain text.