2012-04-16 125 views
0

对于使用OpenSSL API公共密钥加密,如何是关键(公共&私人)初始化在C程序中,鉴于* .PEM文件格式* .key文件格式的私钥和公钥:openSSL:如何初始化公钥加密密钥?

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

谢谢。

回答

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

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

+0

这存储私钥,公钥如何,以及如何使用问题中提到的文件初始化? – Jake 2012-04-16 16:33:18

+0

EVP_PKEY *pkey; FILE *f = fopen("", "rb"); if (f == NULL){ // error handling... } \t //if your key is PEM encoded use this pkey = PEM_read_PUBKEY(f, NULL, NULL, NULL); // pkey now contains the pubKey. \t //We are passing NULL to the others parameters because we dont need password to read a public key \t //if your key is DER encoded use this pkey = d2i_PUBKEY_fp(f, NULL); if (pkey == NULL){ // error handling... } 我没有测试,但应该工作。 – Giovani 2012-04-16 16:49:54

+0

如果密钥是* .key格式,该怎么办? – Jake 2012-04-16 17:09:58