openSSL:如何初始化公钥加密的密钥?

时间:2012-04-16 16:23:38

标签: c linux ssl openssl public-key-encryption

对于使用openSSL API进行公钥加密,如何在C程序中初始化密钥(公共和私有),给定* .key文件格式的私钥和* .pem文件格式的公钥:

 EVP_PKEY *key;
 /* How is key initialized ?
  */
  ctx = EVP_PKEY_CTX_new(key);

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

        EVP_PKEY *pkey;
        FILE *f = fopen("<path for your PEM or DER encoded key>", "rb");
        if (f == NULL){
                // error handling...
        }
    //if your key is PEM encoded use this
        pkey = PEM_read_PUBKEY(f, NULL, NULL, NULL); // pkey now contains the pubKey. 
    //We are passing NULL to the others parameters because we dont need password to read a public key

    //if your key is DER encoded use this
        pkey = d2i_PUBKEY_fp(f, NULL);

        if (pkey == NULL){
                // error handling...
        }

我没有测试但应该工作。

相关问题