C 语言实现的加密算法种类归纳

一, 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);

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值