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

AES encryption/decryption is not working properly.

I am trying to implement cryptographic algorithms for BB10. And I started working on AES encryption/decryption. Below is my code. All functions returning success, but decryption of the cipher text should return actual plain text. But I am not getting the plain text as result for decryption method.

 

 

unsigned char keyValue[17]="passphrasetestss";
const unsigned char plainText[42] = "kony";
unsigned char cipherText[42] = "";
unsigned char iv[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned char plaintextResult[42] = "";


sb_GlobalCtx globalCtx;
hu_GlobalCtxCreateDefault(&globalCtx);
int result = hu_RegisterSbg56(globalCtx);
int seedResult = hu_RegisterSystemSeed(globalCtx);
int initResult = hu_InitSbg56(globalCtx);
sb_Params aesParams;

int aesParamsResult = hu_AESParamsCreate(SB_AES_CFB128, SB_AES_128_BLOCK_BITS, NULL, NULL, &aesParams, globalCtx);
sb_Key aesKey;
int aesKeyResult = hu_AESKeySet(aesParams, SB_AES_128_BLOCK_BITS, keyValue, &aesKey, globalCtx);
sb_Context aesContext;

int aesBeginResult = hu_AESBegin(aesParams, aesKey, SB_AES_128_BLOCK_BYTES, iv, &aesContext, globalCtx);

int aesEncryptResult = hu_AESEncrypt(aesContext, SB_AES_128_BLOCK_BYTES, plainText, cipherText, globalCtx);

 

Can some one find me where I am doing wrong?

Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: AES encryption/decryption is not working properly.

Try the following code, which seems to do something.   I'm struggling with a similar problem myself and not resolved it, but the following seems to work for your situation. 

 

A couple of things:

a) I think you need to give the encryption and decryption a full block (in AES's case, 16 byte blocks)

b) I thin the second parameter in the Encrypt and Decrypt is the number of bytes to be processed.  So

SB_AES_128_BLOCK_BYTES is probably not correct.

 

Anyway, have a look at my code, test it out and see if you figure out what you did wrong.  Then tell me because my routine is encypting nothing....

 

	unsigned char keyValue[17]="passphrasetestss";
	const unsigned char plainText[49] = "kony";
	unsigned char cipherText[49] = "";
	unsigned char iv[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	unsigned char plaintextResult[48] = "";


	sb_GlobalCtx globalCtx;
	hu_GlobalCtxCreateDefault(&globalCtx);
	int result = hu_RegisterSbg56(globalCtx);
	int initResult = hu_InitSbg56(globalCtx);

	sb_Params aesParams;
	int aesParamsResult = hu_AESParamsCreate(SB_AES_CFB128, SB_AES_128_BLOCK_BITS, NULL, NULL, &aesParams, globalCtx);

	sb_Key aesKey;
	int aesKeyResult = hu_AESKeySet(aesParams, SB_AES_128_BLOCK_BITS, keyValue, &aesKey, globalCtx);

	sb_Context aesContext;
	int aesBeginResult = hu_AESBegin(aesParams, aesKey, SB_AES_128_BLOCK_BYTES, iv, &aesContext, globalCtx);

	int aesEncryptResult = hu_AESEncrypt(aesContext, 48, plainText, cipherText, globalCtx);

	sb_Params aesParams2;
	int aesParamsResult2 = hu_AESParamsCreate(SB_AES_CFB128, SB_AES_128_BLOCK_BITS, NULL, NULL, &aesParams2, globalCtx);

	sb_Context aesContext2;
	int aesBeginResult2 = hu_AESBegin(aesParams2, aesKey, SB_AES_128_BLOCK_BYTES, iv, &aesContext2, globalCtx);

	int aesDecryptResult = hu_AESEncrypt(aesContext2, 48, cipherText, plaintextResult, globalCtx);

 

Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: AES encryption/decryption is not working properly.

As you were - it was my logging that was not working correctly that made me think the encryption/decryption was failing.  I am going! 

 

Anyway, hopefully my code will help you sort out your problem because your code helped me with mine!

Contributor
chanduthedev
Posts: 12
Registered: ‎02-19-2013
My Device: Z10

Re: AES encryption/decryption is not working properly.

Yes. Now its working fine. But the issue is, after decryption I am getting only first 4 characters. Below is the output for the plaintext 'jrking'.

 

Name : ptext
    Details:"j\\000r\\000k\\000i\\000\\061\\b\320#1\\b\330\275\\004\\b\320#1\\b\360\2710\\b\\377\\377\\377\\377\\001\324G\\ap\361\\"\\b\\001\\000\\000\\000\204\322\\004\\b"
    Default:0x747d40e
    Decimal:122147854
    Hex:0x747d40e
    Binary:111010001111101010000001110

 

And in the Details, it is showing '\\000' after each character, how to avoid this?. Because of this I cant able to print more than one character.

Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: AES encryption/decryption is not working properly.

Missed the previous post - retreated back to Java coding for a bit!  Now back on BB10! 

 

If the previous poster still has an issue can you please post the code that you used to print the Details:.

 

Also be aware that you must, when using AES, compress 16 bytes at a time.  This means you must implement some padding, which you can then remove after decryption. 

Developer
Shwarup
Posts: 93
Registered: ‎08-18-2011
My Device: 9550

Re: AES encryption/decryption is not working properly.

Hi Peter,

 

could you provide some more details on how to implement it.

 

"when using AES, compress 16 bytes at a time. This means you must implement some padding, which you can then remove after decryption." 

 

Thanks,

Shwarup Biswas
Contributor
chanduthedev
Posts: 12
Registered: ‎02-19-2013
My Device: Z10

Re: AES encryption/decryption is not working properly.

Now I can able to encrypt and decrypt successfully. The mistake I have done was, I was not used hu_AESParamsCreate, hu_AESBegin API's in decryption. You have to call these API's in decrypt method also.

 

Thanks a lot!!!

Developer
Shwarup
Posts: 93
Registered: ‎08-18-2011
My Device: 9550

Re: AES encryption/decryption is not working properly.

Hi Guys,

 

Could you share any sample working code?

Thanks,

Shwarup Biswas
Visitor
naveensharma423
Posts: 1
Registered: ‎05-23-2013
My Device: Developer

Re: AES encryption/decryption is not working properly.

guys, 
 Can someone please post a working code ??
 I tried the same, but it's not working. 
 Need urgently. 

Thanks,

 Naveen

Developer
Shwarup
Posts: 93
Registered: ‎08-18-2011
My Device: 9550

Re: AES encryption/decryption is not working properly.

Try this exmple

 

http://saju.net.in/code/misc/openssl_aes.c.txt

Thanks,

Shwarup Biswas