Cryptography in C++: Encryption and Hashing with OpenSSL and Crypto++
Reliable data protection is a basic need of any modern application. C++ provides developers with powerful tools for implementing cryptographic algorithms: from simple hashing to complex asymmetric encryption schemes. In this article, we will cover the two most popular libraries — OpenSSL and Crypto++ — and show how to use them to solve typical tasks: hashing, symmetric encryption (AES), and asymmetric encryption (RSA).
1. OpenSSL: Universal Crypto Provider
OpenSSL is the de facto standard for cryptography in C/C++. The library supports all major algorithms, X.509 certificates, and TLS protocols. To work with it, you need to install the libssl-dev package (Linux) or download binary builds for Windows.
1.1 SHA-256 Hashing
The simplest task is to compute the hash of a string. We use the low-level API (EVP).
#include <iostream>#include <openssl/evp.h>#include <iomanip>
std::string sha256(const std::string& str) { EVP_MD_CTX* ctx = EVP_MD_CTX_new(); EVP_DigestInit_ex(ctx, EVP_sha256(), nullptr); EVP_DigestUpdate(ctx, str.data(), str.size());
unsigned char hash[EVP_MAX_MD_SIZE]; unsigned int hash_len; EVP_DigestFinal_ex(ctx, hash, &hash_len); EVP_MD_CTX_free(ctx);
std::stringstream ss; for (unsigned int i = 0; i < hash_len; ++i) ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; return ss.str();}
int main() { std::cout << sha256("Hello, Crypto!") << std::endl; return 0;}1.2 Symmetric Encryption AES-256-CBC
To encrypt data with a single key, we use AES in CBC mode. Important: the key and IV (initialization vector) must be random.
#include <openssl/evp.h>#include <vector>
std::vector<unsigned char> aes_encrypt( const std::vector<unsigned char>& plaintext, const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv) { EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key.data(), iv.data());
std::vector<unsigned char> ciphertext(plaintext.size() + 16); int out_len, tmp_len; EVP_EncryptUpdate(ctx, ciphertext.data(), &out_len, plaintext.data(), plaintext.size()); EVP_EncryptFinal_ex(ctx, ciphertext.data() + out_len, &tmp_len); out_len += tmp_len; ciphertext.resize(out_len);
EVP_CIPHER_CTX_free(ctx); return ciphertext;}Decryption is performed similarly using EVP_DecryptInit_ex and EVP_DecryptUpdate.
1.3 Asymmetric Encryption RSA
RSA is used for key exchange or data signing. Generating a key pair:
#include <openssl/rsa.h>#include <openssl/pem.h>
RSA* generate_rsa_keypair(int bits = 2048) { RSA* rsa = RSA_new(); BIGNUM* e = BN_new(); BN_set_word(e, 65537); RSA_generate_key_ex(rsa, bits, e, nullptr); BN_free(e); return rsa;}
// Saving the public key in PEMvoid save_public_key(RSA* rsa, const char* filename) { BIO* bio = BIO_new_file(filename, "w"); PEM_write_bio_RSA_PUBKEY(bio, rsa); BIO_free(bio);}2. Crypto++: C++-Oriented Alternative
Crypto++ (Wei Dai's library) offers a more "native" C++ interface: templates, RAII, streams. It requires no external dependencies.
2.1 SHA-256 Hashing in Crypto++
#include <cryptopp/sha.h>#include <cryptopp/hex.h>#include <iostream>
std::string sha256_cryptopp(const std::string& msg) { CryptoPP::SHA256 hash; byte digest[Cr Also in library
Machine Learning in Rust: Overview of Libraries for ML and Data Science
Cryptography in Kotlin: Encryption and Hashing with Examples
PDF Generation in C# (.NET): Overview of Libraries and Tools
Protocol Buffers for Java: Complete Guide with Examples