02-19-2013 04:33 AM
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?
02-23-2013 05:03 PM
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);
02-23-2013 06:12 PM
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!
02-28-2013 01:54 AM
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\\
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.
04-03-2013 09:18 AM
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.
04-19-2013 11:29 AM
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."
3 weeks ago
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!!!
2 weeks ago
Hi Guys,
Could you share any sample working code?
yesterday
guys,
Can someone please post a working code ??
I tried the same, but it's not working.
Need urgently.
Thanks,
Naveen
yesterday