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

CryptImportKey

関数
キーBLOBから暗号鍵をプロバイダーへインポートする。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CryptImportKey(
    UINT_PTR hProv,
    const BYTE* pbData,
    DWORD dwDataLen,
    UINT_PTR hPubKey,
    CRYPT_KEY_FLAGS dwFlags,
    UINT_PTR* phKey
);

パラメーター

名前方向
hProvUINT_PTRin
pbDataBYTE*in
dwDataLenDWORDin
hPubKeyUINT_PTRin
dwFlagsCRYPT_KEY_FLAGSin
phKeyUINT_PTR*out

戻り値の型: BOOL

各言語での呼び出し定義

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

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

CryptImportKey = ctypes.windll.advapi32.CryptImportKey
CryptImportKey.restype = wintypes.BOOL
CryptImportKey.argtypes = [
    ctypes.c_size_t,  # hProv : UINT_PTR
    ctypes.POINTER(ctypes.c_ubyte),  # pbData : BYTE*
    wintypes.DWORD,  # dwDataLen : DWORD
    ctypes.c_size_t,  # hPubKey : UINT_PTR
    wintypes.DWORD,  # dwFlags : CRYPT_KEY_FLAGS
    ctypes.POINTER(ctypes.c_size_t),  # phKey : UINT_PTR* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
CryptImportKey = Fiddle::Function.new(
  lib['CryptImportKey'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hProv : UINT_PTR
    Fiddle::TYPE_VOIDP,  # pbData : BYTE*
    -Fiddle::TYPE_INT,  # dwDataLen : DWORD
    Fiddle::TYPE_UINTPTR_T,  # hPubKey : UINT_PTR
    -Fiddle::TYPE_INT,  # dwFlags : CRYPT_KEY_FLAGS
    Fiddle::TYPE_VOIDP,  # phKey : UINT_PTR* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn CryptImportKey(
        hProv: usize,  // UINT_PTR
        pbData: *const u8,  // BYTE*
        dwDataLen: u32,  // DWORD
        hPubKey: usize,  // UINT_PTR
        dwFlags: u32,  // CRYPT_KEY_FLAGS
        phKey: *mut usize  // UINT_PTR* 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 CryptImportKey(UIntPtr hProv, IntPtr pbData, uint dwDataLen, UIntPtr hPubKey, uint dwFlags, out UIntPtr phKey);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CryptImportKey' -Namespace Win32 -PassThru
# $api::CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey)
#uselib "ADVAPI32.dll"
#func global CryptImportKey "CryptImportKey" sptr, sptr, sptr, sptr, sptr, sptr
; CryptImportKey hProv, varptr(pbData), dwDataLen, hPubKey, dwFlags, varptr(phKey)   ; 戻り値は stat
; hProv : UINT_PTR -> "sptr"
; pbData : BYTE* -> "sptr"
; dwDataLen : DWORD -> "sptr"
; hPubKey : UINT_PTR -> "sptr"
; dwFlags : CRYPT_KEY_FLAGS -> "sptr"
; phKey : UINT_PTR* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CryptImportKey "CryptImportKey" sptr, var, int, sptr, int, var
; res = CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey)
; hProv : UINT_PTR -> "sptr"
; pbData : BYTE* -> "var"
; dwDataLen : DWORD -> "int"
; hPubKey : UINT_PTR -> "sptr"
; dwFlags : CRYPT_KEY_FLAGS -> "int"
; phKey : UINT_PTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptImportKey(UINT_PTR hProv, BYTE* pbData, DWORD dwDataLen, UINT_PTR hPubKey, CRYPT_KEY_FLAGS dwFlags, UINT_PTR* phKey)
#uselib "ADVAPI32.dll"
#cfunc global CryptImportKey "CryptImportKey" intptr, var, int, intptr, int, var
; res = CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey)
; hProv : UINT_PTR -> "intptr"
; pbData : BYTE* -> "var"
; dwDataLen : DWORD -> "int"
; hPubKey : UINT_PTR -> "intptr"
; dwFlags : CRYPT_KEY_FLAGS -> "int"
; phKey : UINT_PTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCryptImportKey = advapi32.NewProc("CryptImportKey")
)

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