c - 使用 OpenSSL 进行 DSA 签名

标签 c load openssl key dsa

我正在尝试使用来自 OpenSSL 的 DSA 进行签名。我有包含公钥和私钥的文件。

首先,我建立了一个单播连接,一切正常。之后,我需要一个多播 UDP 连接,并且我想对数据包进行签名。我正在尝试使用函数 PEM_read_DSA_PUBKEY() 从我的证书中加载我的公钥,但它不起作用。它总是返回 NULL 而不是 DSA 结构。

这里是代码的简化版本。我这样编译:

gcc -Wall -g -lm prueba.c -o prueba -lcrypto

有什么想法吗?谢谢!

#include <stdio.h>
#include <openssl/dsa.h>
#include <openssl/pem.h>

int main()
{
    FILE *DSA_cert_file = fopen("./certs/cert.pem", "r");
    if (DSA_cert_file == NULL)
        return 1;

    printf("Certificate read\n");

    DSA *dsa = DSA_new();
    if((dsa = PEM_read_DSA_PUBKEY(DSA_cert_file, 0, 0, 0)) == NULL)
        return 1;

    printf("DSA public key read\n");

    return 0;
}

最佳答案

您是否使用受密码保护的公钥?

如果是,您需要将回调函数作为第三个参数传递给 PEM_read_DSA_PUBKEY,因此如果提供的密码匹配,它将能够正确加载您的 key 。

更新:

或者,如 Hasturkun 所指出的那样,您可以传递一个以 null 结尾的字符串作为第四个参数。引用 official documentation :

If the cb parameters is set to NULL and the u parameter is not NULL then the u parameter is interpreted as a null terminated string to use as the passphrase. If both cb and u are NULL then the default callback routine is used which will typically prompt for the passphrase on the current terminal with echoing turned off.

关于c - 使用 OpenSSL 进行 DSA 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5867939/

相关文章:

c - 初始化结构时发生访问冲突

PHP - 从数据库加载/删除是否使用静态方法?

ruby-on-rails-3 - 如何通过rails控制台加载openssl?

c++ - openssl RAND_add() 文档引用 RFC1750。 RFC1750 对此事保持沉默

c - 打印链表时出现段错误

c - 链接共享库时的问题

c - 在堆栈上创建 char** 缓冲区的更好方法?

java - 如何仅将部分图像用于 LWJGL

pdf - 在某一点打开本地 PDF 文件

php - 使用VB.net解密AES256加密字符串