OpenSSL:已如何初始化公共密钥加密密钥?(openSSL: how to initialize

2019-07-29 00:38发布

对于使用OpenSSL API公共密钥加密,是如何给出* .key文件格式的私钥,并在* .PEM文件格式的公钥在C程序初始化密钥(公共和私有):

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

谢谢。

Answer 1:

试试这个:

        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...
        }

我没有测试,但应该工作。



文章来源: openSSL: how to initialize keys for public key encryption?