CryptUnprotectMemory 函式 (dpapi.h)

CryptUnprotectMemory函式會解密使用CryptProtectMemory 函式加密的記憶體。

語法

DPAPI_IMP BOOL CryptUnprotectMemory(
  [in, out] LPVOID pDataIn,
  [in]      DWORD  cbDataIn,
  [in]      DWORD  dwFlags
);

參數

[in, out] pDataIn

要解密之記憶體區塊的指標。 cbData參數會指定函式將嘗試解密的位元組數目。 如果記憶體空間中包含的資料小於指定的位元組數目,則函式會嘗試解密預期區塊以外的資料。 如果大於 cbData 位元組,則只會解密第一個 cbData 位元組。

[in] cbDataIn

要解密的 pData 參數所指向的記憶體位元組數目。 位元組數目必須是 Wincrypt.h 中定義的 CRYPTPROTECTMEMORY_BLOCK_SIZE 常數倍數。

[in] dwFlags

此參數可以是下列其中一個旗標。 加密和解密記憶體時,您必須指定相同的旗標。

意義
CRYPTPROTECTMEMORY_SAME_PROCESS
在同一個進程中加密和解密記憶體。 在不同進程中執行的應用程式將無法解密資料。
CRYPTPROTECTMEMORY_CROSS_PROCESS
在不同的進程中加密和解密記憶體。 在不同的進程中執行的應用程式將能夠解密資料。
CRYPTPROTECTMEMORY_SAME_LOGON
使用相同的登入認證來加密和解密不同進程中的記憶體。 在不同的進程中執行的應用程式將能夠解密資料。 不過,進程必須以加密資料和相同登入會話中的相同使用者身分執行。

傳回值

如果函式成功,函式會傳回 TRUE

如果函式失敗,則會傳回 FALSE。 如需擴充錯誤資訊,請呼叫 GetLastError

備註

使用 CryptProtectMemoryCryptUnprotectMemory 進行密碼加密並不安全,因為資料在加密之前會以純文字的形式存在,且呼叫端隨時解密以供使用。

您必須在相同的開機會話期間加密和解密記憶體。 如果在呼叫 CryptUnprotectMemory 函式之前重新開機電腦,您將無法解密資料。

您必須將相同的旗標傳遞至 CryptUnprotectMemoryCryptProtectMemory。 如果您傳遞不同的旗標, CryptUnprotectMemory 函式 會成功;不過,結果無法預測。

當您完成使用敏感性資訊時,請藉由呼叫 SecureZeroMemory 函式 從記憶體中清除它。

範例

下列範例會呼叫 CryptUnprotectMemory 函式來解密記憶體中的資料。 此範例假設 pEncryptedText 變數指向使用 CryptProtectMemory 函式加密的字串。

#include <windows.h>
#include <stdio.h>
#include <Wincrypt.h>
#include <strsafe.h>
#pragma comment(lib, "crypt32.lib")

void main()
{
    LPWSTR pEncryptedText;  // contains the encrypted text
    DWORD cbEncryptedText;  // number of bytes to which 
	                        // pEncryptedText points

    if (CryptUnprotectMemory(pEncryptedText, cbEncryptedText, 
		CRYPTPROTECTMEMORY_SAME_PROCESS))
    {
        // Use the decrypted string.
    }
    else
    {
        wprintf(L"CryptUnprotectMemory failed: %d\n", 
			GetLastError());
    }

    // Clear and free memory after using
    // the decrypted string or if an error occurs. 
    SecureZeroMemory(pEncryptedText, cbEncryptedText);
    LocalFree(pEncryptedText);
    pEncryptedText = NULL;
}

需求

   
最低支援的用戶端 Windows Vista [傳統型應用程式 |UWP 應用程式]
最低支援的伺服器 Windows Server 2003 [傳統型應用程式 |UWP 應用程式]
目標平台 Windows
標頭 dpapi.h
程式庫 Crypt32.lib
Dll Crypt32.dll

另請參閱

CryptProtectMemory

CryptUnprotectData

RtlDecryptMemory

RtlEncryptMemory