使用openssl编写RSA加解密算法
时间: 2023-08-03 16:07:37 浏览: 88
好的,以下是使用 OpenSSL 库编写 RSA 加解密算法的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#define KEY_LENGTH 2048
#define PUB_KEY_FILE "public.pem"
#define PRI_KEY_FILE "private.pem"
int generate_keypair()
{
int ret = 0;
RSA *rsa = NULL;
BIGNUM *e = NULL;
BIO *pri_bio = NULL, *pub_bio = NULL;
// 创建 RSA 结构体
rsa = RSA_new();
if (!rsa) {
printf("Failed to create RSA struct.\n");
return -1;
}
// 设置 RSA 公钥指数
e = BN_new();
ret = BN_set_word(e, RSA_F4);
if (ret != 1) {
printf("Failed to set RSA exponent.\n");
RSA_free(rsa);
return -1;
}
// 生成 RSA 密钥对
ret = RSA_generate_key_ex(rsa, KEY_LENGTH, e, NULL);
if (ret != 1) {
printf("Failed to generate RSA key pair.\n");
RSA_free(rsa);
return -1;
}
// 保存 RSA 公钥
pub_bio = BIO_new_file(PUB_KEY_FILE, "w");
if (!pub_bio) {
printf("Failed to create public key file.\n");
RSA_free(rsa);
return -1;
}
ret = PEM_write_bio_RSAPublicKey(pub_bio, rsa);
if (ret != 1) {
printf("Failed to write public key.\n");
RSA_free(rsa);
BIO_free(pub_bio);
return -1;
}
BIO_free(pub_bio);
// 保存 RSA 私钥
pri_bio = BIO_new_file(PRI_KEY_FILE, "w");
if (!pri_bio) {
printf("Failed to create private key file.\n");
RSA_free(rsa);
return -1;
}
ret = PEM_write_bio_RSAPrivateKey(pri_bio, rsa, NULL, NULL, 0, NULL, NULL);
if (ret != 1) {
printf("Failed to write private key.\n");
RSA_free(rsa);
BIO_free(pri_bio);
return -1;
}
BIO_free(pri_bio);
RSA_free(rsa);
return 0;
}
int rsa_encrypt(const char *plain_text, char *cipher_text)
{
int ret = 0;
RSA *rsa = NULL;
BIO *pub_bio = NULL;
int len = 0;
// 从文件读取 RSA 公钥
pub_bio = BIO_new_file(PUB_KEY_FILE, "r");
if (!pub_bio) {
printf("Failed to read public key file.\n");
return -1;
}
rsa = PEM_read_bio_RSAPublicKey(pub_bio, NULL, NULL, NULL);
if (!rsa) {
printf("Failed to read public key.\n");
BIO_free(pub_bio);
return -1;
}
BIO_free(pub_bio);
// 加密明文
len = RSA_public_encrypt(strlen(plain_text), (unsigned char *)plain_text, (unsigned char *)cipher_text, rsa, RSA_PKCS1_PADDING);
if (len < 0) {
printf("Failed to encrypt plain text.\n");
RSA_free(rsa);
return -1;
}
RSA_free(rsa);
return len;
}
int rsa_decrypt(const char *cipher_text, char *plain_text)
{
int ret = 0;
RSA *rsa = NULL;
BIO *pri_bio = NULL;
int len = 0;
// 从文件读取 RSA 私钥
pri_bio = BIO_new_file(PRI_KEY_FILE, "r");
if (!pri_bio) {
printf("Failed to read private key file.\n");
return -1;
}
rsa = PEM_read_bio_RSAPrivateKey(pri_bio, NULL, NULL, NULL);
if (!rsa) {
printf("Failed to read private key.\n");
BIO_free(pri_bio);
return -1;
}
BIO_free(pri_bio);
// 解密密文
len = RSA_private_decrypt(KEY_LENGTH/8, (unsigned char *)cipher_text, (unsigned char *)plain_text, rsa, RSA_PKCS1_PADDING);
if (len < 0) {
printf("Failed to decrypt cipher text.\n");
RSA_free(rsa);
return -1;
}
RSA_free(rsa);
return len;
}
int main()
{
char plain_text[] = "Hello, world!";
char cipher_text[KEY_LENGTH/8] = {0};
char decrypted_text[KEY_LENGTH/8] = {0};
int len = 0;
// 生成 RSA 密钥对
if (generate_keypair() != 0) {
printf("Failed to generate RSA key pair.\n");
return -1;
}
// 加密明文
len = rsa_encrypt(plain_text, cipher_text);
if (len < 0) {
printf("Failed to encrypt plain text.\n");
return -1;
}
printf("Cipher text: %s\n", cipher_text);
// 解密密文
len = rsa_decrypt(cipher_text, decrypted_text);
if (len < 0) {
printf("Failed to decrypt cipher text.\n");
return -1;
}
printf("Decrypted text: %s\n", decrypted_text);
return 0;
}
```
这个示例代码中,我们使用 OpenSSL 库中的 RSA 函数实现了 RSA 加解密算法。在程序启动时,我们使用 `generate_keypair()` 函数生成 RSA 密钥对,并将公钥和私钥保存到文件中。接着,我们使用 `rsa_encrypt()` 函数加密明文,使用 `rsa_decrypt()` 函数解密密文,最后输出解密后的明文。需要注意的是,在解密密文时,我们需要使用私钥进行解密。
阅读全文