CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
delete[] pbKeyBlob;
}
else
{
// Decrypt the file with a session key derived from a password.
// Create a hash object.
// Hash in the password data.
if (!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash)
|| !CryptHashData(hHash, (LPBYTE)lpcszPasswd, _tcslen(lpcszPasswd), 0)
|| !CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey))
{
ASSERT(FALSE);
source.Close();
dest.Close();
CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
// Destroy the hash object.
CryptDestroyHash(hHash);
hHash = 0;
}
// The decryption key is now available, either having been imported
// from a blob read in from the source file or having been created
// using the password. This point in the program is not reached if
// the decryption key is not available.
// Determine the number of bytes to decrypt at a time.
// This must be a multiple of ENCRYPT_BLOCK_SIZE.
// Create the session key.
if (!lpcszPasswd)
{
// No password was passed.
// Encrypt the file with a random session key and write the key to a file.
// Create a random session key.
CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
END_CATCH
}
else
{
// The file will be encrypted with a session key derived from a
// password.
// The session key will be recreated when the file is decrypted
// only if the password used to create the key is available.
// Create a hash object.
if (!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))
{
ASSERT(FALSE);
source.Close();
dest.Close();
// Derive a session key from the hash object.
if (!CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey))
{
ASSERT(FALSE);
source.Close();
dest.Close();
// The session key is now ready. If it is not a key derived from a
// password, the session key encrypted with the encrypter's private
// key has been written to the destination file.
// Determine number of bytes to encrypt at a time.
// This must be a multiple of ENCRYPT_BLOCK_SIZE.
// ENCRYPT_BLOCK_SIZE is set by a #define statement.