Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
798 views
in Technique[技术] by (71.8m points)

objective c - Initialize Public Key From Modulus & Exponent using OpenSSL

Trying to generate an RSA Public Key given an APIs modulus and exponent. I'm using OpenSSL on iOS 4.2.

Generating the public key manually is an option (see below) however i'm not sure how to include the exponent logic in the modulus

-----BEGIN PUBLIC KEY-----
Modulus from API
-----END PUBLIC KEY-----

Based on @James comments, I am able to write public pem but getting blank private key. Here is my code:

char szModulus = "1162" ;
char *szExp = "827655" ;
RSA* rsa = RSA_new();
int ret = BN_hex2bn(&rsa->n,szModulus) ;
ret = BN_hex2bn(&rsa->d,szExp) ;
FILE *fp = fopen("/Users/ysi/Desktop/privateKey.pem", "wb"); 
PEM_write_RSAPrivateKey(fp, rsa, NULL, NULL, 0, 0, NULL);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To do this make sure you have the OpenSSL library linked (instructions here http://code.google.com/p/ios-static-libraries/)

Once linked you'll have access to several BIGNUM converters. I turned the modulus into hex using the BN_hex2bn method saving the hex string into 'exponent'

Then create the BIGNUM struct and encrypt using RSA_public_encrypt

RSA *rsa = NULL;

rsa->n = BN_new();
BN_copy(rsa->n,modulus);   
rsa->e = BN_new();
BN_copy(rsa->e,exponent);     
rsa->iqmp=NULL;
rsa->d=NULL;
rsa->p=NULL;
rsa->q=NULL;

Good Luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...