Cryptographie en C++ : guide complet du chiffrement et du hachage

Formateur Python en ligne pour débutants

Apprenez Python facilement sans surcharge théorique. Résolvez des tâches pratiques avec vérification automatique et écrivez du code directement dans le navigateur.

Start Course

Cryptographie en C++ : de la théorie à la pratique du chiffrement et du hachage



La cryptographie est le fondement de la sécurité des applications modernes. En C++, le développeur a accès à des bibliothèques puissantes permettant de mettre en œuvre à la fois le chiffrement symétrique/asymétrique et le hachage cryptographique. Dans ce document, nous aborderons deux bibliothèques clés : OpenSSL (le standard de facto) et Crypto++ (C++ pur, pratique pour l'intégration). Vous apprendrez à chiffrer des données, vérifier l'intégrité et protéger des mots de passe.



Concepts cryptographiques fondamentaux

Avant de passer au code, il est important de comprendre trois opérations de base :

  • Chiffrement symétrique (AES, ChaCha20) — une seule clé pour chiffrer et déchiffrer. Rapide, adapté aux grands volumes de données.
  • Chiffrement asymétrique (RSA, ECC) — une paire de clés (publique/privée). Utilisé pour l'échange de clés et les signatures numériques.
  • Hachage (SHA-256, MD5) — transformation irréversible des données en une chaîne de longueur fixe. Utilisé pour vérifier l'intégrité et stocker les mots de passe.


1. Travailler avec OpenSSL en C++

OpenSSL est une bibliothèque multiplateforme en C, accessible via liaison. Installation : sudo apt install libssl-dev (Linux) ou via vcpkg/conan pour Windows.



Exemple : chiffrement symétrique AES-256-CBC

#include <iostream>#include <string>#include <openssl/evp.h>#include <openssl/rand.h>

std::string 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::string ciphertext(plaintext.size() + 16, '\\0'); int len = 0, ciphertext_len = 0; EVP_EncryptUpdate(ctx, reinterpret_cast<unsigned char*>(&ciphertext[0]), &len, reinterpret_cast<const unsigned char*>(plaintext.data()), plaintext.size()); ciphertext_len = len; EVP_EncryptFinal_ex(ctx, reinterpret_cast<unsigned char*>(&ciphertext[ciphertext_len]), &len); ciphertext_len += len; ciphertext.resize(ciphertext_len); EVP_CIPHER_CTX_free(ctx); return ciphertext;}

int main() { unsigned char key[32], iv[16]; RAND_bytes(key, sizeof(key)); // Génération d'une clé aléatoire RAND_bytes(iv, sizeof(iv));

std::string data = "Message secret"; std::string encrypted = aes_encrypt(data, key, iv); std::cout << "Chiffré : " << encrypted.size() << " octets" << std::endl; return 0;}


Hachage SHA-256 avec OpenSSL

#include <iostream>#include <openssl/sha.h>

std::string sha256(const std::string& str) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, str.c_str(), str.size()); 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::cout << "SHA-256 : " << sha256("Hello, Crypto!") << std::endl; return 0;}


2. Bibliothèque Crypto++ (implémentation pure C++)

Crypto++ (cryptopp) est une bibliothèque complète en C++ sans dépendances externes. Installation : sudo apt install libcrypto++-dev ou téléchargement depuis cryptopp.com.



Exemple : chiffrement RSA avec Crypto++

#include <iostream>#include <string>#include <cryptopp/rsa.h>#include <cryptopp/osrng.h>#include <cryptopp/base64.h>

int main() { using namespace CryptoPP;

AutoSeededRandomPool rng; Inve

Recommendations