根据模数初始化公钥使用OpenSSL的指数 [英] Initialize Public Key From Modulus & Exponent using OpenSSL
问题描述
尝试在给定API模数和指数的情况下生成RSA公钥.我在iOS 4.2上使用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-----
基于@James注释,我能够编写公共pem,但获得空白的私钥.这是我的代码:
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);
推荐答案
To do this make sure you have the OpenSSL library linked (instructions here http://code.google.com/p/ios-static-libraries/)
建立链接后,您将可以访问多个BIGNUM转换器.我使用BN_hex2bn方法将模数转换为十六进制,将十六进制字符串保存为指数"
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'
然后创建BIGNUM结构并使用RSA_public_encrypt加密
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;
祝你好运!
这篇关于根据模数初始化公钥使用OpenSSL的指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!