1

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

4

1 回答 1

0

为此,您可以将整数创建为基本上BIGNUM是 OpenSSL 结构的大数,然后分配给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.
*/
于 2013-11-14T03:53:05.147 回答