c - Implement Windows CryptoAPI CryptDeriveKey Using OpenSSL APIs -


i have cryptoapi code encrypt\ decrypt given data using aes-128 , key derived password using sha-256.

how can write openssl equivalent implementation able encrypt data it, decrypt cryptoapi , vice versa?

trying use evp_bytestokey evp_aes_128_cbc() , evp_sha256() didn’t work “as is”. (by "doesn't work" mean - can't decrypt cryptoapi's generated encrypted data , vice versa. work decrypt openssl's encrypted data).

any idea or reference?

thank in advance.

here windows cryptoapi code:

// handle default provider.        if(cryptacquirecontext(                    &hcryptprov,                     null,                     ms_enh_rsa_aes_prov,                     prov_rsa_aes,                     crypt_verifycontext))           {                    _tprintf(                              text("a cryptographic provider has been acquired. \n"));           }           else           {                    goto exit_preparecapi;           }           // create hash object.            if(!cryptcreatehash(                    hcryptprov,                     hash_algorithm,                     0,                     0,                     &hhash))           {                    goto exit_preparecapi;           }           // hash in password data.            if(!crypthashdata(                    hhash,                     (byte*) strpassword.c_str(),                    strpassword.length(),                    (dword)0))            {                    goto exit_preparecapi;           }           // derive session key hash object.            if(!cryptderivekey(                    hcryptprov,                     encrypt_algorithm,                     hhash,                      0x00800000 /*128 bit*/,                     &hkey))           {                     goto exit_preparecapi;           }           dword cryptmode = crypt_mode_cbc;            if(!cryptsetkeyparam(                    hkey,                     kp_mode,                     (byte*)&cryptmode,                     0))           {                     goto exit_preparecapi;           }                     if(!cryptgethashparam(                    hhash,                    hp_hashsize,                    (byte *)&dwhashlen,                    &dwhashlensize,                    0))           {                    goto exit_preparecapi;           }            pbhash = new byte[dwhashlen];            if(!cryptgethashparam(                    hhash,                    hp_hashval,                    pbhash,                    &dwhashlen,                    0))           {                    goto exit_preparecapi;           }            securezeromemory( ivbuff, sizeof(ivbuff) );            for(dword = 16, j = 0 ; < dwhashlen ; i++, j++)           {                    ivbuff[j] = pbhash[i];           }            if(!cryptsetkeyparam(                    hkey,                     kp_iv,                     ivbuff,                     0))           {                     goto exit_preparecapi;           }           //           // read data pre-allocated pbbuffer           // // encrypt data.           if(!cryptencrypt(                     hkey,                      null,                      feof,                     0,                      pbbuffer,                      &dwcount,                      dwbufferlen)) {                      goto exit_myencryptfile;           }  exit_myencryptfile:           // cleanup allocated objects 

after all, code worked:

int generatekey(const string& strsecter) {     sha256_ctx sha256ctx; unsigned char hash[sha256_digest_length]; securezeromemory(hash, sizeof hash); sha256_init(&sha256ctx); sha256_update(&sha256ctx, strsecter.c_str(), strsecter.length()); sha256_final(hash, &sha256ctx); memcpy(key, hash, aes_block_size); memcpy(iv, hash + aes_block_size, aes_block_size);  return 0; } 

hope someone.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -