[與此頁面相關聯的功能 Windows Media Format 11 SDK是舊版功能。 來源讀取器 和 接收寫入器已取代它。 來源讀取器 和 接收寫入器 已針對 Windows 10 和 Windows 11 優化。 Microsoft強烈建議新程式代碼盡可能使用 來源讀取器 和 接收寫入器,而不是 Windows Media Format 11 SDK。 Microsoft建議使用舊版 API 的現有程式代碼,盡可能改寫成使用新的 API。]
會話金鑰是用來保護內容金鑰。 下列程式代碼示範如何建立會話密鑰,但會排除隨機密鑰產生的詳細數據。
HRESULT CreateSessionKey( WMDRM_IMPORT_SESSION_KEY **ppSessionKey, DWORD *pcbSessionKey )
{
// TODO: Set this value to the desired number of bytes for the session key data.
const DWORD SESSION_KEY_DATA_SIZE = 20;
HRESULT hr = S_OK;
WMDRM_IMPORT_SESSION_KEY *pSessionKey = NULL;
if( NULL == ppSessionKey )
{
hr = E_POINTER;
goto EXIT;
}
// Compute the size of the session key structure plus the session key.
DWORD cbSessionKey = sizeof( WMDRM_IMPORT_SESSION_KEY ) + SESSION_KEY_DATA_SIZE;
// Allocate memory for the session key.
pSessionKey = (WMDRM_IMPORT_SESSION_KEY *)new BYTE[ cbSessionKey ];
if( NULL == pSessionKey )
{
hr = E_OUTOFMEMORY;
goto EXIT;
}
ZeroMemory( pSessionKey, cbSessionKey );
// Set the key type and the key size.
pSessionKey->dwKeyType = WMDRM_KEYTYPE_RC4;
pSessionKey->cbKey = SESSION_KEY_DATA_SIZE;
// Set the key to a random value. Note that the random number
// generator is not very good. It is only intended to be demonstrative
// and it is not recommended for use in shipping code.
srand((unsigned int)time(NULL));
BYTE* pKeyData = pSessionKey->rgbKey;
for (size_t i = 0; i < SESSION_KEY_DATA_SIZE; ++i)
{
*pKeyData++ = (BYTE)(rand() & 0xFF);
}
// If all has been successful set the parameters.
*ppSessionKey = pSessionKey;
pSessionKey = NULL;
*pcbSessionKey = cbSessionKey;
EXIT:
SAFE_ARRAY_DELETE( pSessionKey );
return hr;
}
相關主題