Oturum Anahtarı Oluşturma Örneği

[Windows Media Format 11 SDK bu sayfayla ilişkilendirilmiş özellik eski bir özelliktir. Kaynak Okuyucu ve Havuz Yazıcısıtarafından değiştirildi. Kaynak Okuyucu ve Havuz Yazıcısı Windows 10 ve Windows 11 için iyileştirilmiştir. Microsoft, yeni kodun mümkün olduğunda Windows Media Format 11 SDKyerine Kaynak Okuyucu ve Havuz Yazıcı kullanmasını kesinlikle önerir. Microsoft, mümkünse yeni API'leri kullanmak için eski API'leri kullanan mevcut kodun yeniden yazılmasını önerir.]

Oturum anahtarı, içerik anahtarını korumak için kullanılır. Aşağıdaki kod, oturum anahtarının nasıl oluşturulacağını gösterir, ancak rastgele anahtar oluşturmanın ayrıntılarını dışarıda bırakır.

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;
}

DRM İçeri Aktarma Örneklerini