阿木博主一句话概括:C++ 数据加密与解密技术实践
阿木博主为你简单介绍:随着信息技术的飞速发展,数据安全成为了一个日益重要的话题。C++作为一种高性能的编程语言,在数据加密与解密领域有着广泛的应用。本文将围绕C++语言,探讨几种常见的数据加密与解密技术,并通过实际代码示例进行演示。
一、
数据加密与解密是保障信息安全的重要手段。在C++中,我们可以使用多种加密算法来实现数据的加密和解密。本文将介绍以下几种加密技术:
1. 基于异或(XOR)的加密
2. 基于AES(高级加密标准)的加密
3. 基于RSA(公钥加密)的加密
二、基于异或(XOR)的加密
异或加密是一种简单的加密方法,它通过将明文和密钥进行异或操作,得到密文。解密时,只需将密文和相同的密钥进行异或操作,即可恢复明文。
cpp
include
include
std::string xorEncryptDecrypt(const std::string& data, const std::string& key) {
std::string result;
for (size_t i = 0; i < data.size(); ++i) {
result += (data[i] ^ key[i % key.size()]);
}
return result;
}
int main() {
std::string data = "Hello, World!";
std::string key = "key";
std::string encrypted = xorEncryptDecrypt(data, key);
std::string decrypted = xorEncryptDecrypt(encrypted, key);
std::cout << "Original: " << data << std::endl;
std::cout << "Encrypted: " << encrypted << std::endl;
std::cout << "Decrypted: " << decrypted << std::endl;
return 0;
}
三、基于AES(高级加密标准)的加密
AES是一种广泛使用的对称加密算法,它具有较高的安全性和效率。在C++中,我们可以使用第三方库如OpenSSL来实现AES加密和解密。
cpp
include
include
include
include
include
include
void printHex(const unsigned char data, int len) {
for (int i = 0; i < len; ++i) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast(data[i]);
}
std::cout << std::endl;
}
int main() {
const char key = "0123456789abcdef";
const char iv = "1234567890abcdef";
unsigned char encrypted;
unsigned char decrypted;
int encrypted_len, decrypted_len;
// 初始化加密上下文
EVP_CIPHER_CTX ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, reinterpret_cast(key), reinterpret_cast(iv));
// 加密数据
unsigned char input[] = "Hello, World!";
unsigned char output[1024];
encrypted_len = EVP_EncryptUpdate(ctx, output, &output[0], input, sizeof(input));
encrypted = new unsigned char[encrypted_len + EVP_MAX_BLOCK_LENGTH];
EVP_EncryptFinal_ex(ctx, encrypted + encrypted_len, &encrypted_len);
encrypted_len += EVP_MAX_BLOCK_LENGTH;
// 打印加密结果
std::cout << "Encrypted: ";
printHex(encrypted, encrypted_len);
// 解密数据
EVP_DecryptInit_ex(ctx, NULL, NULL, reinterpret_cast(key), reinterpret_cast(iv));
decrypted_len = EVP_DecryptUpdate(ctx, decrypted, &decrypted[0], encrypted, encrypted_len);
decrypted = new unsigned char[decrypted_len + EVP_MAX_BLOCK_LENGTH];
EVP_DecryptFinal_ex(ctx, decrypted + decrypted_len, &decrypted_len);
decrypted_len += EVP_MAX_BLOCK_LENGTH;
// 打印解密结果
std::cout << "Decrypted: ";
printHex(decrypted, decrypted_len);
// 清理资源
delete[] encrypted;
delete[] decrypted;
EVP_CIPHER_CTX_free(ctx);
return 0;
}
四、基于RSA(公钥加密)的加密
RSA是一种非对称加密算法,它使用公钥和私钥进行加密和解密。在C++中,我们可以使用第三方库如OpenSSL来实现RSA加密和解密。
cpp
include
include
include
include
void printHex(const unsigned char data, int len) {
for (int i = 0; i < len; ++i) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast(data[i]);
}
std::cout << std::endl;
}
int main() {
// 生成RSA密钥对
BIGNUM bn = BN_new();
BN_set_word(bn, RSA_F4);
RSA rsa = RSA_new();
RSA_generate_key_ex(rsa, 2048, bn, NULL);
// 读取公钥
FILE pubFile = fopen("public.pem", "wb");
PEM_write_RSAPublicKey(pubFile, rsa);
fclose(pubFile);
// 读取私钥
FILE privFile = fopen("private.pem", "wb");
PEM_write_RSAPrivateKey(privFile, rsa, NULL, NULL, 0, NULL, NULL);
fclose(privFile);
// 加密数据
unsigned char encrypted;
unsigned char decrypted;
int encrypted_len, decrypted_len;
FILE pubKeyFile = fopen("public.pem", "rb");
FILE privKeyFile = fopen("private.pem", "rb");
EVP_PKEY pubKey = PEM_read_PUBKEY(pubKeyFile, NULL, NULL, NULL);
EVP_PKEY privKey = PEM_read_PrivateKey(privKeyFile, NULL, NULL, NULL);
unsigned char input[] = "Hello, World!";
encrypted_len = RSA_size(rsa);
encrypted = new unsigned char[encrypted_len];
RSA_public_encrypt(sizeof(input), input, encrypted, rsa, RSA_PKCS1_PADDING);
encrypted_len = RSA_size(rsa);
// 打印加密结果
std::cout << "Encrypted: ";
printHex(encrypted, encrypted_len);
// 解密数据
decrypted_len = RSA_size(rsa);
decrypted = new unsigned char[decrypted_len];
RSA_private_decrypt(encrypted_len, encrypted, decrypted, rsa, RSA_PKCS1_PADDING);
decrypted_len = RSA_size(rsa);
// 打印解密结果
std::cout << "Decrypted: ";
printHex(decrypted, decrypted_len);
// 清理资源
delete[] encrypted;
delete[] decrypted;
EVP_PKEY_free(pubKey);
EVP_PKEY_free(privKey);
RSA_free(rsa);
BN_free(bn);
fclose(pubKeyFile);
fclose(privKeyFile);
return 0;
}
五、总结
本文介绍了C++语言中几种常见的数据加密与解密技术,包括基于异或的加密、基于AES的加密和基于RSA的加密。通过实际代码示例,展示了如何使用C++实现这些加密算法。在实际应用中,选择合适的加密算法和密钥管理策略对于保障数据安全至关重要。
Comments NOTHING