C PEM_write_PrivateKey()函数未将RSA私钥存储在private.PEM文件中
这是我正在使用的代码,它引用了其他用户从中提出的堆栈溢出问题。但是当我试图使用PEM_write_PrivateKey()函数将私钥写入文件时。它没有这样做。此函数调用后,控制台屏幕将自动关闭。private.pem文件不包含任何内容C PEM_write_PrivateKey()函数未将RSA私钥存储在private.PEM文件中,c,cryptography,openssl,rsa,pem,C,Cryptography,Openssl,Rsa,Pem,这是我正在使用的代码,它引用了其他用户从中提出的堆栈溢出问题。但是当我试图使用PEM_write_PrivateKey()函数将私钥写入文件时。它没有这样做。此函数调用后,控制台屏幕将自动关闭。private.pem文件不包含任何内容 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/evp.h> #include <openssl/rsa
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/objects.h>
#include <openssl/err.h>
#include <openssl/x509.h>
int main()
{
//
// Local variables definition
//
EVP_PKEY_CTX *evp_ctx = NULL;
EVP_PKEY *ppkey = NULL;
FILE *fpPri = NULL;
FILE *fpPub = NULL;
int retValue = 1;
for (;;)
{
//
// Function allocates public key algorithm context using the algorithm
// specified by id
//
evp_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if (NULL == evp_ctx)
{
printf("RSA Public key algorithm context is not allocated\n");
break;
} // if
printf("RSA Public key algoritm context allocated\n");
//
// Function initializes a public key algorithm context using key pkey
// for a key genration operation
//
retValue = EVP_PKEY_keygen_init(evp_ctx);
if (1 != retValue)
{
printf("Initialization of public key alogorithm context failed\n");
break;
} // if
printf("Public key alogorithm context initialized\n");
//
// Setting RSA key bit to 2048
//
retValue = EVP_PKEY_CTX_set_rsa_keygen_bits(evp_ctx, 2048);
if (1 != retValue)
{
printf("RSA key bits not set to 2048\n");
break;
} // if
printf("RSA key bits set to 2048\n");
//
// Function performs a key generation operation
//
retValue = EVP_PKEY_keygen(evp_ctx, &ppkey);
if (1 != retValue)
{
printf("Key generation operation failed\n");
break;
} // if
printf("Key generated successfully\n");
//
// Creating a file to store RSA private key
//
fpPri = fopen("./private.pem", "w+");
if (NULL == fpPri)
{
printf("File pointer of private.pem file is not opened\n");
break;
} // if
printf("File pointer or private.pem file opened\n");
retValue = PEM_write_PrivateKey(fpPri, ppkey, NULL, NULL, 0, 0, NULL);
if (1 != retValue)
{
printf("Private key is not written to file private.pem\n");
break;
} // if
printf("Private key written to file private.pem\n");
//
// Final break statement
//
break;
} // for
//
// Releasing all the memory allocations and the handles
//
getchar();
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
//
//局部变量定义
//
EVP_PKEY_CTX*EVP_CTX=NULL;
EVP_PKEY*ppkey=NULL;
文件*fpPri=NULL;
FILE*fpPub=NULL;
int-retValue=1;
对于(;;)
{
//
//函数使用该算法分配公钥算法上下文
//由id指定
//
evp_ctx=evp_PKEY_ctx_new_id(evp_PKEY_RSA,NULL);
如果(NULL==evp_ctx)
{
printf(“未分配RSA公钥算法上下文\n”);
打破
}//如果
printf(“RSA公钥算法上下文分配\n”);
//
//函数使用密钥pkey初始化公钥算法上下文
//用于密钥生成操作
//
retValue=EVP\u PKEY\u keygen\u init(EVP\u ctx);
如果(1!=retValue)
{
printf(“公钥alogorithm上下文初始化失败\n”);
打破
}//如果
printf(“公钥alogorithm上下文已初始化\n”);
//
//将RSA密钥位设置为2048
//
retValue=EVP\u PKEY\u CTX\u set\u rsa\u keygen\u位(EVP\u CTX,2048);
如果(1!=retValue)
{
printf(“RSA密钥位未设置为2048\n”);
打破
}//如果
printf(“RSA密钥位设置为2048\n”);
//
//函数执行密钥生成操作
//
retValue=EVP_PKEY_keygen(EVP_ctx和ppkey);
如果(1!=retValue)
{
printf(“密钥生成操作失败\n”);
打破
}//如果
printf(“成功生成密钥\n”);
//
//创建文件以存储RSA私钥
//
fpPri=fopen(“/private.pem”,“w+”);
if(NULL==fpPri)
{
printf(“private.pem文件的文件指针未打开\n”);
打破
}//如果
printf(“文件指针或private.pem文件已打开\n”);
retValue=PEM_write_PrivateKey(fpPri,ppkey,NULL,NULL,0,0,NULL);
如果(1!=retValue)
{
printf(“私钥未写入文件Private.pem\n”);
打破
}//如果
printf(“写入Private.pem文件的私钥”);
//
//最后中断语句
//
打破
}//为了
//
//释放所有内存分配和句柄
//
getchar();
返回0;
}
可能是因为您从未调用过SSL\u library\u init()
此外,在程序退出之前,缓冲文件可能不会被刷新。在程序结束前,您应调用:
fflush(fpPri);
可能是因为您从未调用过SSL\u library\u init() 此外,在程序退出之前,缓冲文件可能不会被刷新。在程序结束前,您应调用:
fflush(fpPri);
我不确定这是否与您的问题有关,但我遇到了一个类似的问题,并在试图找出问题所在时出现在本页上。所以我想我会分享我的发现,希望它能帮助别人。我在其他一些论坛上也看到,人们遇到了与我相同的问题 问题:在使用Visual Studio 2015编译的应用程序上调用PEM_write_PrivateKey时,应用程序将在没有任何错误代码的情况下退出。使用VS2010编译时,相同的应用程序和代码可以正常工作 我第一次发现微软在VS2015中对文件句柄做了一些突破性的更改 文件封装在以前的版本中,文件类型完全相同 在中定义,因此用户代码可以进入 文件并修改其内部结构。stdio库已更改为 隐藏实现细节。作为本文件的一部分,请参阅中定义的文件 现在是不透明类型,其成员无法从中访问 在CRT本身之外。 通过在OpenSSL中进行调试,我发现处理程序“APPLINK_FSETMOD”被设置为“unsupported”。这让我阅读了OpenSSL中的AppLink技术实现 这让我发现解决问题的办法是在我的应用程序中添加以下代码
#include "openssl/applink.c"
重要提示:您必须将include放入.exe源文件之一,因为GetModuleHandle被传递为NULL。我不确定这是否与您的问题有关,但我遇到了类似的问题,并在尝试找出问题所在时出现在本页上。所以我想我会分享我的发现,希望它能帮助别人。我在其他一些论坛上也看到,人们遇到了与我相同的问题 问题:在使用Visual Studio 2015编译的应用程序上调用PEM_write_PrivateKey时,应用程序将在没有任何错误代码的情况下退出。使用VS2010编译时,相同的应用程序和代码可以正常工作 我第一次发现微软在VS2015中对文件句柄做了一些突破性的更改 文件封装在以前的版本中,文件类型完全相同 在中定义,因此用户代码可以进入 文件并修改其内部结构。stdio库已更改为 隐藏实现细节。作为本文件的一部分,请参阅中定义的文件 现在是不透明类型,其成员无法从中访问 在CRT本身之外。 通过在OpenSSL中进行调试,我发现处理程序“APPLINK_FSETMOD”被设置为“unsupported”。这让我阅读了OpenSSL中的AppLink技术实现 这让我发现解决问题的办法是在我的应用程序中添加以下代码
#include "openssl/applink.c"
重要提示:您必须将include放入.exe源文件之一,因为GetM