i could import session key like this.
BYTE pbBuffer[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34
, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
BYTE iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x0A
, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
HCRYPTPROV hProvider;
HCRYPTKEY hKey;
struct KEYBLOB {
BLOBHEADER bh;
DWORD dwKeyLen;
BYTE bytes[16];
}blob;
blob.bh.bType = PLAINTEXTKEYBLOB;
blob.bh.reserved = 0;
blob.bh.bVersion = CUR_BLOB_VERSION;
blob.bh.aiKeyAlg = CALG_AES_128;
blob.dwKeyLen = 16;
memcpy(blob.bytes, pbBuffer, 16);
if (!CryptAcquireContext(&hProvider, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
printf("CryptAcquireContext Error\n");
return FALSE;
}
if (!CryptImportKey(hProvider, (BYTE*)&blob, sizeof(KEYBLOB), NULL, CRYPT_NO_SALT, &hKey)){
printf("CryptImportKey Error\n");
CryptReleaseContext(hProvider, 0);
return FALSE;
}
if (!CryptSetKeyParam(hKey, KP_IV, iv, 0)){
printf("CryptSetKeyParam2 Error\n");
CryptDestroyKey(hKey);
CryptReleaseContext(hProvider, 0);
return FALSE;
}
BYTE pbDataBuff[1024 * 2 + 1];
DWORD dwSize, dwWritten;
HANDLE hEncryptFile, hDecryptFile;
BOOL bEnd;
hEncryptFile = CreateFile(_T("data.aes"), GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
hDecryptFile = CreateFile(_T("outdata.txt"), GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
while (1) {
ReadFile(hEncryptFile, pbDataBuff, 1024 * 2, &dwSize, NULL);
if (dwSize < 1024 * 2)
bEnd = TRUE;
else
bEnd = FALSE;
if (!CryptDecrypt(hKey, 0, bEnd, 0, pbDataBuff, &dwSize)) {
printf("CryptDecrypt Error\n");
CryptDestroyKey(hKey);
CryptReleaseContext(hProvider, 0);
CloseHandle(hEncryptFile);
CloseHandle(hDecryptFile);
return FALSE;
}
WriteFile(hDecryptFile, pbDataBuff, dwSize, &dwWritten, NULL);
if (bEnd)
break;
}
CryptDestroyKey(hKey);
CryptReleaseContext(hProvider, 0);
CloseHandle(hEncryptFile);
CloseHandle(hDecryptFile);
return TRUE;