Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:58:51+00:00 2026-05-26T15:58:51+00:00

I need to do simple single-block AES encryption / decryption in my Qt /

  • 0

I need to do simple single-block AES encryption / decryption in my Qt / C++ application. This is a “keep the honest people honest” implementation, so just a basic encrypt(key, data) is necessary–I’m not worried about initialization vectors, etc. My input and key will always be exactly 16 bytes.

I’d really like to avoid another dependency to compile / link / ship with my application, so I’m trying to use what’s available on each platform. On the Mac, this was a one-liner to CCCrypt. On Windows, I’m getting lost in the API from WinCrypt.h. Their example of encrypting a file is almost 600 lines long. Seriously?

I’m looking at CryptEncrypt, but I’m falling down the rabbit hole of dependencies you have to create before you can call that.

Can anyone provide a simple example of doing AES encryption using the Windows API? Surely there’s a way to do this in a line or two. Assume you already have a 128-bit key and 128-bits of data to encrypt.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:58:51+00:00Added an answer on May 26, 2026 at 3:58 pm

    Here’s the best I’ve been able to come up with. Suggestions for improvement are welcome!

    static void encrypt(const QByteArray &data,
                        const QByteArray &key,
                        QByteArray *encrypted) {
      // Create the crypto provider context.
      HCRYPTPROV hProvider = NULL;
      if (!CryptAcquireContext(&hProvider,
                               NULL,  // pszContainer = no named container
                               NULL,  // pszProvider = default provider
                               PROV_RSA_AES,
                               CRYPT_VERIFYCONTEXT)) {
        throw std::runtime_error("Unable to create crypto provider context.");
      }
    
      // Construct the blob necessary for the key generation.
      AesBlob128 aes_blob;
      aes_blob.header.bType = PLAINTEXTKEYBLOB;
      aes_blob.header.bVersion = CUR_BLOB_VERSION;
      aes_blob.header.reserved = 0;
      aes_blob.header.aiKeyAlg = CALG_AES_128;
      aes_blob.key_length = kAesBytes128;
      memcpy(aes_blob.key_bytes, key.constData(), kAesBytes128);
    
      // Create the crypto key struct that Windows needs.
      HCRYPTKEY hKey = NULL;
      if (!CryptImportKey(hProvider,
                          reinterpret_cast<BYTE*>(&aes_blob),
                          sizeof(AesBlob128),
                          NULL,  // hPubKey = not encrypted
                          0,     // dwFlags
                          &hKey)) {
        throw std::runtime_error("Unable to create crypto key.");
      }
    
      // The CryptEncrypt method uses the *same* buffer for both the input and
      // output (!), so we copy the data to be encrypted into the output array.
      // Also, for some reason, the AES-128 block cipher on Windows requires twice
      // the block size in the output buffer. So we resize it to that length and
      // then chop off the excess after we are done.
      encrypted->clear();
      encrypted->append(data);
      encrypted->resize(kAesBytes128 * 2);
    
      // This acts as both the length of bytes to be encoded (on input) and the
      // number of bytes used in the resulting encrypted data (on output).
      DWORD length = kAesBytes128;
      if (!CryptEncrypt(hKey,
                        NULL,  // hHash = no hash
                        true,  // Final
                        0,     // dwFlags
                        reinterpret_cast<BYTE*>(encrypted->data()),
                        &length,
                        encrypted->length())) {
        throw std::runtime_error("Encryption failed");
      }
    
      // See comment above.
      encrypted->chop(length - kAesBytes128);
    
      CryptDestroyKey(hKey);
      CryptReleaseContext(hProvider, 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes you need to create a very simple single file application in Qt4. However
I need to build a simple, single user database application for Windows. Main requirements
How do I create unique key value in PHP? I need simple unique keys
I need a simple way to monitor multiple text log files distributed over a
I need a simple table with a user name and password field in MySQL.
I need a simple app to edit database tables. Are there any code generators
I need a simple window with three input boxes and three labels (login name,
I need a simple SQL to accomplish the below: Problem: When a petrol bunk
I need a simple function which will take a FileInfo and a destination_directory_name as
I need a simple communication protocol between two devices (a PC and a microcontroller).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.