CryptProtectMemory
関数シグネチャ
// CRYPT32.dll
#include <windows.h>
BOOL CryptProtectMemory(
void* pDataIn,
DWORD cbDataIn,
DWORD dwFlags
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pDataIn | void* | inout | 暗号化するメモリブロックへのポインター。cbDataIn パラメーターは暗号化されるバイト数を指定します。メモリ領域に格納されているデータが指定したバイト数より小さい場合、対象ブロックの範囲外のデータも暗号化されます。cbDataIn バイトより大きい場合は、先頭の cbDataIn バイトのみが暗号化されます。 |
| cbDataIn | DWORD | in | pData パラメーターが指すメモリのうち、暗号化するバイト数。このバイト数は、Wincrypt.h で定義されている定数 CRYPTPROTECTMEMORY_BLOCK_SIZE の倍数でなければなりません。 |
| dwFlags | DWORD | in | このパラメーターには次のいずれかのフラグを指定できます。メモリの暗号化時と復号化時には同じフラグを指定する必要があります。 |
戻り値の型: BOOL
公式ドキュメント
メモリを暗号化し、プロセス内の機密情報を他者から閲覧されないようにします。
戻り値
関数が成功した場合、TRUE を返します。
関数が失敗した場合、FALSE を返します。拡張エラー情報を取得するには、GetLastError を呼び出してください。
解説(Remarks)
CryptProtectMemory および CryptUnprotectMemory をパスワードの暗号化に使用することは安全ではありません。データは暗号化される前、および呼び出し元が使用のために復号化したときに、平文としてメモリ内に存在するためです。
通常、CryptProtectMemory 関数は、プロセスの実行中に復号化する機密情報を暗号化するために使用します。後で復号化したいデータの保存にはこの関数を使用しないでください。コンピューターが再起動された場合、データを復号化できなくなります。後で復号化するために暗号化データをファイルに保存するには、CryptProtectData 関数を使用してください。
CryptProtectMemory 関数で暗号化したメモリを復号化するには、CryptUnprotectMemory 関数を呼び出してください。機密情報の使用が終わったら、SecureZeroMemory 関数を呼び出してメモリから消去してください。
RPC または LRPC を使用して暗号化データを別のプロセスに渡す場合は、CRYPTPROTECTMEMORY_CROSS_PROCESS または CRYPTPROTECTMEMORY_SAME_LOGON フラグを使用してください。受信側のプロセスはデータを復号化するために同じフラグを指定する必要があります。また、共有メモリを使用する場合もこれらのフラグを使用してください。
クライアントが CRYPTPROTECTMEMORY_SAME_LOGON フラグを使用する場合、サーバーはメモリを復号化する前にクライアントを偽装(RpcImpersonateClient)する必要があります。
例
次の例では、CryptProtectMemory 関数を呼び出して、メモリ内のデータを暗号化します。
#include <windows.h>
#include <stdio.h>
#include <Wincrypt.h>
#define SSN_STR_LEN 12 // includes null
void main()
{
HRESULT hr = S_OK;
LPWSTR pSensitiveText = NULL;
DWORD cbSensitiveText = 0;
DWORD cbPlainText = SSN_STR_LEN*sizeof(WCHAR);
DWORD dwMod = 0;
// Memory to encrypt must be a multiple of CRYPTPROTECTMEMORY_BLOCK_SIZE.
if (dwMod = cbPlainText % CRYPTPROTECTMEMORY_BLOCK_SIZE)
cbSensitiveText = cbPlainText +
(CRYPTPROTECTMEMORY_BLOCK_SIZE - dwMod);
else
cbSensitiveText = cbPlainText;
pSensitiveText = (LPWSTR)LocalAlloc(LPTR, cbSensitiveText);
if (NULL == pSensitiveText)
{
wprintf(L"Memory allocation failed.\n");
return E_OUTOFMEMORY;
}
// Place sensitive string to encrypt in pSensitiveText.
if (!CryptProtectMemory(pSensitiveText, cbSensitiveText,
CRYPTPROTECTMEMORY_SAME_PROCESS))
{
wprintf(L"CryptProtectMemory failed: %d\n", GetLastError());
SecureZeroMemory(pSensitiveText, cbSensitiveText);
LocalFree(pSensitiveText);
pSensitiveText = NULL;
return E_FAIL;
}
// Call CryptUnprotectMemory to decrypt and use the memory.
SecureZeroMemory(pSensitiveText, cbSensitiveText);
LocalFree(pSensitiveText);
pSensitiveText = NULL;
return hr;
}
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// CRYPT32.dll
#include <windows.h>
BOOL CryptProtectMemory(
void* pDataIn,
DWORD cbDataIn,
DWORD dwFlags
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptProtectMemory(
IntPtr pDataIn, // void* in/out
uint cbDataIn, // DWORD
uint dwFlags // DWORD
);<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptProtectMemory(
pDataIn As IntPtr, ' void* in/out
cbDataIn As UInteger, ' DWORD
dwFlags As UInteger ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' pDataIn : void* in/out
' cbDataIn : DWORD
' dwFlags : DWORD
Declare PtrSafe Function CryptProtectMemory Lib "crypt32" ( _
ByVal pDataIn As LongPtr, _
ByVal cbDataIn As Long, _
ByVal dwFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
CryptProtectMemory = ctypes.windll.crypt32.CryptProtectMemory
CryptProtectMemory.restype = wintypes.BOOL
CryptProtectMemory.argtypes = [
ctypes.POINTER(None), # pDataIn : void* in/out
wintypes.DWORD, # cbDataIn : DWORD
wintypes.DWORD, # dwFlags : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('CRYPT32.dll')
CryptProtectMemory = Fiddle::Function.new(
lib['CryptProtectMemory'],
[
Fiddle::TYPE_VOIDP, # pDataIn : void* in/out
-Fiddle::TYPE_INT, # cbDataIn : DWORD
-Fiddle::TYPE_INT, # dwFlags : DWORD
],
Fiddle::TYPE_INT)#[link(name = "crypt32")]
extern "system" {
fn CryptProtectMemory(
pDataIn: *mut (), // void* in/out
cbDataIn: u32, // DWORD
dwFlags: u32 // DWORD
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true)]
public static extern bool CryptProtectMemory(IntPtr pDataIn, uint cbDataIn, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptProtectMemory' -Namespace Win32 -PassThru
# $api::CryptProtectMemory(pDataIn, cbDataIn, dwFlags)#uselib "CRYPT32.dll"
#func global CryptProtectMemory "CryptProtectMemory" sptr, sptr, sptr
; CryptProtectMemory pDataIn, cbDataIn, dwFlags ; 戻り値は stat
; pDataIn : void* in/out -> "sptr"
; cbDataIn : DWORD -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "CRYPT32.dll"
#cfunc global CryptProtectMemory "CryptProtectMemory" sptr, int, int
; res = CryptProtectMemory(pDataIn, cbDataIn, dwFlags)
; pDataIn : void* in/out -> "sptr"
; cbDataIn : DWORD -> "int"
; dwFlags : DWORD -> "int"; BOOL CryptProtectMemory(void* pDataIn, DWORD cbDataIn, DWORD dwFlags)
#uselib "CRYPT32.dll"
#cfunc global CryptProtectMemory "CryptProtectMemory" intptr, int, int
; res = CryptProtectMemory(pDataIn, cbDataIn, dwFlags)
; pDataIn : void* in/out -> "intptr"
; cbDataIn : DWORD -> "int"
; dwFlags : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
procCryptProtectMemory = crypt32.NewProc("CryptProtectMemory")
)
// pDataIn (void* in/out), cbDataIn (DWORD), dwFlags (DWORD)
r1, _, err := procCryptProtectMemory.Call(
uintptr(pDataIn),
uintptr(cbDataIn),
uintptr(dwFlags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction CryptProtectMemory(
pDataIn: Pointer; // void* in/out
cbDataIn: DWORD; // DWORD
dwFlags: DWORD // DWORD
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CryptProtectMemory';result := DllCall("CRYPT32\CryptProtectMemory"
, "Ptr", pDataIn ; void* in/out
, "UInt", cbDataIn ; DWORD
, "UInt", dwFlags ; DWORD
, "Int") ; return: BOOL●CryptProtectMemory(pDataIn, cbDataIn, dwFlags) = DLL("CRYPT32.dll", "bool CryptProtectMemory(void*, dword, dword)")
# 呼び出し: CryptProtectMemory(pDataIn, cbDataIn, dwFlags)
# pDataIn : void* in/out -> "void*"
# cbDataIn : DWORD -> "dword"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "crypt32" fn CryptProtectMemory(
pDataIn: ?*anyopaque, // void* in/out
cbDataIn: u32, // DWORD
dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;proc CryptProtectMemory(
pDataIn: pointer, # void* in/out
cbDataIn: uint32, # DWORD
dwFlags: uint32 # DWORD
): int32 {.importc: "CryptProtectMemory", stdcall, dynlib: "CRYPT32.dll".}pragma(lib, "crypt32");
extern(Windows)
int CryptProtectMemory(
void* pDataIn, // void* in/out
uint cbDataIn, // DWORD
uint dwFlags // DWORD
);ccall((:CryptProtectMemory, "CRYPT32.dll"), stdcall, Int32,
(Ptr{Cvoid}, UInt32, UInt32),
pDataIn, cbDataIn, dwFlags)
# pDataIn : void* in/out -> Ptr{Cvoid}
# cbDataIn : DWORD -> UInt32
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t CryptProtectMemory(
void* pDataIn,
uint32_t cbDataIn,
uint32_t dwFlags);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CryptProtectMemory(pDataIn, cbDataIn, dwFlags)
-- pDataIn : void* in/out
-- cbDataIn : DWORD
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CryptProtectMemory = lib.func('__stdcall', 'CryptProtectMemory', 'int32_t', ['void *', 'uint32_t', 'uint32_t']);
// CryptProtectMemory(pDataIn, cbDataIn, dwFlags)
// pDataIn : void* in/out -> 'void *'
// cbDataIn : DWORD -> 'uint32_t'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("CRYPT32.dll", {
CryptProtectMemory: { parameters: ["pointer", "u32", "u32"], result: "i32" },
});
// lib.symbols.CryptProtectMemory(pDataIn, cbDataIn, dwFlags)
// pDataIn : void* in/out -> "pointer"
// cbDataIn : DWORD -> "u32"
// dwFlags : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t CryptProtectMemory(
void* pDataIn,
uint32_t cbDataIn,
uint32_t dwFlags);
C, "CRYPT32.dll");
// $ffi->CryptProtectMemory(pDataIn, cbDataIn, dwFlags);
// pDataIn : void* in/out
// cbDataIn : DWORD
// dwFlags : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Crypt32 extends StdCallLibrary {
Crypt32 INSTANCE = Native.load("crypt32", Crypt32.class);
boolean CryptProtectMemory(
Pointer pDataIn, // void* in/out
int cbDataIn, // DWORD
int dwFlags // DWORD
);
}@[Link("crypt32")]
lib LibCRYPT32
fun CryptProtectMemory = CryptProtectMemory(
pDataIn : Void*, # void* in/out
cbDataIn : UInt32, # DWORD
dwFlags : UInt32 # DWORD
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CryptProtectMemoryNative = Int32 Function(Pointer<Void>, Uint32, Uint32);
typedef CryptProtectMemoryDart = int Function(Pointer<Void>, int, int);
final CryptProtectMemory = DynamicLibrary.open('CRYPT32.dll')
.lookupFunction<CryptProtectMemoryNative, CryptProtectMemoryDart>('CryptProtectMemory');
// pDataIn : void* in/out -> Pointer<Void>
// cbDataIn : DWORD -> Uint32
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CryptProtectMemory(
pDataIn: Pointer; // void* in/out
cbDataIn: DWORD; // DWORD
dwFlags: DWORD // DWORD
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CryptProtectMemory';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CryptProtectMemory"
c_CryptProtectMemory :: Ptr () -> Word32 -> Word32 -> IO CInt
-- pDataIn : void* in/out -> Ptr ()
-- cbDataIn : DWORD -> Word32
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let cryptprotectmemory =
foreign "CryptProtectMemory"
((ptr void) @-> uint32_t @-> uint32_t @-> returning int32_t)
(* pDataIn : void* in/out -> (ptr void) *)
(* cbDataIn : DWORD -> uint32_t *)
(* dwFlags : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library crypt32 (t "CRYPT32.dll"))
(cffi:use-foreign-library crypt32)
(cffi:defcfun ("CryptProtectMemory" crypt-protect-memory :convention :stdcall) :int32
(p-data-in :pointer) ; void* in/out
(cb-data-in :uint32) ; DWORD
(dw-flags :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CryptProtectMemory = Win32::API::More->new('CRYPT32',
'BOOL CryptProtectMemory(LPVOID pDataIn, DWORD cbDataIn, DWORD dwFlags)');
# my $ret = $CryptProtectMemory->Call($pDataIn, $cbDataIn, $dwFlags);
# pDataIn : void* in/out -> LPVOID
# cbDataIn : DWORD -> DWORD
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f CryptProtectData — 現在のユーザーまたは機械のコンテキストでデータを暗号化保護する。
- f CryptUnprotectMemory — CryptProtectMemoryで保護されたメモリを復号する。
- f RtlDecryptMemory — RtlEncryptMemoryで暗号化したメモリブロックを復号する。
- f RtlEncryptMemory — 指定したメモリブロックの内容をその場で暗号化する。