Home > Article > Backend Development > How to encrypt data in C++ code?
How to encrypt data in C code?
With the rapid development of the Internet and the increasing maturity of information technology, data security has become more and more important. In programming, it is very important to protect the security of data and prevent data from illegal access or tampering. This article will introduce how to use C code to encrypt and decrypt data to ensure data security.
The key encryption algorithm is a commonly used symmetric encryption algorithm that uses the same key to encrypt and decrypt data. In C, you can use some open source encryption libraries (such as OpenSSL) to implement key encryption algorithms. The following is a sample code that uses the AES algorithm to encrypt and decrypt data:
#include <openssl/aes.h> #include <string> std::string encryptAES(std::string data, std::string key) { AES_KEY aesKey; AES_set_encrypt_key((const unsigned char*)key.c_str(), 128, &aesKey); std::string encryptedData; encryptedData.resize(data.size() + AES_BLOCK_SIZE); unsigned char iv[AES_BLOCK_SIZE]; memset(iv, 0, sizeof(iv)); AES_cbc_encrypt((const unsigned char*)data.c_str(), (unsigned char*)encryptedData.data(), data.size(), &aesKey, iv, AES_ENCRYPT); return encryptedData; } std::string decryptAES(std::string encryptedData, std::string key) { AES_KEY aesKey; AES_set_decrypt_key((const unsigned char*)key.c_str(), 128, &aesKey); std::string decryptedData; decryptedData.resize(encryptedData.size()); unsigned char iv[AES_BLOCK_SIZE]; memset(iv, 0, sizeof(iv)); AES_cbc_encrypt((const unsigned char*)encryptedData.c_str(), (unsigned char*)decryptedData.data(), encryptedData.size(), &aesKey, iv, AES_DECRYPT); return decryptedData; }
The above code uses the AES algorithm to take data and keys as input, then performs encryption and decryption operations, and returns the encrypted/decrypted results. . In actual use, you need to ensure the security of the key and do not store or transmit it in plain text.
Asymmetric encryption algorithm is an algorithm that uses different keys for encryption and decryption. Common asymmetric encryption algorithms include RSA and ECC. In C, you can use some open source encryption libraries (such as Crypto) to implement asymmetric encryption algorithms. Below is a sample code that uses the RSA algorithm to encrypt and decrypt data:
#include <cryptopp/rsa.h> #include <cryptopp/osrng.h> #include <cryptopp/base64.h> #include <string> std::string encryptRSA(std::string data, std::string publicKey) { CryptoPP::RSA::PublicKey rsaPublicKey; CryptoPP::Base64Decoder base64Decoder; base64Decoder.Attach(new CryptoPP::StringSink(publicKey), false); rsaPublicKey.Load(base64Decoder); CryptoPP::AutoSeededRandomPool rng; std::string encryptedData; CryptoPP::RSAES_OAEP_SHA_Encryptor encryptor(rsaPublicKey); CryptoPP::StringSource(data, true, new CryptoPP::PK_EncryptorFilter(rng, encryptor, new CryptoPP::StringSink(encryptedData))); return encryptedData; } std::string decryptRSA(std::string encryptedData, std::string privateKey) { CryptoPP::RSA::PrivateKey rsaPrivateKey; CryptoPP::Base64Decoder base64Decoder; base64Decoder.Attach(new CryptoPP::StringSink(privateKey), false); rsaPrivateKey.Load(base64Decoder); CryptoPP::AutoSeededRandomPool rng; std::string decryptedData; CryptoPP::RSAES_OAEP_SHA_Decryptor decryptor(rsaPrivateKey); CryptoPP::StringSource(encryptedData, true, new CryptoPP::PK_DecryptorFilter(rng, decryptor, new CryptoPP::StringSink(decryptedData))); return decryptedData; }
The above code uses the RSA algorithm to take data and public/private keys as input, then performs encryption and decryption operations, and returns encryption/decryption the final result. The public key is typically used to encrypt data, and the private key is used to decrypt data. In actual use, it is necessary to ensure the security of the private key and not leak it.
In the process of data encryption, data integrity also needs to be considered. Data integrity verification ensures that the data has not been tampered with during transmission by adding a check code to the data. Common data integrity check algorithms include CRC and Hash algorithms. In C, you can use some open source verification libraries (such as Crypto) to implement data integrity verification. The following is a sample code that uses the Hash algorithm to calculate the check code of the data:
#include <cryptopp/sha.h> #include <cryptopp/hex.h> #include <string> std::string calculateHash(std::string data) { CryptoPP::SHA256 hash; std::string hashValue; CryptoPP::StringSource(data, true, new CryptoPP::HashFilter(hash, new CryptoPP::HexEncoder(new CryptoPP::StringSink(hashValue)))); return hashValue; }
The above code uses the SHA256 algorithm to calculate the check code of the data and returns the hexadecimal representation of the check code. After the check code is calculated, it is transmitted or stored together with the data. The receiver calculates the check code again after receiving the data and compares it with the received check code. If the two are consistent, the data does not been tampered with.
Summary:
This article introduces how to use C code to encrypt and decrypt data, and how to perform data integrity verification. In practical applications, appropriate encryption algorithms and verification algorithms can be selected according to actual needs, and the security of keys and private keys can be ensured to ensure data security and integrity. During data transmission or storage, other security measures (such as SSL/TLS protocols) can also be combined to further enhance data security.
The above is the detailed content of How to encrypt data in C++ code?. For more information, please follow other related articles on the PHP Chinese website!