openSSL: how to initialize keys for public key encryption?

南楼画角 提交于 2019-12-31 07:04:09

问题


For using openSSL API for public key encryption, how is the key (public & private) initialized in a C program, given private key in *.key file format, and public key in *.pem file format:

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

Thanks.


回答1:


try this:

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

I didnt test but should work.



来源:https://stackoverflow.com/questions/10177916/openssl-how-to-initialize-keys-for-public-key-encryption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!