Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用我的pkcs#8文件获取ecdsa字符串_C_Openssl_Ecdsa - Fatal编程技术网

如何使用我的pkcs#8文件获取ecdsa字符串

如何使用我的pkcs#8文件获取ecdsa字符串,c,openssl,ecdsa,C,Openssl,Ecdsa,我正在尝试从文件PKCS#8本地获取签名字符串,该文件为.p8格式。当我打开它时,我可以看到语句之间有一个字符串 -----BEGIN PRIVATE KEY----- // strings line 64 characters // strings line 64 characters // strings line 64 characters // strings line 8 characters -----END PRIVATE KEY----- 我想转换此文件并登录ecdsa以获取

我正在尝试从文件PKCS#8本地获取签名字符串,该文件为.p8格式。当我打开它时,我可以看到语句之间有一个字符串

 -----BEGIN PRIVATE KEY-----
// strings line 64 characters
// strings line 64 characters
// strings line 64 characters
// strings line 8 characters
-----END PRIVATE KEY-----
我想转换此文件并登录ecdsa以获取签名


如何使用openssl在c语言中实现这一点使用
PEM_Read_PrivateKey()
(或
PEM_Read_bio_PrivateKey()
)读取PKCS 8文件中的密钥数据。这将为您提供作为
EVP_PKEY
对象的密钥。这些函数的文档如下:

通常在签名时,您通常希望先使用一些摘要函数(例如SHA256)对要签名的数据进行摘要,然后再使用签名操作(在本例中为ECDSA)。假设您希望这样做,您应该使用
EVP\u DigestSign*
函数系列。这些函数的文档如下:

代码可能如下所示(未经测试):

EVP_PKEY *pkey = PEM_read_PrivateKey(myfile, NULL, NULL, NULL);
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
size_t siglen = 0;
unsigned char *sig;

if (mdctx == NULL || pkey == NULL)
    goto err;

if (!EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), NULL, pkey))
    goto err;

 if(!EVP_DigestSignUpdate(mdctx, tobesigned, tobesignedlen))
    goto err;

 /* Find out the signature length */
 if(!EVP_DigestSignFinal(mdctx, NULL, &siglen))
    goto err;

 /* Allocate memory for the signature length */
 sig = OPENSSL_malloc(siglen);
 if (sig == NULL)
     goto err;

 /* Now get the signature */
 if(!EVP_DigestSignFinal(mdctx, sig, &siglen))
     goto err;

 EVP_MD_CTX_free(mdctx);
 EVP_PKEY_free(pkey);
/* Free "sig" when you've finished with it */