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
Developer
Charuta_Rajopadhye
Posts: 79
Registered: ‎08-01-2013
My Device: Z10, Q5, Q10
My Carrier: Vodaphone
Accepted Solution

RSA encryption using .cer file

Hi all,

  

I need to encrypt my data using RSA, with a public key stored in my file system. (assets)

like in iOS and android we have .cer file, which file format do i need to store in my file system, from which i'll extract the public key?

and is there any code snippet that i can refer to, to get the public key from the certificate for encryption?

 

Please help!

 

Regards.

Please use plain text.
BlackBerry Development Advisor (Retired)
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: RSA encryption using .cer file

Hi,

 

The easiest way to read a .cer is to use OpenSSL.

 

 X509 *x;
 x = PEM_read_bio_X509(bp, NULL, 0, NULL);
 if (x == NULL)
        {
        /* Error */
        }

 

See https://www.openssl.org/docs/crypto/pem.html

 

You can then use the public key in the certificate (via the OpenSSL API) for encrypt/sign operations.

 

Thanks!

--
Rob is no longer associated with BlackBerry.
Please use plain text.
Developer
Charuta_Rajopadhye
Posts: 79
Registered: ‎08-01-2013
My Device: Z10, Q5, Q10
My Carrier: Vodaphone

Re: RSA encryption using .cer file

how are we supposed to create the BIO object?

Will you please point me to some link? or some starting point from where i could learn using the functions documented here : http://developer.blackberry.com/native/reference/core/com.qnx.doc.crypto.lib_ref/topic/manual/about_...

 

Please help!!

 

Regards

Please use plain text.
BlackBerry Development Advisor (Retired)
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: RSA encryption using .cer file

Hi,

 

Not all of the functions require a BIO,

X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);

If you do need a BIO, the functions covering them are listed here:

https://www.openssl.org/docs/crypto/bio.html

 

Once the certificate is loaded, you could use the OpenSSL RSA functions without conversion.

 

If you want to use the hursa functions, you would need to convert the big numbers in the key to the unsigned char * used by hursa.

 

The AESCryptoSample at https://github.com/blackberry/Cascades-Community-Samples/tree/master/AESCryptoDemo shows most of the concepts needed for using the Security Builder contexts.

 

Thank you.

--
Rob is no longer associated with BlackBerry.
Please use plain text.
Developer
Charuta_Rajopadhye
Posts: 79
Registered: ‎08-01-2013
My Device: Z10, Q5, Q10
My Carrier: Vodaphone

Re: RSA encryption using .cer file

this is what i did to read the file :

 

QFile file(CERTIFICATE_PATH) ;
if(file.exists())
{
file.open(QIODevice::ReadOnly) ;
QByteArray certificateDataBytes = file.readAll() ;

int length = certificateDataBytes.size() ;
X509* certificate ;

const unsigned char *data = (unsigned char *)certificateDataBytes.data() ;

// length is the length of the certificateDataBytes in terms of bytes.
certificate = d2i_X509 (NULL, &data, length) ;

_publicKey = X509_get_pubkey (certificate) ;

RSA * rsa ;
rsa = EVP_PKEY_get1_RSA(_publicKey) ;

int maxSize = RSA_size(rsa) ;

char *inputPainText = convertToUStr(plainText) ;
unsigned char *encrypted = (unsigned char*) malloc (maxSize) ;
int bufferSize = RSA_public_encrypt(strlen(inputPainText), (unsigned char *) inputPainText, encrypted, rsa, RSA_PKCS1_PADDING) ;

 }

 

Thanks all :smileyhappy:

Please use plain text.
BlackBerry Development Advisor (Retired)
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: RSA encryption using .cer file

Great! Thank you for sharing the code.

 

Do you have cleanup code too?

--
Rob is no longer associated with BlackBerry.
Please use plain text.