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
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

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

Hi,

 

Test padding it... Take the 96 bit key and add 4 zero bytes to the end...

 

eg:

_key.append((char)0);
_key.append((char)0);
_key.append((char)0);
_key.append((char)0);

 

 

 

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

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

Thanks mcuh robbie. Hope we have the same timezone. I will try that tom after school and let you know. :smileywink:

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

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

I solved my other problem.. I didn't have to.convert the key to base64.. all is good now!!!! Thanks so much Robbie and this forum!!! :smileyhappy: will post my full solution.later.

Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

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

Great! Looking forward to seeing it!

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

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

[ Edited ]

By the way here is my full code now that really works very good! :smileyhappy:

 

void ApplicationUI::pad(QByteArray & in)
{
	int padLength = 16 - (in.length() % 16);

	for (int i = 0; i < padLength; ++i)
	{
		in.append((char) padLength);
	}
}

bool ApplicationUI::encrypt(QString filename, QString newfilename)
{
	QString key = BLOB_ENCRYPTION_KEY;
	_key 	= toHex(key.toUtf8());

	QByteArray buffer(16, 0);
	_iv = toHex(buffer);

	QFile* file = new QFile(filename);

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

	QByteArray in(file->readAll());
	pad(in);
	QByteArray out(in.length(), 0);

	if (crypt("ECB", true, in, out))
	{
		QFile* newFile = new QFile(newfilename);

		if (!newFile->open(QIODevice::WriteOnly))
		{
			qDebug() << "PROBLEM OPENING FILE: " + newfilename;
		}
		else
		{
			newFile->write(out);
		}

		newFile->close();
	}

	file->close();

	return false;
}

bool ApplicationUI::crypt(QString encryptionMode, bool isEncrypt, const QByteArray & in,QByteArray & out)
{
	QByteArray key, iv;
	QString fail;

	if (!fromHex(_key, key))
	{
		fail += "Key is not valid hex. ";
	}

	if (!fromHex(_iv, iv))
	{
		fail += "IV is not valid hex. ";
	}

	if (!fail.isEmpty())
	{
		qDebug() << fail;
		return false;
	}

	AESParams params(globalContext, encryptionMode);

	if (!params.isValid())
	{
		qDebug() << QString("Could not create params. %1").arg(SBError::getErrorText(params.lastError()));
		return false;
	}

	AESKey aesKey(params, key);

	if (!aesKey.isValid())
	{
		qDebug() << QString("Could not create a key. %1").arg(SBError::getErrorText(aesKey.lastError()));
		return false;
	}

	int rc;

	if (isEncrypt)
	{
		rc = hu_AESEncryptMsg(params.aesParams(), aesKey.aesKey(), iv.length(),
				(const unsigned char*) iv.constData(), in.length(),
				(const unsigned char *) in.constData(),
				(unsigned char *) out.data(), globalContext.ctx());
	}
	else
	{
		rc = hu_AESDecryptMsg(params.aesParams(), aesKey.aesKey(), iv.length(),
				(const unsigned char*) iv.constData(), in.length(),
				(const unsigned char *) in.constData(),
				(unsigned char *) out.data(), globalContext.ctx());
	}

	if (rc == SB_SUCCESS)
	{
		return true;
	}

	qDebug() << QString("Crypto operation failed. %1").arg(SBError::getErrorText(rc));
	return false;

}

void ApplicationUI::decrypt(QString filename, QString newfilename, QString encryptionMode, QString key, QString iv)
{
	if(encryptionMode == "ECB")
	{
		_key 	= toHex(key.toUtf8());

		QByteArray buffer(16, 0);
		_iv = toHex(buffer);
	}
	else if(encryptionMode == "CBC")
	{
		_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;
		showToast("Some error occured while downloading.");
	}
	else
	{
		QByteArray in(file->readAll());

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

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

				QFile* newFile = new QFile(newfilename);

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

					newFile->write(out);
				}

				newFile->close();
			}
		}
	}

	file->close();
}

bool ApplicationUI::removePadding(QByteArray & out)
{
	char paddingLength = out[out.length() - 1];

	if (paddingLength < 1 || paddingLength > 16)
	{
		qDebug() << "Invalid padding length. Were the keys good?";
		out.clear();
		return false;
	}

	if (paddingLength > out.length())
	{
		qDebug() << "Padding is claiming to be longer than the buffer!";
		out.clear();
		return false;
	}

	for (int i = 1; i < paddingLength; ++i)
	{
		char next = out[out.length() - 1 - i];

		if (next != paddingLength)
		{
			qDebug() << "Not all padding bytes are correct!";
			out.clear();
			return false;
		}
	}

	out.remove(out.length() - paddingLength, paddingLength);
	return true;
}

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;
}

bool ApplicationUI::fromHex(const QString in, QByteArray & toReturn)
{
	QString temp(in);
	temp.replace(" ","");
	temp.replace(":","");
	temp.replace(".","");

	QByteArray content(temp.toLocal8Bit());

	const char * c(content.constData());

	if (content.length() == 0 || ((content.length() % 2) != 0))
	{
		return false;
	}

	for (int i = 0; i < content.length(); i += 2)
	{
		char a = c[i];
		char b = c[i + 1];
		a = nibble(a);
		b = nibble(b);

		if (a < 0 || b < 0)
		{
			toReturn.clear();
			return false;
		}

		toReturn.append((a << 4) | b);
	}

	return true;
}

char ApplicationUI::nibble(char c) {
	if (c >= '0' && c <= '9') {
		return c - '0';
	} else if (c >= 'a' && c <= 'f') {
		return c - 'a' + 10;
	} else if (c >= 'A' && c <= 'F') {
		return c - 'A' + 10;
	}
	return -1;
}

I've also modified the AESParams.cpp

 

AESParams::AESParams(GlobalContext & g, QString encryptionMode) :
		Crypto("AESParams"), _globalContext(g), _aesParams(NULL)
{
	int rc;

	if(encryptionMode == "ECB")
	{
		rc = hu_AESParamsCreate(SB_AES_ECB, SB_AES_128_BLOCK_BITS, NULL, NULL, &_aesParams, _globalContext.ctx());
	}
	else if(encryptionMode == "CBC")
	{
		rc = hu_AESParamsCreate(SB_AES_CBC, SB_AES_128_BLOCK_BITS, NULL, NULL, &_aesParams, _globalContext.ctx());
	}

	maybeLog("AESParamsCreate", rc);
}

 

 

 

and this is how I call the function 

 

for ECB

 

Qt.app.decrypt(fileLocation, fileLocationReplace, "ECB", ENCRYPTIONKEY, "");

 

for CBC

 

Qt.app.decrypt(fileLocation, fileLocationReplace, "CBC", ENCRYPTIONKEY, IV);

 

for encrypting in ECB

 

Qt.app.encrypt(fileLocation, fileLocationReplace);

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

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

Thanks so much to 

Developer
strobejb
Posts: 282
Registered: ‎10-15-2012
My Device: bb10 developer

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


nemoryoliver wrote:

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.


Might want to consider changing your encryption key, now that it has been plastered all over the web ;-)

Developer
knobtviker
Posts: 145
Registered: ‎05-31-2013
My Device: BlackBerry Z10 Limited Edition

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

Doesn't matter. It's a temporary key provided from a server and used only once. It's already obsolete.

Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

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

Good to see! Thanks for sharing!

--
Rob is no longer associated with BlackBerry.