Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. AES128 encrypt problems

AES128 encrypt problems

Scheduled Pinned Locked Moved Mobile and Embedded
3 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rileo8
    wrote on last edited by
    #1

    Hi,

    i'm trying to crypt a text using AES128..this is my code:

    @typedef struct {
    BLOBHEADER header;
    DWORD key_length;
    BYTE key_bytes[16];
    }AesBlob128;

    void encrypt2(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)) {
    qDebug()<<"Unable to create crypto provider context.";
    }

    int kAesBytes128=16;

    AesBlob128 aes_blob;

    // Construct the blob necessary for the key generation.
    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)) {
    qDebug()<<"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(data.size() * 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;
    bool last=false;
    int blocks=data.size()/kAesBytes128;
    if(data.size()%kAesBytes128!=0)
    blocks=blocks+1;

    for(int i=0;i<blocks;i++)
    {
    if(i==blocks-1)
    last=true;

    if (!CryptEncrypt(hKey,
    NULL, // hHash = no hash
    last, // Final
    0, // dwFlags
    reinterpret_cast<BYTE*>(encrypted->data())+(kAesBytes128*i),
    &length,
    encrypted->length())) {
    qDebug()<<"Encryption failed";
    }
    }

    // See comment above.
    // encrypted->chop(length - kAesBytes128);

    CryptDestroyKey(hKey);
    CryptReleaseContext(hProvider, 0);
    }@

    but it is working only for the first block of data (so 16 byte), for the remaining data it gives no errors but data are not undecryptable...

    What i'm doing wrong?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      How is that related to Qt?

      Here you can use "QCA":http://delta.affinix.com/qca/.

      (Z(:^

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rileo8
        wrote on last edited by
        #3

        I'm developing for windows ce platform
        I tried to compile openssl for windows ce but without success so there is no way i can use qca i think...

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved