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

Posts: 15
Registered: ‎10-10-2012
My Device: Blackberry 10 Dev Alpha
Accepted Solution

RSA encrypt in Blackberry10 C++

Hi. I'm new working with Blackberry10 and I have to encrypt a password with RSA. I've been searching in blackberry developers site and I've found the hursa.h security API. I have some experience working with Blackberry in Java and I know that in Java the first thing to do once you got the Module and the public exponent is to create the RSA publick Key object, e.g. RSAPublicKey publicKey = new RSAPublicKey(new RSACryptoSystem(2048), ebytes, mbytes); I'm trying to perform this exact task in the new BB10 platform with C++ and I've not been successful.
I've seen several methods in the page but none of them is properly documented, like this one hu_RSAKeySet(), which I think I should be using. I've tried to use it, but I know for sure that I'm missing something, I dont know If I should create an object of this hursa class or something like that. Right now I'm just including hursa into my class and trying to implement the method and the message I get is "undefined reference to `hu_RSAKeySet'"


Any help will be appreciated. Any at all. Like I said I'm not a Guru in RSA encrypting process but I've perfomed this ecrypting in old Blackberry java paltform and it wasn't this hard.


Thanks to all and best regards.

Posts: 15
Registered: ‎10-10-2012
My Device: Blackberry 10 Dev Alpha

Re: RSA encrypt in Blackberry10 C++

Resolved. After doing some research and getting to know better with BB10 coding and RSA, I finally came out with the solution to successfuly encrypt a plain text with RSA and Base64 in C/C++ for BB10 or any other platform. Please take in consideration that I have the modulus and the public exponent of the rsa object from the services that I'm working with.

The code:


QByteArray answer;

    RSA* rsa = RSA_new();

    BIGNUM *modulus = BN_new();
    BIGNUM *exponent = BN_new();
    constchar*modulusString = rsaObj->getM();//My Modulus
    constchar*exponentString = rsaObj->getE();//My exponent

    BN_hex2bn(&modulus, modulusString);
    BN_hex2bn(&exponent, exponentString);

    rsa->n = BN_new();
    BN_copy(rsa->n, modulus);
    rsa->e = BN_new();
    BN_copy(rsa->e, exponent);

    int maxSize = RSA_size(rsa);
    qDebug()<<"maxSize:"<< maxSize;

    constchar*inn ="1234";
    unsignedchar*encrypted =(unsignedchar*) malloc(maxSize);
    int bufferSize = RSA_public_encrypt(strlen(inn),(unsignedchar*) inn,
            encrypted, rsa, RSA_PKCS1_PADDING);

    if(bufferSize ==-1){

    QByteArray enc =QByteArray::fromRawData((constchar*) encrypted,256);
    answer = enc.toBase64();

    return answer;


Thanks and I expect this helps to the new BB10 developers

New Contributor
Posts: 3
Registered: ‎03-04-2013
My Device: NA

Re: RSA encrypt in Blackberry10 C++


  Thank you so much for the post.  I am trying to utilize the same RSA object, but seem to have an issue.  I keep getting an error on RSA_public_encrypt, and for the variable you have as maxSize I get zero (not sure if that is expected).  I was guessing maybe I was missing a link to a library or initialization of some other variable.  Or possible the format I am using for BN_hex2bn isn't correct.  The format I was using was soemthing like the example below, any ideas that might help?


modulusString = "25 c2 ba..." total length 482

exponentString = "fa 11 ..."   total length 383

Posts: 15
Registered: ‎10-10-2012
My Device: Blackberry 10 Dev Alpha

Re: RSA encrypt in Blackberry10 C++

Hi mhant,


The includes that you must have are the following:


#include <openssl/opensslconf.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
#include "openssl/rsa.h"
#include "openssl/aes.h"
#include "openssl/opensslconf.h"
#include "openssl/engine.h"
#include "openssl/pem.h"
#include "openssl/rc4.h"



Things I'm seing different in our codes:


1) I have my modulus set as follows: 

m = (const char *) "00B4B0..."  //total lenght 514

(no spaces) 


2) I have my exponent set as follows:

e = (const char *) "0100..";  //total length 6

(no spaces) 



In order to help you a little bit more I recommend that you post your code. Another thing to keep in mind is the size of the message that your are trying to encrypt, the theoretical max length of the message should be 256 characters long including the null terminator.


For further information you could check this site http://shanetully.com/2012/04/simple-public-key-encryption-with-rsa-and-openssl/

New Contributor
Posts: 4
Registered: ‎06-08-2011
My Device: Storm

Re: RSA encrypt in Blackberry10 C++

Also need to adding in the .pro file:


LIBS += -lcrypto
Posts: 84
Registered: ‎04-22-2013
My Device: BB 10 Dev

Re: RSA encrypt in Blackberry10 C++

[ Edited ]

Ooops sorry for the interrupt, previously i was having prob in getting the correct encrypted password  because in this line

QByteArray enc = QByteArray::fromRawData((const char*) encrypted, 256);

it prints out a length of 512 string for me. hence i need to changed it to 128 in order to get a length of 256 chars.

Hope this helps others that having the same prob as mine.


Anyway thanks for the hard work.