Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/encryption-keys/cpp$ _

Generate Encryption Keys in C++ - Crypto++ Library Examples

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: C++

What: Generate AES and RSA encryption keys in C++ using the Crypto++ library for secure cryptographic operations.

Try it: Use our interactive Encryption-keys generator or integrate this code into your C++ application.

Generate AES and RSA encryption keys in C++ using the Crypto++ library for secure cryptographic operations. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , Ruby and Go or use our interactive web generator.

C++ Code Example

#include <iostream>
#include <cryptopp/cryptlib.h>
#include <cryptopp/rijndael.h>
#include <cryptopp/modes.h>
#include <cryptopp/files.h>
#include <cryptopp/osrng.h>
#include <cryptopp/rsa.h>
#include <cryptopp/hex.h>
#include <cryptopp/base64.h>

using namespace CryptoPP;

int main() {
    AutoSeededRandomPool rng;

    // AES-256 Key Generation
    byte aes256Key[AES::DEFAULT_KEYLENGTH]; // 32 bytes
    rng.GenerateBlock(aes256Key, sizeof(aes256Key));

    std::string aes256Hex;
    HexEncoder hexEncoder(new StringSink(aes256Hex));
    hexEncoder.Put(aes256Key, sizeof(aes256Key));
    hexEncoder.MessageEnd();

    std::cout << "AES-256 Key (Hex): " << aes256Hex << std::endl;

    // AES-128 Key
    byte aes128Key[16];
    rng.GenerateBlock(aes128Key, 16);

    std::string aes128Hex;
    HexEncoder(new StringSink(aes128Hex)).Put(aes128Key, 16);
    std::cout << "AES-128 Key (Hex): " << aes128Hex << std::endl;

    // RSA Key Pair Generation (2048-bit)
    RSA::PrivateKey privateKey;
    privateKey.GenerateRandomWithKeySize(rng, 2048);

    RSA::PublicKey publicKey(privateKey);

    // Export private key (DER format)
    std::string privateKeyDer;
    StringSink privKeySink(privateKeyDer);
    privateKey.DEREncode(privKeySink);
    privKeySink.MessageEnd();

    std::string privateKeyBase64;
    Base64Encoder(new StringSink(privateKeyBase64)).Put(
        (const byte*)privateKeyDer.data(),
        privateKeyDer.size()
    );

    std::cout << "Private Key (Base64): " << privateKeyBase64 << std::endl;

    return 0;
}

[EXPLANATION]

Crypto++ provides AutoSeededRandomPool for cryptographically secure random number generation. For AES keys, use GenerateBlock() with 16 bytes (AES-128), 24 bytes (AES-192), or 32 bytes (AES-256). AES::DEFAULT_KEYLENGTH is 32 bytes. HexEncoder converts binary keys to hexadecimal strings for storage. For RSA, RSA::PrivateKey with GenerateRandomWithKeySize() creates a private key with the specified bit length (2048 or 4096). Construct the public key from the private key. DEREncode() exports keys in DER format, which can be converted to PEM by adding headers and Base64 encoding.

Expected Output

AES-256 Key (Hex): C7E2F5A9D3B8E1C4F7A2D6B9E3C5F8A1D4B7E9C2F5A8D1B4E7F9C3A6D8E2F5A9
AES-128 Key (Hex): F3A7D2E5C1B8F4A9D6C3E7B2F5A8C1D4
Private Key (Base64): MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDH...

Common Use Cases

  • High-performance encryption in C++ applications
  • Secure file encryption/decryption
  • Embedded systems cryptography
  • Game server security
  • IoT device secure communication

Important Notes

  • Install Crypto++: apt-get install libcrypto++-dev (Linux)
  • Link against -lcryptopp during compilation
  • AutoSeededRandomPool uses OS entropy sources
  • For PEM export, add "-----BEGIN PRIVATE KEY-----" headers manually
  • Crypto++ is header-only compatible with modern C++ (C++11+)

Try Our Interactive Generator

Don't want to write code? Use our free web-based Encryption-keys generator with instant results.

TRY ENCRYPTION-KEYS GENERATOR →