NCryptStreamUpdate 函式 (ncryptprotect.h)

NCryptStreamUpdate 函式會加密和解密數據區塊。

語法

SECURITY_STATUS NCryptStreamUpdate(
  [in] NCRYPT_STREAM_HANDLE hStream,
  [in] const BYTE           *pbData,
       SIZE_T               cbData,
       BOOL                 fFinal
);

參數

[in] hStream

呼叫 NCryptStreamOpenToProtectNCryptStreamOpenToUnprotect 所建立的數據流物件句柄。

[in] pbData

要處理的位元組陣組指標。

cbData

pbData 參數所指定之二進位陣列中的位元元組數目。

fFinal

布爾值,指定是否已處理最後一個數據區塊。

傳回值

傳回狀態代碼,指出函式的成功或失敗。 可能的傳回碼包括但不限於下列專案。

傳回碼 Description
ERROR_SUCCESS
函式成功。
NTE_BAD_DATA
無法譯碼內容。
NTE_INVALID_HANDLE
hStream 參數指向的數據流句柄無效。
NTE_NO_MEMORY
記憶體不足,無法處理內容。

備註

您必須呼叫 NCryptStreamOpenToProtectNCryptStreamOpenToUnprotect,才能呼叫 NCryptStreamUpdate 之前開啟數據流

訊息可能很大,因此將整個訊息儲存在記憶體中可能會很困難,因此一次處理它們。 不過,將要處理的數據分割成可管理的區塊,即可處理大型訊息。

若要這樣做,請在迴圈中使用 NCryptStreamUpdate ,逐區塊前進到檔案區塊。 處理串流訊息時,產生的輸出數據會使用您指定的回呼函式傳回您的應用程式。 如下列範例所示。 如需回呼函式的詳細資訊,請參閱 PFNCryptStreamOutputCallback

注意 建議您不要使用太小的區塊大小。 小型區塊需要更多呼叫,因此需要更多呼叫額外負荷。 此外,串流 API 已針對較大的區塊優化。 您應該實驗找出您必須處理之數據的最佳區塊大小。
 
BOOL                        fFinal = FALSE;
PBYTE                       pbBuf = NULL;

// Determine the number of bytes to read.
DWORD cbData = GetFileSize( hFileIn, NULL );

// Call NCryptStreamUpdate while there is data left to read.
while(FALSE == fFinal)
{
    // Read dwBlockSize bytes from the file.
    if(dwBlockSize > 1)
    {
        if( !ReadFile(hFileIn, pbBuf, dwBlockSize, &cbResult, NULL) )
        {
            hr = HRESULT_FROM_WIN32(hr);            
            goto CleanUp;
        }
    }

    // Decrement the number of bytes to read by the current amount read.
    cbData -= cbResult;

    // Set fFinal if there are no bytes left to read.
    if (cbData <= 0) fFinal = TRUE;

    // Encrypt (or decrypt) the bytes pointed to by pbBuf
    hr = NCryptStreamUpdate(hStream, pbBuf, cbResult, fFinal); 
    if( FAILED(hr) )
    {            
        goto CleanUp;
    }         
}      

CleanUp:
    if( NULL != hStream )
    {
        NCryptStreamClose(hStream);
    }
    if( NULL != hDescriptor )
    {
        NCryptCloseProtectionDescriptor( hDescriptor );
    }
    if(NULL != pbBuf)
    {
        LocalFree(pbBuf);
        pbBuf = NULL;
    }

規格需求

需求
最低支援的用戶端 Windows 8 [僅限傳統型應用程式]
最低支援的伺服器 Windows Server 2012 [僅限傳統型應用程式]
目標平台 Windows
標頭 ncryptprotect.h
程式庫 NCrypt.lib
Dll NCrypt.dll

另請參閱

CNG DPAPI 函式

NCRYPT_PROTECT_STREAM_INFO

NCryptStreamOpenToProtect

NCryptStreamOpenToUnprotect