AES128 encrypt problems



  • 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?


  • Moderators

    How is that related to Qt?

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



  • 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...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.