一, AES 加密算法的 C 语言实现:
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <openssl/conf.h>
#include <openssl/err.h>
// 加密函数
int aes_encrypt(const char* plaintext, const char* key, char* ciphertext) {
AES_KEY aes_key;
AES_ENCRYPT encrypt;
EVP_CIPHER_CTX ctx;
// 初始化 AES 密钥
if (AES_set_encrypt_key(key, 128, &aes_key) < 0) {
fprintf(stderr, "AES_set_encrypt_key failed\n");
return -1;
}
// 创建 EVP 加密上下文
EVP_CIPHER_CTX_init(&ctx);
// 设置加密算法为 AES-128-CBC
EVP_CIPHER_CTX_set_cipher(&ctx, EVP_aes_128_cbc(), &aes_key, NULL);
// 加密数据
EVP_CIPHER_CTX_encrypt(&ctx, ciphertext, (unsigned char*)plaintext, strlen(plaintext));
// 清理 EVP 加密上下文
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
// 解密函数
int aes_decrypt(const char* ciphertext, const char* key, char* plaintext) {
AES_KEY aes_key;
AES_DECRYPT decrypt;
EVP_CIPHER_CTX ctx;
// 初始化 AES 密钥
if (AES_set_decrypt_key(key, 128, &aes_key) < 0) {
fprintf(stderr, "AES_set_decrypt_key failed\n");
return -1;
}
// 创建 EVP 解密上下文
EVP_CIPHER_CTX_init(&ctx);
// 设置解密算法为 AES-128-CBC
EVP_CIPHER_CTX_set_cipher(&ctx, EVP_aes_128_cbc(), &aes_key, NULL);
// 解密数据
EVP_CIPHER_CTX_decrypt(&ctx, plaintext, (unsigned char*)ciphertext, strlen(ciphertext));
// 清理 EVP 解密上下文
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
int main() {
// 明文
char plaintext[] = "This is a plaintext message!";
// 密钥
char key[] = "This is a secret key!";
// 密文
char ciphertext[1024];
// 加密明文
aes_encrypt(plaintext, key, ciphertext);
// 输出密文
printf("Ciphertext: %s\n", ciphertext);
// 解密密文
char plaintext2[1024];
aes_decrypt(ciphertext, key, plaintext2);
// 输出明文
printf("Plaintext: %s\n", plaintext2);
return 0;
}
二,DES 加密算法的 C 语言实现:
#include <openssl/des.h>
#include <openssl/evp.h>
#include <openssl/conf.h>
#include <openssl/err.h>
// 加密函数
int des_encrypt(const char* plaintext, const char* key, char* ciphertext) {
DES_key_schedule ks1, ks2;
DES_cblock iv = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
EVP_CIPHER_CTX ctx;
// 初始化 DES 密钥调度表
if (DES_set_key(&key[0], &ks1) < 0) {
fprintf(stderr, "DES_set_key failed\n");
return -1;
}
// 创建 EVP 加密上下文
EVP_CIPHER_CTX_init(&ctx);
// 设置加密算法为 DES-ECB
EVP_CIPHER_CTX_set_cipher(&ctx, EVP_des_ecb(), &ks1, &iv);
// 加密数据
EVP_CIPHER_CTX_encrypt(&ctx, ciphertext, (unsigned char*)plaintext, strlen(plaintext));
// 清理 EVP 加密上下文
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
// 解密函数
int des_decrypt(const char* ciphertext, const char* key, char* plaintext) {
DES_key_schedule ks1, ks2;
DES_cblock iv = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
EVP_CIPHER_CTX ctx;
// 初始化 DES 密钥调度表
if (DES_set_key(&key[0], &ks1) < 0) {
fprintf(stderr, "DES_set_key failed\n");
return -1;
}
// 创建 EVP 解密上下文
EVP_CIPHER_CTX_init(&ctx);
// 设置解密算法为 DES-ECB
EVP_CIPHER_CTX_set_cipher(&ctx, EVP_des_ecb(), &ks1, &iv);
// 解密数据
EVP_CIPHER_CTX_decrypt(&ctx, plaintext, (unsigned char*)ciphertext, strlen(ciphertext));
// 清理 EVP 解密上下文
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
int main() {
// 明文
char plaintext[] = "This is a plaintext message!";
// 密钥
char key[] = "This is a secret key!";
// 密文
char ciphertext[1024];
// 加密明文
des_encrypt(plaintext, key, ciphertext);
// 输出密文
printf("Ciphertext: %s\n", ciphertext);
// 解密密文
char plaintext2[1024];
des_decrypt(ciphertext, key, plaintext2);
// 输出明文
printf("Plaintext: %s\n", plaintext2);
return 0;
}
二,RSA 加密算法的 C 语言实现:
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/conf.h>
#include <openssl/err.h>
// 加密函数
int rsa_encrypt(const char* plaintext, const char* pubkey, char* ciphertext) {
RSA* rsa = NULL;
BIO* bio = NULL;
int ret = -1;
// 打开 RSA 公钥文件
bio = BIO_new_file(pubkey, "r");
if (bio == NULL) {
fprintf(stderr, "Failed to open RSA public key file: %s\n", pubkey);
goto end;
}
// 读取 RSA 公钥
rsa = PEM_read_RSA_PUBKEY(bio, NULL, NULL, NULL);
if (rsa == NULL) {
fprintf(stderr, "Failed to read RSA public key from file: %s\n", pubkey);
goto end;
}
// 获取 RSA 公钥的模长
int key_len = RSA_size(rsa);
// 加密明文
ret = RSA_public_encrypt(strlen(plaintext), (const unsigned char*)plaintext, (unsigned char*)ciphertext, rsa, RSA_PKCS1_PADDING);
// 清理 BIO
BIO_free(bio);
RSA_free(rsa);
if (ret < 0) {
fprintf(stderr, "Failed to encrypt plaintext using RSA public key: %s\n", pubkey);
goto end;
}
// 设置密文的长度为模长
ciphertext[key_len] = '\0';
end:
return ret;
}
// 解密函数
int rsa_decrypt(const char* ciphertext, const char* privkey, char* plaintext) {
RSA* rsa = NULL;
BIO* bio = NULL;
int ret = -1;
// 打开 RSA 私钥文件
bio = BIO_new_file(privkey, "r");
if (bio == NULL) {
fprintf(stderr, "Failed to open RSA private key file: %s\n", privkey);
goto end;
}
// 读取 RSA 私钥
rsa = PEM_read_RSAPrivateKey(bio, NULL, NULL, NULL);
if (rsa == NULL) {
fprintf(stderr, "Failed to read RSA private key from file: %s\n", privkey);
goto end;
}
// 获取 RSA 私钥的模长
int key_len = RSA_size(rsa);
// 解密密文
ret = RSA_private_decrypt(strlen(ciphertext), (const unsigned char*)ciphertext, (unsigned char*)plaintext, rsa, RSA_PKCS1_PADDING);
// 清理 BIO
BIO_free(bio);
RSA_free(rsa);
if (ret < 0) {
fprintf(stderr, "Failed to decrypt ciphertext using RSA private key: %s\n", privkey);
goto end;
}
// 设置明文的长度为模长
plaintext[key_len] = '\0';
end:
return ret;
}
int main() {
// 明文
char plaintext[] = "This is a plaintext message!";
// 公钥文件路径
char pubkey[] = "pubkey.pem";
// 私钥文件路径
char privkey[] = "privkey.pem";
// 密文
char ciphertext[1024];
// 加密明文
rsa_encrypt(plaintext, pubkey, ciphertext);
// 输出密文
printf("Ciphertext: %s\n", ciphertext);
// 解密密文
char plaintext2[1024];
rsa_decrypt(ciphertext, privkey, plaintext2);