ホーム › Security.Cryptography › BCryptEncapsulate
BCryptEncapsulate
関数公開鍵を用いて共有秘密を生成し暗号文にカプセル化する。
シグネチャ
// bcrypt.dll
#include <windows.h>
NTSTATUS BCryptEncapsulate(
BCRYPT_KEY_HANDLE hKey,
BYTE* pbSecretKey, // optional
DWORD cbSecretKey,
DWORD* pcbSecretKey,
BYTE* pbCipherText, // optional
DWORD cbCipherText,
DWORD* pcbCipherText,
DWORD dwFlags
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| hKey | BCRYPT_KEY_HANDLE | in |
| pbSecretKey | BYTE* | outoptional |
| cbSecretKey | DWORD | in |
| pcbSecretKey | DWORD* | out |
| pbCipherText | BYTE* | outoptional |
| cbCipherText | DWORD | in |
| pcbCipherText | DWORD* | out |
| dwFlags | DWORD | in |
戻り値の型: NTSTATUS
各言語での呼び出し定義
// bcrypt.dll
#include <windows.h>
NTSTATUS BCryptEncapsulate(
BCRYPT_KEY_HANDLE hKey,
BYTE* pbSecretKey, // optional
DWORD cbSecretKey,
DWORD* pcbSecretKey,
BYTE* pbCipherText, // optional
DWORD cbCipherText,
DWORD* pcbCipherText,
DWORD dwFlags
);[DllImport("bcrypt.dll", ExactSpelling = true)]
static extern int BCryptEncapsulate(
IntPtr hKey, // BCRYPT_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("bcrypt.dll", ExactSpelling:=True)>
Public Shared Function BCryptEncapsulate(
hKey As IntPtr, ' BCRYPT_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 : BCRYPT_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 BCryptEncapsulate Lib "bcrypt" ( _
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
BCryptEncapsulate = ctypes.windll.bcrypt.BCryptEncapsulate
BCryptEncapsulate.restype = ctypes.c_int
BCryptEncapsulate.argtypes = [
wintypes.HANDLE, # hKey : BCRYPT_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('bcrypt.dll')
BCryptEncapsulate = Fiddle::Function.new(
lib['BCryptEncapsulate'],
[
Fiddle::TYPE_VOIDP, # hKey : BCRYPT_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 = "bcrypt")]
extern "system" {
fn BCryptEncapsulate(
hKey: *mut core::ffi::c_void, // BCRYPT_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("bcrypt.dll")]
public static extern int BCryptEncapsulate(IntPtr hKey, IntPtr pbSecretKey, uint cbSecretKey, out uint pcbSecretKey, IntPtr pbCipherText, uint cbCipherText, out uint pcbCipherText, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'bcrypt_BCryptEncapsulate' -Namespace Win32 -PassThru
# $api::BCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags)#uselib "bcrypt.dll"
#func global BCryptEncapsulate "BCryptEncapsulate" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; BCryptEncapsulate hKey, varptr(pbSecretKey), cbSecretKey, varptr(pcbSecretKey), varptr(pbCipherText), cbCipherText, varptr(pcbCipherText), dwFlags ; 戻り値は stat
; hKey : BCRYPT_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 "bcrypt.dll" #cfunc global BCryptEncapsulate "BCryptEncapsulate" sptr, var, int, var, var, int, var, int ; res = BCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags) ; hKey : BCRYPT_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 方式にも切替可。#uselib "bcrypt.dll" #cfunc global BCryptEncapsulate "BCryptEncapsulate" sptr, sptr, int, sptr, sptr, int, sptr, int ; res = BCryptEncapsulate(hKey, varptr(pbSecretKey), cbSecretKey, varptr(pcbSecretKey), varptr(pbCipherText), cbCipherText, varptr(pcbCipherText), dwFlags) ; hKey : BCRYPT_KEY_HANDLE -> "sptr" ; pbSecretKey : BYTE* optional, out -> "sptr" ; cbSecretKey : DWORD -> "int" ; pcbSecretKey : DWORD* out -> "sptr" ; pbCipherText : BYTE* optional, out -> "sptr" ; cbCipherText : DWORD -> "int" ; pcbCipherText : DWORD* out -> "sptr" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; NTSTATUS BCryptEncapsulate(BCRYPT_KEY_HANDLE hKey, BYTE* pbSecretKey, DWORD cbSecretKey, DWORD* pcbSecretKey, BYTE* pbCipherText, DWORD cbCipherText, DWORD* pcbCipherText, DWORD dwFlags) #uselib "bcrypt.dll" #cfunc global BCryptEncapsulate "BCryptEncapsulate" intptr, var, int, var, var, int, var, int ; res = BCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags) ; hKey : BCRYPT_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 方式にも切替可。; NTSTATUS BCryptEncapsulate(BCRYPT_KEY_HANDLE hKey, BYTE* pbSecretKey, DWORD cbSecretKey, DWORD* pcbSecretKey, BYTE* pbCipherText, DWORD cbCipherText, DWORD* pcbCipherText, DWORD dwFlags) #uselib "bcrypt.dll" #cfunc global BCryptEncapsulate "BCryptEncapsulate" intptr, intptr, int, intptr, intptr, int, intptr, int ; res = BCryptEncapsulate(hKey, varptr(pbSecretKey), cbSecretKey, varptr(pcbSecretKey), varptr(pbCipherText), cbCipherText, varptr(pcbCipherText), dwFlags) ; hKey : BCRYPT_KEY_HANDLE -> "intptr" ; pbSecretKey : BYTE* optional, out -> "intptr" ; cbSecretKey : DWORD -> "int" ; pcbSecretKey : DWORD* out -> "intptr" ; pbCipherText : BYTE* optional, out -> "intptr" ; cbCipherText : DWORD -> "int" ; pcbCipherText : DWORD* out -> "intptr" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
bcrypt = windows.NewLazySystemDLL("bcrypt.dll")
procBCryptEncapsulate = bcrypt.NewProc("BCryptEncapsulate")
)
// hKey (BCRYPT_KEY_HANDLE), pbSecretKey (BYTE* optional, out), cbSecretKey (DWORD), pcbSecretKey (DWORD* out), pbCipherText (BYTE* optional, out), cbCipherText (DWORD), pcbCipherText (DWORD* out), dwFlags (DWORD)
r1, _, err := procBCryptEncapsulate.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 // NTSTATUSfunction BCryptEncapsulate(
hKey: THandle; // BCRYPT_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 'bcrypt.dll' name 'BCryptEncapsulate';result := DllCall("bcrypt\BCryptEncapsulate"
, "Ptr", hKey ; BCRYPT_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: NTSTATUS●BCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags) = DLL("bcrypt.dll", "int BCryptEncapsulate(void*, void*, dword, void*, void*, dword, void*, dword)")
# 呼び出し: BCryptEncapsulate(hKey, pbSecretKey, cbSecretKey, pcbSecretKey, pbCipherText, cbCipherText, pcbCipherText, dwFlags)
# hKey : BCRYPT_KEY_HANDLE -> "void*"
# 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)。