# Encrypt and decrypt using AES key

This sample app demonstrates how to generate a AES key, encrypt and decrypt given data using this key.

1. Get a SecurityFactory instance

auto &secFact = telux::sec::SecurityFactory::getInstance();
    Copy to clipboard

2. Get a CryptoManager instance

std::shared_ptr<telux::sec::ICryptoManager> cryptMgr;
    
    cryptMgr = secFact.getCryptoManager(ec);
    if (!cryptMgr) {
        std::cout << "Can't allocate CryptoManager, err: " <<
                static_cast<int>(ec) << std::endl;
        return -1;
    }
    Copy to clipboard

3. Define parameters using CryptoParamBuilder for the key

std::shared_ptr<telux::sec::ICryptoParam> cp;
    
    cp = telux::sec::CryptoParamBuilder()
            .setAlgorithm(telux::sec::Algorithm::ALGORITHM_AES)
            .setCryptoOperation(telux::sec::CryptoOperation::CRYPTO_OP_ENCRYPT |
                        telux::sec::CryptoOperation::CRYPTO_OP_DECRYPT)
            .setKeySize(128)
            .setBlockMode(telux::sec::BlockMode::BLOCK_MODE_CBC |
                        telux::sec::BlockMode::BLOCK_MODE_CTR)
            .setPadding(telux::sec::Padding::PADDING_PKCS7 |
                        telux::sec::Padding::PADDING_NONE)
            .setCallerNonce(true)
            .build();
    Copy to clipboard

4. Generate AES key using generateKey() API

std::vector<uint8_t> kb;
    telux::common::ErrorCode ec;
    
    ec = cryptMgr->generateKey(cp, kb);
    if (ec != telux::common::ErrorCode::SUCCESS) {
        std::cout << "Can't generate AES sym key, err: " <<
                static_cast<int>(ec) << std::endl;
        return -1;
    }
    Copy to clipboard

5. Define data to be encrypted

std::vector<uint8_t> pt {'h', 'e', 'l', 'l', 'o'};
    Copy to clipboard

6. Specify initialization vector for encryption/decryption

std::vector<uint8_t> initVector((128/8), 0x01);
    Copy to clipboard

7. Define parameters using CryptoParamBuilder for encryption/decryption

cp = telux::sec::CryptoParamBuilder()
            .setAlgorithm(telux::sec::Algorithm::ALGORITHM_AES)
            .setBlockMode(telux::sec::BlockMode::BLOCK_MODE_CBC)
            .setPadding(telux::sec::Padding::PADDING_PKCS7)
            .setInitVector(initVector)
            .build();
    Copy to clipboard

8. Encrypt the required data using encryptData() API

std::vector<uint8_t> et;
    
    ec = cryptMgr->encryptData(cp, kb, pt, et);
    if (ec != telux::common::ErrorCode::SUCCESS) {
        std::cout << "Can't encrypt data, err: " <<
                static_cast<int>(ec) << std::endl;
        return -1;
    }
    Copy to clipboard

9. Decrypt data using decryptData() API

std::vector<uint8_t> dt;
    
    ec = cryptMgr->decryptData(cp, kb, et, dt);
    if (ec != telux::common::ErrorCode::SUCCESS) {
        std::cout << "Can't decrypt data, err: " <<
                static_cast<int>(ec) << std::endl;
        return -1;
    }
    Copy to clipboard

Last Published: Apr 14, 2026

[Previous Topic
Sign and verify using RSA key](https://docs.qualcomm.com/bundle/publicresource/80-PF458-1/topics/RSAAsymKeySignVerifyApp.md) [Next Topic
Export a RSA key](https://docs.qualcomm.com/bundle/publicresource/80-PF458-1/topics/RSAExportKeyApp.md)