C语言实现rsa数字签名代码和验签
时间: 2023-10-26 08:24:07 浏览: 995
RSA数字签名是一种常用的数字签名算法,实现代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
// 生成RSA密钥对
int generate_key(char *pub_key_file, char *pri_key_file, int key_len)
{
int ret = 0;
RSA *rsa = NULL;
BIGNUM *bne = NULL;
BIO *bp_public = NULL, *bp_private = NULL;
char *pub_key = NULL, *pri_key = NULL;
// 生成RSA密钥对
bne = BN_new();
ret = BN_set_word(bne, RSA_F4);
if (ret != 1) {
printf("BN_set_word failed\n");
return -1;
}
rsa = RSA_new();
ret = RSA_generate_key_ex(rsa, key_len, bne, NULL);
if (ret != 1) {
printf("RSA_generate_key_ex failed\n");
return -1;
}
// 将密钥对输出到文件
bp_public = BIO_new(BIO_s_file());
bp_private = BIO_new(BIO_s_file());
ret = BIO_write_filename(bp_public, pub_key_file);
if (ret <= 0) {
printf("BIO_write_filename public failed\n");
return -1;
}
ret = PEM_write_bio_RSAPublicKey(bp_public, rsa);
if (ret != 1) {
printf("PEM_write_bio_RSAPublicKey failed\n");
return -1;
}
ret = BIO_write_filename(bp_private, pri_key_file);
if (ret <= 0) {
printf("BIO_write_filename private failed\n");
return -1;
}
ret = PEM_write_bio_RSAPrivateKey(bp_private, rsa, NULL, NULL, 0, NULL, NULL);
if (ret != 1) {
printf("PEM_write_bio_RSAPrivateKey failed\n");
return -1;
}
RSA_free(rsa);
BN_free(bne);
BIO_free_all(bp_public);
BIO_free_all(bp_private);
return 0;
}
// RSA数字签名
int rsa_sign(char *data, int data_len, char *pri_key_file, char *signature, int *sig_len)
{
int ret = 0;
RSA *rsa = NULL;
FILE *fp = NULL;
char *pri_key = NULL;
// 读取密钥文件
fp = fopen(pri_key_file, "rb");
if (fp == NULL) {
printf("open private key file failed\n");
return -1;
}
rsa = PEM_read_RSAPrivateKey(fp, &rsa, NULL, NULL);
if (rsa == NULL) {
printf("PEM_read_RSAPrivateKey failed\n");
return -1;
}
// RSA数字签名
ret = RSA_sign(NID_sha256, (unsigned char *)data, data_len, (unsigned char *)signature, (unsigned int *)sig_len, rsa);
if (ret != 1) {
printf("RSA_sign failed\n");
return -1;
}
RSA_free(rsa);
fclose(fp);
return 0;
}
// RSA数字验签
int rsa_verify(char *data, int data_len, char *pub_key_file, char *signature, int sig_len)
{
int ret = 0;
RSA *rsa = NULL;
FILE *fp = NULL;
char *pub_key = NULL;
// 读取密钥文件
fp = fopen(pub_key_file, "rb");
if (fp == NULL) {
printf("open public key file failed\n");
return -1;
}
rsa = PEM_read_RSAPublicKey(fp, &rsa, NULL, NULL);
if (rsa == NULL) {
printf("PEM_read_RSAPublicKey failed\n");
return -1;
}
// RSA数字验签
ret = RSA_verify(NID_sha256, (unsigned char *)data, data_len, (unsigned char *)signature, sig_len, rsa);
if (ret != 1) {
printf("RSA_verify failed\n");
return -1;
}
RSA_free(rsa);
fclose(fp);
return 0;
}
```
使用示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int ret = 0;
char *pub_key_file = "rsa_public_key.pem";
char *pri_key_file = "rsa_private_key.pem";
char *data = "Hello, World!";
char signature[1024] = {0};
int sig_len = 0;
// 生成RSA密钥对
ret = generate_key(pub_key_file, pri_key_file, 2048);
if (ret != 0) {
printf("generate_key failed\n");
return -1;
}
// RSA数字签名
ret = rsa_sign(data, strlen(data), pri_key_file, signature, &sig_len);
if (ret != 0) {
printf("rsa_sign failed\n");
return -1;
}
printf("signature: ");
for (int i = 0; i < sig_len; i++) {
printf("%02x", signature[i]);
}
printf("\n");
// RSA数字验签
ret = rsa_verify(data, strlen(data), pub_key_file, signature, sig_len);
if (ret != 0) {
printf("rsa_verify failed\n");
return -1;
}
printf("verify success\n");
return 0;
}
```
阅读全文