如何在给定整数形式的公钥对的情况下创建.key文件



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

为此

,您可以创建整数到大数字,这基本上是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.
*/

相关内容

最新更新