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

查看:197
本文介绍了根据模数初始化公钥使用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库(此处的说明

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屋!

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