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

NCryptEncapsulate

関数
キーカプセル化により共有秘密と暗号文を生成する。
DLLncrypt.dll呼出規約winapi

シグネチャ

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

HRESULT NCryptEncapsulate(
    NCRYPT_KEY_HANDLE hKey,
    BYTE* pbSecretKey,   // optional
    DWORD cbSecretKey,
    DWORD* pcbSecretKey,
    BYTE* pbCipherText,   // optional
    DWORD cbCipherText,
    DWORD* pcbCipherText,
    DWORD dwFlags
);

パラメーター

名前方向
hKeyNCRYPT_KEY_HANDLEin
pbSecretKeyBYTE*outoptional
cbSecretKeyDWORDin
pcbSecretKeyDWORD*out
pbCipherTextBYTE*outoptional
cbCipherTextDWORDin
pcbCipherTextDWORD*out
dwFlagsDWORDin

戻り値の型: HRESULT

各言語での呼び出し定義

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

HRESULT NCryptEncapsulate(
    NCRYPT_KEY_HANDLE hKey,
    BYTE* pbSecretKey,   // optional
    DWORD cbSecretKey,
    DWORD* pcbSecretKey,
    BYTE* pbCipherText,   // optional
    DWORD cbCipherText,
    DWORD* pcbCipherText,
    DWORD dwFlags
);
[DllImport("ncrypt.dll", ExactSpelling = true)]
static extern int NCryptEncapsulate(
    UIntPtr hKey,   // NCRYPT_KEY_HANDLE
    IntPtr pbSecretKey,   // BYTE* optional, out
    uint cbSecretKey,   // DWORD
    out uint pcbSecretKey,   // DWORD* out
    IntPtr pbCipherText,   // BYTE* optional, out
    uint cbCipherText,   // DWORD
    out uint pcbCipherText,   // DWORD* out
    uint dwFlags   // DWORD
);
<DllImport("ncrypt.dll", ExactSpelling:=True)>
Public Shared Function NCryptEncapsulate(
    hKey As UIntPtr,   ' NCRYPT_KEY_HANDLE
    pbSecretKey As IntPtr,   ' BYTE* optional, out
    cbSecretKey As UInteger,   ' DWORD
    <Out> ByRef pcbSecretKey As UInteger,   ' DWORD* out
    pbCipherText As IntPtr,   ' BYTE* optional, out
    cbCipherText As UInteger,   ' DWORD
    <Out> ByRef pcbCipherText As UInteger,   ' DWORD* out
    dwFlags As UInteger   ' DWORD
) As Integer
End Function
' hKey : NCRYPT_KEY_HANDLE
' pbSecretKey : BYTE* optional, out
' cbSecretKey : DWORD
' pcbSecretKey : DWORD* out
' pbCipherText : BYTE* optional, out
' cbCipherText : DWORD
' pcbCipherText : DWORD* out
' dwFlags : DWORD
Declare PtrSafe Function NCryptEncapsulate Lib "ncrypt" ( _
    ByVal hKey As LongPtr, _
    ByVal pbSecretKey As LongPtr, _
    ByVal cbSecretKey As Long, _
    ByRef pcbSecretKey As Long, _
    ByVal pbCipherText As LongPtr, _
    ByVal cbCipherText As Long, _
    ByRef pcbCipherText 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

NCryptEncapsulate = ctypes.windll.ncrypt.NCryptEncapsulate
NCryptEncapsulate.restype = ctypes.c_int
NCryptEncapsulate.argtypes = [
    ctypes.c_size_t,  # hKey : NCRYPT_KEY_HANDLE
    ctypes.POINTER(ctypes.c_ubyte),  # pbSecretKey : BYTE* optional, out
    wintypes.DWORD,  # cbSecretKey : DWORD
    ctypes.POINTER(wintypes.DWORD),  # pcbSecretKey : DWORD* out
    ctypes.POINTER(ctypes.c_ubyte),  # pbCipherText : BYTE* optional, out
    wintypes.DWORD,  # cbCipherText : DWORD
    ctypes.POINTER(wintypes.DWORD),  # pcbCipherText : DWORD* out
    wintypes.DWORD,  # dwFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

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

var (
	ncrypt = windows.NewLazySystemDLL("ncrypt.dll")
	procNCryptEncapsulate = ncrypt.NewProc("NCryptEncapsulate")
)

// hKey (NCRYPT_KEY_HANDLE), pbSecretKey (BYTE* optional, out), cbSecretKey (DWORD), pcbSecretKey (DWORD* out), pbCipherText (BYTE* optional, out), cbCipherText (DWORD), pcbCipherText (DWORD* out), dwFlags (DWORD)
r1, _, err := procNCryptEncapsulate.Call(
	uintptr(hKey),
	uintptr(pbSecretKey),
	uintptr(cbSecretKey),
	uintptr(pcbSecretKey),
	uintptr(pbCipherText),
	uintptr(cbCipherText),
	uintptr(pcbCipherText),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function NCryptEncapsulate(
  hKey: NativeUInt;   // NCRYPT_KEY_HANDLE
  pbSecretKey: Pointer;   // BYTE* optional, out
  cbSecretKey: DWORD;   // DWORD
  pcbSecretKey: Pointer;   // DWORD* out
  pbCipherText: Pointer;   // BYTE* optional, out
  cbCipherText: DWORD;   // DWORD
  pcbCipherText: Pointer;   // DWORD* out
  dwFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'ncrypt.dll' name 'NCryptEncapsulate';
result := DllCall("ncrypt\NCryptEncapsulate"
    , "UPtr", hKey   ; NCRYPT_KEY_HANDLE
    , "Ptr", pbSecretKey   ; BYTE* optional, out
    , "UInt", cbSecretKey   ; DWORD
    , "Ptr", pcbSecretKey   ; DWORD* out
    , "Ptr", pbCipherText   ; BYTE* optional, out
    , "UInt", cbCipherText   ; DWORD
    , "Ptr", pcbCipherText   ; DWORD* out
    , "UInt", dwFlags   ; DWORD
    , "Int")   ; return: HRESULT
●NCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags) = DLL("ncrypt.dll", "int NCryptEncapsulate(int, void*, dword, void*, void*, dword, void*, dword)")
# 呼び出し: NCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags)
# hKey : NCRYPT_KEY_HANDLE -> "int"
# pbSecretKey : BYTE* optional, out -> "void*"
# cbSecretKey : DWORD -> "dword"
# pcbSecretKey : DWORD* out -> "void*"
# pbCipherText : BYTE* optional, out -> "void*"
# cbCipherText : DWORD -> "dword"
# pcbCipherText : DWORD* out -> "void*"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。