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
Contributor
Posts: 12
Registered: ‎02-19-2013
My Device: Z10
My Carrier: 123456789

Re: AES encryption/decryption is not working properly.

http://chanduthedev.blogspot.in/2013/06/bb10-cryptography-sample-applicationaes.html

 

please check this for sample working application for AES algorithm.

 

Let me know if you need any help.

 

Thanks,

Chanduthedev

New Developer
Posts: 50
Registered: ‎03-18-2009
My Device: 8530
My Carrier: Movilnet

Re: AES encryption/decryption is not working properly.

Hi thanks but your code has an issue. If the plain text has more than 16 characters it doesn't decrypt the complete text.
// Code for getting Thanks
if( iWasHelpful && solvedYourProblem ) {
Thanks++;
solution = true;
}
Contributor
Posts: 12
Registered: ‎02-19-2013
My Device: Z10
My Carrier: 123456789

Re: AES encryption/decryption is not working properly.

Yes you are right. Actually its a sample application. Many ppl are facing issue while decrypting, so I provide with simple example. I will upload complete working example for Triple DES in this week. Thanks for your inputs.

New Developer
Posts: 50
Registered: ‎03-18-2009
My Device: 8530
My Carrier: Movilnet

Re: AES encryption/decryption is not working properly.

Yes, there aren't a good guide or examples from BlackBerry to Decrypting, your example is really helpful, I did some changes and now it's working with a longer String.

 

QString AesAlgoSample::AESEncrypt(QString input)
{

	QByteArray ipBytes = input.toAscii();
	unsigned char* plainText = reinterpret_cast<unsigned char*>(ipBytes.data());

	int returnCode = SB_SUCCESS;
	QString initVector = "0000000000000000";

	int a = SB_AES_128_BLOCK_BYTES;
	int b = round(strlen((char*)plainText) /a);
	int c = a*b*2;
	qDebug()<<"c:  "<<c;
	returnCode = hu_AESEncryptMsg(aesParams, aesKey, initVector.size(), (unsigned char*)initVector.utf16(), c , plainText, cipherText, globalCtx);
	if(returnCode != SB_SUCCESS)
	{
		qDebug()<<"Err:  in hu_AESEncryptMsg returnCode is  "<<returnCode;
	}
	QByteArray byteData = QByteArray::fromRawData(reinterpret_cast<const char *>(cipherText), c);
	QString cipher = QString::fromAscii(byteData.toHex());

	return cipher;

}

 

The thing was replace the The length params (in bytes) of plaintext that must be a multiple of SB_AES_128_BLOCK_BYTES but it isn't have to be SB_AES_128_BLOCK_BYTES for that reason you are limiting String longer than SB_AES_128_BLOCK_BYTES (16 in bytes / 128 in bite) and for make it multiple of SB_AES_128_BLOCK_BYTES I did this:

 

int a = SB_AES_128_BLOCK_BYTES;
int b = round(strlen((char*)plainText) /a);
int c = a*b*2;

 And this part need to be chached too:

 

QByteArray byteData = QByteArray::fromRawData(reinterpret_cast<const char *>(cipherText), c);

You need to put the ciphertext and its length.

 

And this Note from the Api reference is important consider too:

 

"The plaintext and ciphertext buffers must be the same length, and may overlap in memory subject to the constraints described in the API Reference section on overlapping buffers."

 

The same thing happend with the Decrypting part.

// Code for getting Thanks
if( iWasHelpful && solvedYourProblem ) {
Thanks++;
solution = true;
}
Contributor
Posts: 12
Registered: ‎02-19-2013
My Device: Z10
My Carrier: 123456789

Re: AES encryption/decryption is not working properly.

Here is the sample BB10 application for Triple DES algorithm

 

http://chanduthedev.blogspot.in/2013/07/bb10-triple-des-crypto-sample-app.html

 

Let me know if you need any info regarding this.