ホーム › Security.Cryptography › BCryptDestroyKey
BCryptDestroyKey
関数CNGの鍵オブジェクトを破棄する。
シグネチャ
// bcrypt.dll
#include <windows.h>
NTSTATUS BCryptDestroyKey(
BCRYPT_KEY_HANDLE hKey
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hKey | BCRYPT_KEY_HANDLE | inout | 破棄するキーのハンドル。 |
戻り値の型: NTSTATUS
公式ドキュメント
キーを破棄します。
戻り値
関数の成功または失敗を示すステータスコードを返します。
返される可能性のあるコードには、以下のものが含まれますが、これらに限定されません。
| 戻り値コード | 説明 |
|---|---|
| 関数は成功しました。 | |
| hKey パラメーターのキーハンドルが無効です。 |
解説(Remarks)
サポートされているアルゴリズムプロバイダーを使用する場合、BCryptDestroyKey はユーザーモードまたはカーネルモードのいずれからも呼び出すことができます。カーネルモードの呼び出し元は、PASSIVE_LEVEL IRQL または DISPATCH_LEVEL IRQL のいずれかで実行できます。現在の IRQL レベルが DISPATCH_LEVEL の場合、hKey パラメーターで指定されるハンドルは、BCRYPT_PROV_DISPATCH フラグを指定して開かれたプロバイダーが返したアルゴリズムハンドルから派生したものでなければなりません。
この関数をカーネルモードで呼び出すには、Driver Development Kit (DDK) の一部である Cng.lib を使用します。Windows Server 2008 および Windows Vista: この関数をカーネルモードで呼び出すには、Ksecdd.lib を使用します。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// bcrypt.dll
#include <windows.h>
NTSTATUS BCryptDestroyKey(
BCRYPT_KEY_HANDLE hKey
);[DllImport("bcrypt.dll", ExactSpelling = true)]
static extern int BCryptDestroyKey(
IntPtr hKey // BCRYPT_KEY_HANDLE in/out
);<DllImport("bcrypt.dll", ExactSpelling:=True)>
Public Shared Function BCryptDestroyKey(
hKey As IntPtr ' BCRYPT_KEY_HANDLE in/out
) As Integer
End Function' hKey : BCRYPT_KEY_HANDLE in/out
Declare PtrSafe Function BCryptDestroyKey Lib "bcrypt" ( _
ByVal hKey As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
BCryptDestroyKey = ctypes.windll.bcrypt.BCryptDestroyKey
BCryptDestroyKey.restype = ctypes.c_int
BCryptDestroyKey.argtypes = [
wintypes.HANDLE, # hKey : BCRYPT_KEY_HANDLE in/out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('bcrypt.dll')
BCryptDestroyKey = Fiddle::Function.new(
lib['BCryptDestroyKey'],
[
Fiddle::TYPE_VOIDP, # hKey : BCRYPT_KEY_HANDLE in/out
],
Fiddle::TYPE_INT)#[link(name = "bcrypt")]
extern "system" {
fn BCryptDestroyKey(
hKey: *mut core::ffi::c_void // BCRYPT_KEY_HANDLE in/out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("bcrypt.dll")]
public static extern int BCryptDestroyKey(IntPtr hKey);
"@
$api = Add-Type -MemberDefinition $sig -Name 'bcrypt_BCryptDestroyKey' -Namespace Win32 -PassThru
# $api::BCryptDestroyKey(hKey)#uselib "bcrypt.dll"
#func global BCryptDestroyKey "BCryptDestroyKey" sptr
; BCryptDestroyKey hKey ; 戻り値は stat
; hKey : BCRYPT_KEY_HANDLE in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "bcrypt.dll"
#cfunc global BCryptDestroyKey "BCryptDestroyKey" sptr
; res = BCryptDestroyKey(hKey)
; hKey : BCRYPT_KEY_HANDLE in/out -> "sptr"; NTSTATUS BCryptDestroyKey(BCRYPT_KEY_HANDLE hKey)
#uselib "bcrypt.dll"
#cfunc global BCryptDestroyKey "BCryptDestroyKey" intptr
; res = BCryptDestroyKey(hKey)
; hKey : BCRYPT_KEY_HANDLE in/out -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
bcrypt = windows.NewLazySystemDLL("bcrypt.dll")
procBCryptDestroyKey = bcrypt.NewProc("BCryptDestroyKey")
)
// hKey (BCRYPT_KEY_HANDLE in/out)
r1, _, err := procBCryptDestroyKey.Call(
uintptr(hKey),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // NTSTATUSfunction BCryptDestroyKey(
hKey: THandle // BCRYPT_KEY_HANDLE in/out
): Integer; stdcall;
external 'bcrypt.dll' name 'BCryptDestroyKey';result := DllCall("bcrypt\BCryptDestroyKey"
, "Ptr", hKey ; BCRYPT_KEY_HANDLE in/out
, "Int") ; return: NTSTATUS●BCryptDestroyKey(hKey) = DLL("bcrypt.dll", "int BCryptDestroyKey(void*)")
# 呼び出し: BCryptDestroyKey(hKey)
# hKey : BCRYPT_KEY_HANDLE in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "bcrypt" fn BCryptDestroyKey(
hKey: ?*anyopaque // BCRYPT_KEY_HANDLE in/out
) callconv(std.os.windows.WINAPI) i32;proc BCryptDestroyKey(
hKey: pointer # BCRYPT_KEY_HANDLE in/out
): int32 {.importc: "BCryptDestroyKey", stdcall, dynlib: "bcrypt.dll".}pragma(lib, "bcrypt");
extern(Windows)
int BCryptDestroyKey(
void* hKey // BCRYPT_KEY_HANDLE in/out
);ccall((:BCryptDestroyKey, "bcrypt.dll"), stdcall, Int32,
(Ptr{Cvoid},),
hKey)
# hKey : BCRYPT_KEY_HANDLE in/out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t BCryptDestroyKey(
void* hKey);
]]
local bcrypt = ffi.load("bcrypt")
-- bcrypt.BCryptDestroyKey(hKey)
-- hKey : BCRYPT_KEY_HANDLE in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('bcrypt.dll');
const BCryptDestroyKey = lib.func('__stdcall', 'BCryptDestroyKey', 'int32_t', ['void *']);
// BCryptDestroyKey(hKey)
// hKey : BCRYPT_KEY_HANDLE in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("bcrypt.dll", {
BCryptDestroyKey: { parameters: ["pointer"], result: "i32" },
});
// lib.symbols.BCryptDestroyKey(hKey)
// hKey : BCRYPT_KEY_HANDLE in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t BCryptDestroyKey(
void* hKey);
C, "bcrypt.dll");
// $ffi->BCryptDestroyKey(hKey);
// hKey : BCRYPT_KEY_HANDLE in/out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Bcrypt extends StdCallLibrary {
Bcrypt INSTANCE = Native.load("bcrypt", Bcrypt.class);
int BCryptDestroyKey(
Pointer hKey // BCRYPT_KEY_HANDLE in/out
);
}@[Link("bcrypt")]
lib Libbcrypt
fun BCryptDestroyKey = BCryptDestroyKey(
hKey : Void* # BCRYPT_KEY_HANDLE in/out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef BCryptDestroyKeyNative = Int32 Function(Pointer<Void>);
typedef BCryptDestroyKeyDart = int Function(Pointer<Void>);
final BCryptDestroyKey = DynamicLibrary.open('bcrypt.dll')
.lookupFunction<BCryptDestroyKeyNative, BCryptDestroyKeyDart>('BCryptDestroyKey');
// hKey : BCRYPT_KEY_HANDLE in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function BCryptDestroyKey(
hKey: THandle // BCRYPT_KEY_HANDLE in/out
): Integer; stdcall;
external 'bcrypt.dll' name 'BCryptDestroyKey';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "BCryptDestroyKey"
c_BCryptDestroyKey :: Ptr () -> IO Int32
-- hKey : BCRYPT_KEY_HANDLE in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let bcryptdestroykey =
foreign "BCryptDestroyKey"
((ptr void) @-> returning int32_t)
(* hKey : BCRYPT_KEY_HANDLE in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library bcrypt (t "bcrypt.dll"))
(cffi:use-foreign-library bcrypt)
(cffi:defcfun ("BCryptDestroyKey" bcrypt-destroy-key :convention :stdcall) :int32
(h-key :pointer)) ; BCRYPT_KEY_HANDLE in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $BCryptDestroyKey = Win32::API::More->new('bcrypt',
'int BCryptDestroyKey(HANDLE hKey)');
# my $ret = $BCryptDestroyKey->Call($hKey);
# hKey : BCRYPT_KEY_HANDLE in/out -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
公式の関連項目
- f BCryptGenerateKeyPair — 指定アルゴリズムの公開鍵・秘密鍵ペアを生成する。
- f BCryptGenerateSymmetricKey — 秘密値から共通鍵(対称鍵)オブジェクトを生成する。
- f BCryptImportKey — キーBLOBからCNGの共通鍵をインポートする。
- f BCryptImportKeyPair — キーBLOBからCNGの公開鍵・秘密鍵ペアをインポートする。