Win32 API 日本語リファレンス
ホームSecurity.Cryptography › CryptDecrypt

CryptDecrypt

関数
指定した鍵で暗号化データを復号する。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// ADVAPI32.dll
#include <windows.h>

BOOL CryptDecrypt(
    UINT_PTR hKey,
    UINT_PTR hHash,
    BOOL Final,
    DWORD dwFlags,
    BYTE* pbData,
    DWORD* pdwDataLen
);

パラメーター

名前方向
hKeyUINT_PTRin
hHashUINT_PTRin
FinalBOOLin
dwFlagsDWORDin
pbDataBYTE*inout
pdwDataLenDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

// ADVAPI32.dll
#include <windows.h>

BOOL CryptDecrypt(
    UINT_PTR hKey,
    UINT_PTR hHash,
    BOOL Final,
    DWORD dwFlags,
    BYTE* pbData,
    DWORD* pdwDataLen
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptDecrypt(
    UIntPtr hKey,   // UINT_PTR
    UIntPtr hHash,   // UINT_PTR
    bool Final,   // BOOL
    uint dwFlags,   // DWORD
    IntPtr pbData,   // BYTE* in/out
    ref uint pdwDataLen   // DWORD* in/out
);
<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptDecrypt(
    hKey As UIntPtr,   ' UINT_PTR
    hHash As UIntPtr,   ' UINT_PTR
    Final As Boolean,   ' BOOL
    dwFlags As UInteger,   ' DWORD
    pbData As IntPtr,   ' BYTE* in/out
    ByRef pdwDataLen As UInteger   ' DWORD* in/out
) As Boolean
End Function
' hKey : UINT_PTR
' hHash : UINT_PTR
' Final : BOOL
' dwFlags : DWORD
' pbData : BYTE* in/out
' pdwDataLen : DWORD* in/out
Declare PtrSafe Function CryptDecrypt Lib "advapi32" ( _
    ByVal hKey As LongPtr, _
    ByVal hHash As LongPtr, _
    ByVal Final As Long, _
    ByVal dwFlags As Long, _
    ByVal pbData As LongPtr, _
    ByRef pdwDataLen As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptDecrypt = ctypes.windll.advapi32.CryptDecrypt
CryptDecrypt.restype = wintypes.BOOL
CryptDecrypt.argtypes = [
    ctypes.c_size_t,  # hKey : UINT_PTR
    ctypes.c_size_t,  # hHash : UINT_PTR
    wintypes.BOOL,  # Final : BOOL
    wintypes.DWORD,  # dwFlags : DWORD
    ctypes.POINTER(ctypes.c_ubyte),  # pbData : BYTE* in/out
    ctypes.POINTER(wintypes.DWORD),  # pdwDataLen : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
CryptDecrypt = Fiddle::Function.new(
  lib['CryptDecrypt'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hKey : UINT_PTR
    Fiddle::TYPE_UINTPTR_T,  # hHash : UINT_PTR
    Fiddle::TYPE_INT,  # Final : BOOL
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
    Fiddle::TYPE_VOIDP,  # pbData : BYTE* in/out
    Fiddle::TYPE_VOIDP,  # pdwDataLen : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn CryptDecrypt(
        hKey: usize,  // UINT_PTR
        hHash: usize,  // UINT_PTR
        Final: i32,  // BOOL
        dwFlags: u32,  // DWORD
        pbData: *mut u8,  // BYTE* in/out
        pdwDataLen: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true)]
public static extern bool CryptDecrypt(UIntPtr hKey, UIntPtr hHash, bool Final, uint dwFlags, IntPtr pbData, ref uint pdwDataLen);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CryptDecrypt' -Namespace Win32 -PassThru
# $api::CryptDecrypt(hKey, hHash, Final, dwFlags, pbData, pdwDataLen)
#uselib "ADVAPI32.dll"
#func global CryptDecrypt "CryptDecrypt" sptr, sptr, sptr, sptr, sptr, sptr
; CryptDecrypt hKey, hHash, Final, dwFlags, varptr(pbData), varptr(pdwDataLen)   ; 戻り値は stat
; hKey : UINT_PTR -> "sptr"
; hHash : UINT_PTR -> "sptr"
; Final : BOOL -> "sptr"
; dwFlags : DWORD -> "sptr"
; pbData : BYTE* in/out -> "sptr"
; pdwDataLen : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CryptDecrypt "CryptDecrypt" sptr, sptr, int, int, var, var
; res = CryptDecrypt(hKey, hHash, Final, dwFlags, pbData, pdwDataLen)
; hKey : UINT_PTR -> "sptr"
; hHash : UINT_PTR -> "sptr"
; Final : BOOL -> "int"
; dwFlags : DWORD -> "int"
; pbData : BYTE* in/out -> "var"
; pdwDataLen : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptDecrypt(UINT_PTR hKey, UINT_PTR hHash, BOOL Final, DWORD dwFlags, BYTE* pbData, DWORD* pdwDataLen)
#uselib "ADVAPI32.dll"
#cfunc global CryptDecrypt "CryptDecrypt" intptr, intptr, int, int, var, var
; res = CryptDecrypt(hKey, hHash, Final, dwFlags, pbData, pdwDataLen)
; hKey : UINT_PTR -> "intptr"
; hHash : UINT_PTR -> "intptr"
; Final : BOOL -> "int"
; dwFlags : DWORD -> "int"
; pbData : BYTE* in/out -> "var"
; pdwDataLen : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCryptDecrypt = advapi32.NewProc("CryptDecrypt")
)

// hKey (UINT_PTR), hHash (UINT_PTR), Final (BOOL), dwFlags (DWORD), pbData (BYTE* in/out), pdwDataLen (DWORD* in/out)
r1, _, err := procCryptDecrypt.Call(
	uintptr(hKey),
	uintptr(hHash),
	uintptr(Final),
	uintptr(dwFlags),
	uintptr(pbData),
	uintptr(pdwDataLen),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptDecrypt(
  hKey: NativeUInt;   // UINT_PTR
  hHash: NativeUInt;   // UINT_PTR
  Final: BOOL;   // BOOL
  dwFlags: DWORD;   // DWORD
  pbData: Pointer;   // BYTE* in/out
  pdwDataLen: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CryptDecrypt';
result := DllCall("ADVAPI32\CryptDecrypt"
    , "UPtr", hKey   ; UINT_PTR
    , "UPtr", hHash   ; UINT_PTR
    , "Int", Final   ; BOOL
    , "UInt", dwFlags   ; DWORD
    , "Ptr", pbData   ; BYTE* in/out
    , "Ptr", pdwDataLen   ; DWORD* in/out
    , "Int")   ; return: BOOL
●CryptDecrypt(hKey, hHash, Final, dwFlags, pbData, pdwDataLen) = DLL("ADVAPI32.dll", "bool CryptDecrypt(int, int, bool, dword, void*, void*)")
# 呼び出し: CryptDecrypt(hKey, hHash, Final, dwFlags, pbData, pdwDataLen)
# hKey : UINT_PTR -> "int"
# hHash : UINT_PTR -> "int"
# Final : BOOL -> "bool"
# dwFlags : DWORD -> "dword"
# pbData : BYTE* in/out -> "void*"
# pdwDataLen : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。