windows下C语言使用openssl库生成rsa密钥对进行加解密并给出源代码:1、包含生成密钥对函数,将密钥对写到文件中;2、包含公钥加密、解密函数;3、包含私钥加密、解密函数
时间: 2023-07-10 16:16:53 浏览: 106
好的,以下是使用openssl库在Windows下用C语言生成RSA密钥对并进行加解密的源代码,包含了生成密钥对函数,将密钥对写到文件中,公钥加密解密函数,私钥加密解密函数。需要注意的是,以下代码需要在包含openssl库的环境下编译运行。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#define KEY_LENGTH 2048
#define PUB_EXP 3
#define MESSAGE_SIZE 1024
// 生成RSA密钥对并写入文件
void generate_keypair_and_write_to_file(char *pubkey_file, char *prikey_file)
{
RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);
// 写入公钥
FILE *pubkey_fp = fopen(pubkey_file, "wb");
if(pubkey_fp == NULL)
{
printf("无法写入公钥文件\n");
exit(EXIT_FAILURE);
}
PEM_write_RSA_PUBKEY(pubkey_fp, keypair);
fclose(pubkey_fp);
// 写入私钥
FILE *prikey_fp = fopen(prikey_file, "wb");
if(prikey_fp == NULL)
{
printf("无法写入私钥文件\n");
exit(EXIT_FAILURE);
}
PEM_write_RSAPrivateKey(prikey_fp, keypair, NULL, NULL, 0, NULL, NULL);
fclose(prikey_fp);
RSA_free(keypair);
}
// 公钥加密
void public_encrypt(char *plaintext, char *ciphertext, char *pubkey_file)
{
FILE *file = fopen(pubkey_file, "rb");
if(file == NULL)
{
printf("无法打开公钥文件\n");
exit(EXIT_FAILURE);
}
RSA *rsa = RSA_new();
rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL);
fclose(file);
unsigned char encrypted[MESSAGE_SIZE];
int encrypted_len = RSA_public_encrypt(strlen(plaintext)+1, (unsigned char*)plaintext, encrypted, rsa, RSA_PKCS1_PADDING);
strcpy(ciphertext, "");
for(int i = 0; i < encrypted_len; i++)
{
char tmp[4];
sprintf(tmp, "%02x ", encrypted[i]);
strcat(ciphertext, tmp);
}
RSA_free(rsa);
}
// 公钥解密
void public_decrypt(char *ciphertext, char *decryptedtext, char *pubkey_file)
{
FILE *file = fopen(pubkey_file, "rb");
if(file == NULL)
{
printf("无法打开公钥文件\n");
exit(EXIT_FAILURE);
}
RSA *rsa = RSA_new();
rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL);
fclose(file);
unsigned char encrypted[MESSAGE_SIZE];
int encrypted_len = 0;
char *token = strtok(ciphertext, " ");
while(token != NULL)
{
encrypted[encrypted_len++] = (unsigned char) strtol(token, NULL, 16);
token = strtok(NULL, " ");
}
int decrypted_len = RSA_public_decrypt(encrypted_len, encrypted, (unsigned char*)decryptedtext, rsa, RSA_PKCS1_PADDING);
decryptedtext[decrypted_len] = '\0';
RSA_free(rsa);
}
// 私钥加密
void private_encrypt(char *plaintext, char *ciphertext, char *prikey_file)
{
FILE *file = fopen(prikey_file, "rb");
if(file == NULL)
{
printf("无法打开私钥文件\n");
exit(EXIT_FAILURE);
}
RSA *rsa = RSA_new();
rsa = PEM_read_RSAPrivateKey(file, NULL, NULL, NULL);
fclose(file);
unsigned char encrypted[MESSAGE_SIZE];
int encrypted_len = RSA_private_encrypt(strlen(plaintext)+1, (unsigned char*)plaintext, encrypted, rsa, RSA_PKCS1_PADDING);
strcpy(ciphertext, "");
for(int i = 0; i < encrypted_len; i++)
{
char tmp[4];
sprintf(tmp, "%02x ", encrypted[i]);
strcat(ciphertext, tmp);
}
RSA_free(rsa);
}
// 私钥解密
void private_decrypt(char *ciphertext, char *decryptedtext, char *prikey_file)
{
FILE *file = fopen(prikey_file, "rb");
if(file == NULL)
{
printf("无法打开私钥文件\n");
exit(EXIT_FAILURE);
}
RSA *rsa = RSA_new();
rsa = PEM_read_RSAPrivateKey(file, NULL, NULL, NULL);
fclose(file);
unsigned char encrypted[MESSAGE_SIZE];
int encrypted_len = 0;
char *token = strtok(ciphertext, " ");
while(token != NULL)
{
encrypted[encrypted_len++] = (unsigned char) strtol(token, NULL, 16);
token = strtok(NULL, " ");
}
int decrypted_len = RSA_private_decrypt(encrypted_len, encrypted, (unsigned char*)decryptedtext, rsa, RSA_PKCS1_PADDING);
decryptedtext[decrypted_len] = '\0';
RSA_free(rsa);
}
int main()
{
// 生成密钥对并写入文件
generate_keypair_and_write_to_file("pubkey.pem", "prikey.pem");
// 加密解密测试
char plaintext[MESSAGE_SIZE] = "This is a message to be encrypted";
char ciphertext[MESSAGE_SIZE];
char decryptedtext[MESSAGE_SIZE];
// 公钥加密
public_encrypt(plaintext, ciphertext, "pubkey.pem");
printf("公钥加密后的密文:\n%s\n", ciphertext);
// 公钥解密
public_decrypt(ciphertext, decryptedtext, "pubkey.pem");
printf("公钥解密后的明文:\n%s\n", decryptedtext);
// 私钥加密
private_encrypt(plaintext, ciphertext, "prikey.pem");
printf("私钥加密后的密文:\n%s\n", ciphertext);
// 私钥解密
private_decrypt(ciphertext, decryptedtext, "prikey.pem");
printf("私钥解密后的明文:\n%s\n", decryptedtext);
return 0;
}
```
以上代码使用openssl库生成了一个2048位的RSA密钥对,并将公钥和私钥分别写入文件中。同时,还提供了公钥加密解密和私钥加密解密的函数,可以使用公钥对数据进行加密,使用私钥对数据进行解密,也可以使用私钥对数据进行加密,使用公钥对数据进行解密。
阅读全文