共用方式為


媒體樣本加密範例

[與此頁面相關聯的功能 Windows Media Format 11 SDK是舊版功能。 Source ReaderSink Writer取代了它。 來源讀取器接收寫入器 已針對 Windows 10 和 Windows 11 優化。 Microsoft 強烈建議新程式碼盡可能使用 Source ReaderSink Writer,而不是 Windows Media Format 11 SDK。 Microsoft建議使用舊版 API 的現有程式代碼,盡可能改寫成使用新的 API。]

下列不完整的範例示範如何使用DRM加密來加密媒體範例。 由於空間限制,RC4 加密演算法已排除在範例中。

QWORD GetNextSalt(QWORD qwSalt)
{
   return InterlockedIncrement64( (volatile LONGLONG*)&qwSalt );
}

HRESULT EncryptSample( INSSBuffer *pSample )
{
    HRESULT hr = S_OK;
    INSSBuffer3 *pNSSBuffer3 = NULL;
    QWORD qwSalt = 0;
    BYTE *pbData = NULL;
    DWORD cbData = 0;

    hr = pSample->QueryInterface( IID_INSSBuffer3, (void**)&pNSSBuffer3 );
    if( FAILED( hr ) ) goto EXIT;

    hr = pSample->GetBufferAndLength( &pbData, &cbData );
    if( FAILED( hr ) ) goto EXIT;

    qwSalt = GetNextSalt(qwSalt);

    // TODO: Encrypt the sample by concatenating the initialization vector
    //   and using RC4 encryption.

    hr = pNSSBuffer3->SetProperty( 
        WM_SampleExtensionGUID_SampleProtectionSalt, 
        &qwSalt, sizeof( qwSalt ) );
    if( FAILED( hr ) ) goto EXIT;

EXIT:

    SAFE_RELEASE( pNSSBuffer3 );
    return hr;
}

DRM匯入範例