I am using blob in my encryption application to store the encryption key.
I am a beginner in MFC. I am using CryptExportKey()
to export key to the blob. I have put the below code in my Encrypt()
function. See the code in the Encrypt()
function below,
if (CryptGetUserKey(
m_hCryptProv,
AT_KEYEXCHANGE,
&hXchgKey))
{
printf("The key exchange key has been acquired. \n");
}
else
{
printf("Error during CryptGetUserKey exchange key.");
}
if (CryptExportKey(
m_hKey,
hXchgKey,
SIMPLEBLOB,
0,
NULL,
&dwBlobLen))
{
_tprintf(
TEXT("The key BLOB is %d bytes long. \n"),
dwBlobLen);
}
if (pbKeyBlob = (BYTE*)malloc(dwBlobLen))
{
printf("Memory has been allocated for the BLOB. \n");
}
if (CryptExportKey(
m_hKey,
hXchgKey,
SIMPLEBLOB,
0,
pbKeyBlob,
&dwBlobLen))
{
printf("Contents have been written to the BLOB. \n");
}
Also I have put the below code in Decrypt()
function to import the key.
if (!CryptImportKey(
m_hCryptProv,
pbKeyBlob,
dwBlobLen,
0,
0,
&m_hKey))
{
//success
}
The exporting succeeded but importing failed.
My file will be closed after encryption. When loading the application, the decryption also fails since key is not successfully imported. During encryption and decryption the m_hCryptProv
have different values. Is it significant in this case? I am working so longer on this encryption work. Any help appreciated.