ホーム › Security.Cryptography › CryptGenKey
CryptGenKey
関数指定アルゴリズムの暗号鍵またはキーペアを生成する。
シグネチャ
// ADVAPI32.dll
#include <windows.h>
BOOL CryptGenKey(
UINT_PTR hProv,
ALG_ID Algid,
CRYPT_KEY_FLAGS dwFlags,
UINT_PTR* phKey
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| hProv | UINT_PTR | in |
| Algid | ALG_ID | in |
| dwFlags | CRYPT_KEY_FLAGS | in |
| phKey | UINT_PTR* | out |
戻り値の型: BOOL
各言語での呼び出し定義
// ADVAPI32.dll
#include <windows.h>
BOOL CryptGenKey(
UINT_PTR hProv,
ALG_ID Algid,
CRYPT_KEY_FLAGS dwFlags,
UINT_PTR* phKey
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptGenKey(
UIntPtr hProv, // UINT_PTR
uint Algid, // ALG_ID
uint dwFlags, // CRYPT_KEY_FLAGS
out UIntPtr phKey // UINT_PTR* out
);<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptGenKey(
hProv As UIntPtr, ' UINT_PTR
Algid As UInteger, ' ALG_ID
dwFlags As UInteger, ' CRYPT_KEY_FLAGS
<Out> ByRef phKey As UIntPtr ' UINT_PTR* out
) As Boolean
End Function' hProv : UINT_PTR
' Algid : ALG_ID
' dwFlags : CRYPT_KEY_FLAGS
' phKey : UINT_PTR* out
Declare PtrSafe Function CryptGenKey Lib "advapi32" ( _
ByVal hProv As LongPtr, _
ByVal Algid As Long, _
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
CryptGenKey = ctypes.windll.advapi32.CryptGenKey
CryptGenKey.restype = wintypes.BOOL
CryptGenKey.argtypes = [
ctypes.c_size_t, # hProv : UINT_PTR
wintypes.DWORD, # Algid : ALG_ID
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')
CryptGenKey = Fiddle::Function.new(
lib['CryptGenKey'],
[
Fiddle::TYPE_UINTPTR_T, # hProv : UINT_PTR
-Fiddle::TYPE_INT, # Algid : ALG_ID
-Fiddle::TYPE_INT, # dwFlags : CRYPT_KEY_FLAGS
Fiddle::TYPE_VOIDP, # phKey : UINT_PTR* out
],
Fiddle::TYPE_INT)#[link(name = "advapi32")]
extern "system" {
fn CryptGenKey(
hProv: usize, // UINT_PTR
Algid: u32, // ALG_ID
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 CryptGenKey(UIntPtr hProv, uint Algid, uint dwFlags, out UIntPtr phKey);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CryptGenKey' -Namespace Win32 -PassThru
# $api::CryptGenKey(hProv, Algid, dwFlags, phKey)#uselib "ADVAPI32.dll"
#func global CryptGenKey "CryptGenKey" sptr, sptr, sptr, sptr
; CryptGenKey hProv, Algid, dwFlags, varptr(phKey) ; 戻り値は stat
; hProv : UINT_PTR -> "sptr"
; Algid : ALG_ID -> "sptr"
; dwFlags : CRYPT_KEY_FLAGS -> "sptr"
; phKey : UINT_PTR* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "ADVAPI32.dll" #cfunc global CryptGenKey "CryptGenKey" sptr, int, int, var ; res = CryptGenKey(hProv, Algid, dwFlags, phKey) ; hProv : UINT_PTR -> "sptr" ; Algid : ALG_ID -> "int" ; dwFlags : CRYPT_KEY_FLAGS -> "int" ; phKey : UINT_PTR* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ADVAPI32.dll" #cfunc global CryptGenKey "CryptGenKey" sptr, int, int, sptr ; res = CryptGenKey(hProv, Algid, dwFlags, varptr(phKey)) ; hProv : UINT_PTR -> "sptr" ; Algid : ALG_ID -> "int" ; dwFlags : CRYPT_KEY_FLAGS -> "int" ; phKey : UINT_PTR* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL CryptGenKey(UINT_PTR hProv, ALG_ID Algid, CRYPT_KEY_FLAGS dwFlags, UINT_PTR* phKey) #uselib "ADVAPI32.dll" #cfunc global CryptGenKey "CryptGenKey" intptr, int, int, var ; res = CryptGenKey(hProv, Algid, dwFlags, phKey) ; hProv : UINT_PTR -> "intptr" ; Algid : ALG_ID -> "int" ; dwFlags : CRYPT_KEY_FLAGS -> "int" ; phKey : UINT_PTR* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL CryptGenKey(UINT_PTR hProv, ALG_ID Algid, CRYPT_KEY_FLAGS dwFlags, UINT_PTR* phKey) #uselib "ADVAPI32.dll" #cfunc global CryptGenKey "CryptGenKey" intptr, int, int, intptr ; res = CryptGenKey(hProv, Algid, dwFlags, varptr(phKey)) ; hProv : UINT_PTR -> "intptr" ; Algid : ALG_ID -> "int" ; dwFlags : CRYPT_KEY_FLAGS -> "int" ; phKey : UINT_PTR* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procCryptGenKey = advapi32.NewProc("CryptGenKey")
)
// hProv (UINT_PTR), Algid (ALG_ID), dwFlags (CRYPT_KEY_FLAGS), phKey (UINT_PTR* out)
r1, _, err := procCryptGenKey.Call(
uintptr(hProv),
uintptr(Algid),
uintptr(dwFlags),
uintptr(phKey),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction CryptGenKey(
hProv: NativeUInt; // UINT_PTR
Algid: DWORD; // ALG_ID
dwFlags: DWORD; // CRYPT_KEY_FLAGS
phKey: Pointer // UINT_PTR* out
): BOOL; stdcall;
external 'ADVAPI32.dll' name 'CryptGenKey';result := DllCall("ADVAPI32\CryptGenKey"
, "UPtr", hProv ; UINT_PTR
, "UInt", Algid ; ALG_ID
, "UInt", dwFlags ; CRYPT_KEY_FLAGS
, "Ptr", phKey ; UINT_PTR* out
, "Int") ; return: BOOL●CryptGenKey(hProv, Algid, dwFlags, phKey) = DLL("ADVAPI32.dll", "bool CryptGenKey(int, dword, dword, void*)")
# 呼び出し: CryptGenKey(hProv, Algid, dwFlags, phKey)
# hProv : UINT_PTR -> "int"
# Algid : ALG_ID -> "dword"
# dwFlags : CRYPT_KEY_FLAGS -> "dword"
# phKey : UINT_PTR* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。