如何以整数形式给定公钥私钥对来创建.key文件

时间:2013-11-13 20:47:14

标签: openssl rsa

我正在执行密码分析,我有一个公钥文件,我使用它来计算整数形式的私钥。现在我想在openssl中使用它解密另一条消息,我需要创建私钥的.key文件。如何在给定私钥的情况下创建.key文件。感谢

1 个答案:

答案 0 :(得分:0)

为此,您可以将整数设置为大数,这基本上是OpenSSL的BIGNUM结构,然后分配给RSA结构。

现在,您可以将RSA写入档案。

RSA *pubkey = RSA_new();

/*
    RSA private key is basically d and n. You already have n as you have public key.
    You may have computed d or p and q.
    First read the private key from the file into a char * buffer say p.
    Depending on the fact whether your integer is in hex or decimal, you can use
    BN_hex2bn or BN_dec2bn.

    Depending on what you have computed d or p and q, assign it to RSA structure.
    I assume that you may have computed d.
*/

//Depending on the fact whether your integer is in hex or decimal, you can use    
int len = BN_hex2bn(&pubkey->d, (const char *)p);
// or int len = BN_hex2bn(&pubkey->d, (const char *)p);

if (len == 0 || p[len])
    fprintf(stderr, "'%s' does not appear to be a valid number.\n", p);

/*Now, you use private key. either to decrypt or sign.*/
/*Or write your private key using d2i_RSAPrivateKey_fp or using PEM 
  which you can search through OpenSSL.
*/