从模数初始化公钥 &使用 OpenSSL 的指数 [英] Initialize Public Key From Modulus & Exponent using OpenSSL

查看:29
本文介绍了从模数初始化公钥 &使用 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);

推荐答案

为此,请确保您已链接 OpenSSL 库(此处的说明 http://code.google.com/p/ios-static-libraries/)

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;

祝你好运!

这篇关于从模数初始化公钥 &amp;使用 OpenSSL 的指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆