在信息技术飞速发展的今天,数据安全成为了人们关注的焦点。C语言作为一种历史悠久且功能强大的编程语言,因其高效性和灵活性,在加密解密领域有着广泛的应用。本文将详细介绍如何使用C语言编写加密解密程序,帮助大家轻松应对数据安全挑战。
加密解密概述
加密解密是保证数据安全的重要手段,其核心思想是将原始数据通过特定的算法和密钥转换成密文,只有持有正确密钥的人才能将密文还原成原始数据。常见的加密算法有对称加密、非对称加密和哈希算法等。
对称加密
对称加密是指加密和解密使用相同的密钥。常见的对称加密算法有DES、AES等。下面以AES算法为例,介绍如何使用C语言实现加密解密。
1. 加密
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <string.h>
#include <stdio.h>
#define KEY_SIZE 16 // AES密钥长度
#define IV_SIZE 16 // 初始化向量长度
void encrypt(const unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext) {
AES_KEY aes_key;
AES_set_encrypt_key(key, KEY_SIZE * 8, &aes_key);
AES_cbc_encrypt(plaintext, ciphertext, plaintext_len, &aes_key, iv, AES_ENCRYPT);
}
int main() {
const unsigned char *key = (const unsigned char *)"0123456789abcdef";
const unsigned char *iv = (const unsigned char *)"1234567890abcdef";
const unsigned char *plaintext = (const unsigned char *)"Hello, world!";
unsigned char ciphertext[1024];
int ciphertext_len;
encrypt(plaintext, strlen((char *)plaintext), key, iv, ciphertext);
ciphertext_len = strlen((char *)ciphertext);
printf("Ciphertext is:\n");
for (int i = 0; i < ciphertext_len; i++) {
printf("%02x", ciphertext[i]);
}
printf("\n");
return 0;
}
2. 解密
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <string.h>
#include <stdio.h>
#define KEY_SIZE 16 // AES密钥长度
#define IV_SIZE 16 // 初始化向量长度
void decrypt(const unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext) {
AES_KEY aes_key;
AES_set_decrypt_key(key, KEY_SIZE * 8, &aes_key);
AES_cbc_encrypt(ciphertext, plaintext, ciphertext_len, &aes_key, iv, AES_DECRYPT);
}
int main() {
const unsigned char *key = (const unsigned char *)"0123456789abcdef";
const unsigned char *iv = (const unsigned char *)"1234567890abcdef";
const unsigned char *ciphertext = (const unsigned char *)"c083e2b4a9c392c7";
unsigned char plaintext[1024];
decrypt(ciphertext, strlen((char *)ciphertext), key, iv, plaintext);
printf("Plaintext is:\n");
printf("%s\n", plaintext);
return 0;
}
非对称加密
非对称加密是指加密和解密使用不同的密钥,通常包括公钥和私钥。常见的非对称加密算法有RSA、ECC等。下面以RSA算法为例,介绍如何使用C语言实现加密解密。
1. 加密
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
void encrypt(const unsigned char *plaintext, int plaintext_len, RSA *rsa,
unsigned char *ciphertext) {
int ciphertext_len;
BIGNUM *bn = BN_new();
BN_bin2bn(plaintext, plaintext_len, bn);
ciphertext_len = RSA_size(rsa);
if (!RSA_public_encrypt(bn->d, ciphertext, &bn->d, rsa, RSA_PKCS1_PADDING)) {
ERR_print_errors_fp(stderr);
}
BN_free(bn);
}
int main() {
FILE *pub_key_file = fopen("public.pem", "r");
RSA *rsa = PEM_read_RSAPublicKey(pub_key_file, NULL, NULL, NULL);
fclose(pub_key_file);
const unsigned char *plaintext = (const unsigned char *)"Hello, world!";
unsigned char ciphertext[1024];
encrypt(plaintext, strlen((char *)plaintext), rsa, ciphertext);
printf("Ciphertext is:\n");
for (int i = 0; i < RSA_size(rsa); i++) {
printf("%02x", ciphertext[i]);
}
printf("\n");
RSA_free(rsa);
return 0;
}
2. 解密
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
void decrypt(const unsigned char *ciphertext, int ciphertext_len, RSA *rsa,
unsigned char *plaintext) {
int plaintext_len;
BIGNUM *bn = BN_new();
BN_bin2bn(ciphertext, ciphertext_len, bn);
plaintext_len = RSA_size(rsa);
if (!RSA_private_decrypt(bn->d, plaintext, &bn->d, rsa, RSA_PKCS1_PADDING)) {
ERR_print_errors_fp(stderr);
}
BN_free(bn);
}
int main() {
FILE *pri_key_file = fopen("private.pem", "r");
RSA *rsa = PEM_read_RSAPrivateKey(pri_key_file, NULL, NULL, NULL);
fclose(pri_key_file);
const unsigned char *ciphertext = (const unsigned char *)"c083e2b4a9c392c7";
unsigned char plaintext[1024];
decrypt(ciphertext, strlen((char *)ciphertext), rsa, plaintext);
printf("Plaintext is:\n");
printf("%s\n", plaintext);
RSA_free(rsa);
return 0;
}
总结
掌握C语言编写加密解密程序,有助于我们更好地应对数据安全挑战。通过本文的介绍,相信大家已经对使用C语言实现加密解密有了初步的了解。在实际应用中,我们需要根据具体需求选择合适的加密算法,并注意密钥管理和安全存储等问题。
