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

标签 c linux ssl openssl public-key-encryption

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

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

谢谢。

最佳答案

试试这个:

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

我没有测试,但应该可以。

关于c - openSSL:如何初始化公钥加密的 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10177916/

相关文章:

C 编程 : How to add up divisors of an integer n

php - MediaWiki 安全 : Alternate file ayout

mysql - ssl连接错误: unknown error number

c - 递归调用 pipeline() 后 I/O 挂起

c++ - Gcc 编译的可执行文件产生 "access denied"消息?

将二维数组传递给函数的正确方法

mysql - 从命令行连接时,用户访问被拒绝...

python - 在 Python 中写入共享内存非常慢

android - Android 4.3 上的 javax.net.ssl.SSLHandshakeException

ssl - 拒绝所有带有 nginx 和 ssl 的子域。