I am writing a native-C program that (among other things) handles data encryption / decryption. Data is processed (before or after execution) using a python script, the main purpose of which is to encrypt / decrypt the data (and, possibly, some additional parses).
I am having problems decrypting data created using the WinCrypt API using the PyCrypto API and vice versa. In particular, I ran into this problem in AES-256 based encryption. Both sides of the encryption / decryption scheme work on the same computer and have the same encryption key. They are based on CBC and (if the documentation does not lie) are initialized with an initialization vector based on 0. However, no matter what I try to do, they do not seem to get along with each other. The received data (encrypted using WinCrypt and decrypted using PyCrypto or vice versa) is all corrupted (not even close to the original). Just to be clear - each of the mechanisms works on its own (decrypting WinCrypt encrypted data using WinCrypt works fine, and the same goes for PyCrypto).
Native encryption code is based on WinCrypt examples. It usually looks like this:
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
CHAR szPassword[PASSWORD_LENGTH] = "";
DWORD dwLength;
PBYTE pbBuffer = NULL;
DWORD dwBufferLen;
DWORD dwCount;
CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_AES, 0);
CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash);
CryptHashData(hHash, (BYTE *)szPassword, dwLength, 0);
CryptDeriveKey(hCryptProv, CALG_AES_256, hHash, 0, &hKey);
CryptEncrypt(hKey, NULL, FALSE, 0, pbBuffer, &dwCount, dwBufferLen);
CryptDecrypt(hKey, 0, FALSE, 0, pbBuffer, &dwCount);
(This is just a general outline, and obviously the actual code contains all the necessary error handling, handles cleanup, etc.).
PyCrypto Code:
from Crypto.Cipher import AES
context = AES.new("K"*32, mode=AES.MODE_CBC, IV="\x00"*16)
context.encrypt(ORIGINAL_DATA)
context.decrypt(ENC_DATA)