【问题标题】:openSSL: how to initialize keys for public key encryption?openSSL:如何初始化公钥加密的密钥?
【发布时间】:2012-04-28 00:09:39
【问题描述】:

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

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

谢谢。

【问题讨论】:

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


    【解决方案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...
            }
    

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

    【讨论】:

    • 这存储了私钥,公钥呢,以及如何使用问题中提到的文件对其进行初始化?
    • EVP_PKEY *pkey; FILE *f = fopen("", "rb"); if (f == NULL){ // 错误处理... } //如果你的密钥是 PEM 编码的,使用这个 pkey = PEM_read_PUBKEY(f, NULL, NULL, NULL); // pkey 现在包含 pubKey。 //我们将 NULL 传递给其他参数,因为我们不需要密码来读取公钥 //如果您的密钥是 DER 编码的,请使用此 pkey = d2i_PUBKEY_fp(f, NULL); if (pkey == NULL){ // 错误处理... } 我没有测试,但应该可以。
    • .key 只是扩展名,而不是编码类型...我也是初学者,但我相信 .key 扩展名意味着证书是 PEM 编码的。见en.wikipedia.org/wiki/X.509#Certificate_filename_extensions
    猜你喜欢
    • 1970-01-01
    • 2020-03-15
    • 2013-06-09
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多