Cryptography in C++: Encryption and Hashing — Complete Guide
Cryptography is the fundamental basis of security for modern applications. In C++, developers have access to powerful libraries that enable data encryption, decryption, and hashing. In this article, we will take a detailed look at two popular libraries: OpenSSL and Crypto++, and also examine key concepts: symmetric encryption (AES), asymmetric encryption (RSA), and cryptographic hash functions (SHA-256).
1. Basic Concepts of Cryptography
Before moving to code, it is important to understand the basic terms:
- Symmetric encryption — one key is used for both encryption and decryption. Examples: AES, DES, ChaCha20.
- Asymmetric encryption — a pair of keys: public (for encryption) and private (for decryption). Examples: RSA, ECC.
- Hash functions — irreversible transformation of data into a fixed-length string. Examples: SHA-256, MD5 (deprecated), BLAKE2.
- Salt — random data added to a password before hashing to protect against rainbow tables.
2. OpenSSL Library in C++
OpenSSL is a cross-platform open-source library that implements SSL/TLS protocols and a wide range of cryptographic algorithms. To work with it in C++, you need to include header files and link with the libssl and libcrypto libraries.
Example 1: SHA-256 Hashing with OpenSSL
#include <iostream>#include <openssl/sha.h>#include <cstring>
std::string sha256(const std::string& input) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, input.c_str(), input.length()); SHA256_Final(hash, &sha256);
std::string output; for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) { char buf[3]; sprintf(buf, "%02x", hash[i]); output += buf; } return output;}
int main() { std::string data = "PythonLib - cryptography in C++"; std::cout << "SHA-256: " << sha256(data) << std::endl; return 0;}This code demonstrates basic usage of the SHA256 context. The SHA256_Init function initializes the context, SHA256_Update adds data (can be called multiple times for streaming processing), and SHA256_Final completes the computation and writes the result.
Example 2: AES-256-CBC Symmetric Encryption
#include <iostream>#include <openssl/evp.h>#include <vector>#include <cstring>
std::vector<unsigned char> aes_encrypt(const std::string& plaintext, const unsigned char* key, const unsigned char* iv) { EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, iv);
std::vector<unsigned char> ciphertext(plaintext.size() + EVP_CIPHER_block_size(EVP_aes_256_cbc())); int len = 0; int ciphertext_len = 0;
EVP_EncryptUpdate(ctx, ciphertext.data(), &len, reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.size()); ciphertext_len = len;
EVP_EncryptFinal_ex(ctx, ciphertext.data() + len, &len); ciphertext_len += len; ciphertext.resize(ciphertext_len);
EVP_CIPHER_CTX_free(ctx); return ciphertext;}
int main() { // 256-bit key (32 bytes) and 128-bit IV (16 bytes) unsigned char key[32] = "01234567890123456789012345678901"; unsigned char iv[16] = "0123456789012345";
std::string message = "Secret data for PythonLib"; Also in library
Geolocation and Maps in Go (Golang): Libraries and Code Examples
Rate Limiting in C# (.NET): Complete Guide to System.Threading.RateLimiting Library
Cryptography in Node.js: Encryption and Hashing with crypto