ホーム › Security.Cryptography › CryptUnprotectMemory
CryptUnprotectMemory
関数CryptProtectMemoryで保護されたメモリを復号する。
シグネチャ
// CRYPT32.dll
#include <windows.h>
BOOL CryptUnprotectMemory(
void* pDataIn,
DWORD cbDataIn,
DWORD dwFlags
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pDataIn | void* | inout | 復号するメモリブロックへのポインターです。cbData パラメーターは、関数が復号を試みるバイト数を指定します。メモリ領域に含まれるデータが指定されたバイト数より小さい場合、関数は対象ブロックの外側のデータを復号しようとします。cbData バイトより大きい場合は、先頭の cbData バイトのみが復号されます。 |
| cbDataIn | DWORD | in | pData パラメーターが指すメモリのうち、復号するバイト数です。バイト数は、Wincrypt.h で定義されている CRYPTPROTECTMEMORY_BLOCK_SIZE 定数の倍数である必要があります。 |
| dwFlags | DWORD | in | このパラメーターには、次のいずれかのフラグを指定できます。メモリを暗号化するときと復号するときには、同じフラグを指定する必要があります。 |
戻り値の型: BOOL
公式ドキュメント
CryptProtectMemory 関数を使用して暗号化されたメモリを復号します。
戻り値
関数が成功した場合は TRUE を返します。
関数が失敗した場合は FALSE を返します。拡張エラー情報を取得するには、GetLastError を呼び出します。
解説(Remarks)
パスワードの暗号化に CryptProtectMemory および CryptUnprotectMemory を使用することは安全ではありません。なぜなら、データは暗号化される前、および呼び出し側が使用のために復号する間、平文としてメモリ内に存在するためです。
メモリの暗号化と復号は、同じブートセッション中に行う必要があります。CryptUnprotectMemory 関数を呼び出す前にコンピューターが再起動された場合、データを復号できません。
CryptUnprotectMemory と CryptProtectMemory には同じフラグを渡す必要があります。異なるフラグを渡した場合、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;
}
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// CRYPT32.dll
#include <windows.h>
BOOL CryptUnprotectMemory(
void* pDataIn,
DWORD cbDataIn,
DWORD dwFlags
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptUnprotectMemory(
IntPtr pDataIn, // void* in/out
uint cbDataIn, // DWORD
uint dwFlags // DWORD
);<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptUnprotectMemory(
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 CryptUnprotectMemory 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
CryptUnprotectMemory = ctypes.windll.crypt32.CryptUnprotectMemory
CryptUnprotectMemory.restype = wintypes.BOOL
CryptUnprotectMemory.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')
CryptUnprotectMemory = Fiddle::Function.new(
lib['CryptUnprotectMemory'],
[
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 CryptUnprotectMemory(
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 CryptUnprotectMemory(IntPtr pDataIn, uint cbDataIn, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptUnprotectMemory' -Namespace Win32 -PassThru
# $api::CryptUnprotectMemory(pDataIn, cbDataIn, dwFlags)#uselib "CRYPT32.dll"
#func global CryptUnprotectMemory "CryptUnprotectMemory" sptr, sptr, sptr
; CryptUnprotectMemory pDataIn, cbDataIn, dwFlags ; 戻り値は stat
; pDataIn : void* in/out -> "sptr"
; cbDataIn : DWORD -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "CRYPT32.dll"
#cfunc global CryptUnprotectMemory "CryptUnprotectMemory" sptr, int, int
; res = CryptUnprotectMemory(pDataIn, cbDataIn, dwFlags)
; pDataIn : void* in/out -> "sptr"
; cbDataIn : DWORD -> "int"
; dwFlags : DWORD -> "int"; BOOL CryptUnprotectMemory(void* pDataIn, DWORD cbDataIn, DWORD dwFlags)
#uselib "CRYPT32.dll"
#cfunc global CryptUnprotectMemory "CryptUnprotectMemory" intptr, int, int
; res = CryptUnprotectMemory(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")
procCryptUnprotectMemory = crypt32.NewProc("CryptUnprotectMemory")
)
// pDataIn (void* in/out), cbDataIn (DWORD), dwFlags (DWORD)
r1, _, err := procCryptUnprotectMemory.Call(
uintptr(pDataIn),
uintptr(cbDataIn),
uintptr(dwFlags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction CryptUnprotectMemory(
pDataIn: Pointer; // void* in/out
cbDataIn: DWORD; // DWORD
dwFlags: DWORD // DWORD
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CryptUnprotectMemory';result := DllCall("CRYPT32\CryptUnprotectMemory"
, "Ptr", pDataIn ; void* in/out
, "UInt", cbDataIn ; DWORD
, "UInt", dwFlags ; DWORD
, "Int") ; return: BOOL●CryptUnprotectMemory(pDataIn, cbDataIn, dwFlags) = DLL("CRYPT32.dll", "bool CryptUnprotectMemory(void*, dword, dword)")
# 呼び出し: CryptUnprotectMemory(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 CryptUnprotectMemory(
pDataIn: ?*anyopaque, // void* in/out
cbDataIn: u32, // DWORD
dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;proc CryptUnprotectMemory(
pDataIn: pointer, # void* in/out
cbDataIn: uint32, # DWORD
dwFlags: uint32 # DWORD
): int32 {.importc: "CryptUnprotectMemory", stdcall, dynlib: "CRYPT32.dll".}pragma(lib, "crypt32");
extern(Windows)
int CryptUnprotectMemory(
void* pDataIn, // void* in/out
uint cbDataIn, // DWORD
uint dwFlags // DWORD
);ccall((:CryptUnprotectMemory, "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 CryptUnprotectMemory(
void* pDataIn,
uint32_t cbDataIn,
uint32_t dwFlags);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CryptUnprotectMemory(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 CryptUnprotectMemory = lib.func('__stdcall', 'CryptUnprotectMemory', 'int32_t', ['void *', 'uint32_t', 'uint32_t']);
// CryptUnprotectMemory(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", {
CryptUnprotectMemory: { parameters: ["pointer", "u32", "u32"], result: "i32" },
});
// lib.symbols.CryptUnprotectMemory(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 CryptUnprotectMemory(
void* pDataIn,
uint32_t cbDataIn,
uint32_t dwFlags);
C, "CRYPT32.dll");
// $ffi->CryptUnprotectMemory(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 CryptUnprotectMemory(
Pointer pDataIn, // void* in/out
int cbDataIn, // DWORD
int dwFlags // DWORD
);
}@[Link("crypt32")]
lib LibCRYPT32
fun CryptUnprotectMemory = CryptUnprotectMemory(
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 CryptUnprotectMemoryNative = Int32 Function(Pointer<Void>, Uint32, Uint32);
typedef CryptUnprotectMemoryDart = int Function(Pointer<Void>, int, int);
final CryptUnprotectMemory = DynamicLibrary.open('CRYPT32.dll')
.lookupFunction<CryptUnprotectMemoryNative, CryptUnprotectMemoryDart>('CryptUnprotectMemory');
// pDataIn : void* in/out -> Pointer<Void>
// cbDataIn : DWORD -> Uint32
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CryptUnprotectMemory(
pDataIn: Pointer; // void* in/out
cbDataIn: DWORD; // DWORD
dwFlags: DWORD // DWORD
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CryptUnprotectMemory';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CryptUnprotectMemory"
c_CryptUnprotectMemory :: 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 cryptunprotectmemory =
foreign "CryptUnprotectMemory"
((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 ("CryptUnprotectMemory" crypt-unprotect-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 $CryptUnprotectMemory = Win32::API::More->new('CRYPT32',
'BOOL CryptUnprotectMemory(LPVOID pDataIn, DWORD cbDataIn, DWORD dwFlags)');
# my $ret = $CryptUnprotectMemory->Call($pDataIn, $cbDataIn, $dwFlags);
# pDataIn : void* in/out -> LPVOID
# cbDataIn : DWORD -> DWORD
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。