ホーム › Security.Cryptography › CryptDuplicateKey
CryptDuplicateKey
関数暗号鍵ハンドルを複製して状態をコピーする。
シグネチャ
// ADVAPI32.dll
#include <windows.h>
BOOL CryptDuplicateKey(
UINT_PTR hKey,
DWORD* pdwReserved, // optional
DWORD dwFlags,
UINT_PTR* phKey
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hKey | UINT_PTR | in | 複製するキーのハンドルです。 |
| pdwReserved | DWORD* | optional | 将来の使用のために予約されており、NULL でなければなりません。 |
| dwFlags | DWORD | in | 将来の使用のために予約されており、ゼロでなければなりません。 |
| phKey | UINT_PTR* | out | 複製されたキーのハンドルのアドレスです。キーの使用が終わったら、CryptDestroyKey 関数を呼び出してハンドルを解放してください。 |
戻り値の型: BOOL
公式ドキュメント
キーおよびキーの状態の正確なコピーを作成します。
戻り値
関数が成功した場合、戻り値は 0 以外 (TRUE) になります。
関数が失敗した場合、戻り値は 0 (FALSE) になります。拡張エラー情報を取得するには、 GetLastError を呼び出してください。
"NTE" で始まるエラーコードは、使用している特定の CSP によって生成されます。考えられるエラーコードの一部を次の表に示します。
| 戻り値 | 説明 |
|---|---|
| これは新しい関数であるため、既存の CSP では実装されていない場合があります。CSP がこの関数をサポートしていない場合に、このエラーが返されます。 | |
| いずれかのパラメーターに無効な値が含まれています。多くの場合、これは無効なポインターです。 | |
| 元のキーのハンドルが無効です。 |
解説(Remarks)
CryptDuplicateKey は、キーのコピーと、そのキーの正確な状態を作成します。この関数が使用されるシナリオの 1 つとして、アプリケーションが同じキーを使用しつつ異なるソルト値で 2 つの個別のメッセージを暗号化する必要がある場合があります。まず元のキーを生成し、次に CryptDuplicateKey 関数を使用して複製キーを作成します。その後、 CryptSetKeyParam 関数を個別に呼び出して、元のキーと複製キーに異なるソルト値を設定します。
CryptDuplicateKey を使用して作成されたキーを破棄するには、CryptDestroyKey を呼び出す必要があります。元のキーを破棄しても、複製キーは破棄されません。複製キーが作成された後は、それは元のキーとは独立しています。2 つのキー間で状態が共有されることはありません。
例
次の例は、既存のセッションキーの複製であるセッションキーの作成を示しています。この例の完全なコンテキストを含む例については、C プログラムの例: セッションキーの複製を参照してください。
//--------------------------------------------------------------------
// Declare and initialize variables.
HCRYPTKEY hDuplicateKey;
// Duplicate the key. hOriginalKey is a previously
// assigned HCRYPTKEY variable.
if (CryptDuplicateKey(
hOriginalKey,
NULL,
0,
&hDuplicateKey))
{
printf("The session key has been duplicated. \n");
}
else
{
printf("Error using CryptDuplicateKey.\n");
exit(1);
}
// Insert code that uses the duplicate key here.
// When you have finished using the key, the handle must be released.
if (CryptDestroyKey(hDuplicateKey))
{
printf("The handle has been released.\n");
}
else
{
printf("The handle could not be released.\n");
}
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll
#include <windows.h>
BOOL CryptDuplicateKey(
UINT_PTR hKey,
DWORD* pdwReserved, // optional
DWORD dwFlags,
UINT_PTR* phKey
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptDuplicateKey(
UIntPtr hKey, // UINT_PTR
IntPtr pdwReserved, // DWORD* optional
uint dwFlags, // DWORD
out UIntPtr phKey // UINT_PTR* out
);<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptDuplicateKey(
hKey As UIntPtr, ' UINT_PTR
pdwReserved As IntPtr, ' DWORD* optional
dwFlags As UInteger, ' DWORD
<Out> ByRef phKey As UIntPtr ' UINT_PTR* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hKey : UINT_PTR
' pdwReserved : DWORD* optional
' dwFlags : DWORD
' phKey : UINT_PTR* out
Declare PtrSafe Function CryptDuplicateKey Lib "advapi32" ( _
ByVal hKey As LongPtr, _
ByVal pdwReserved 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
CryptDuplicateKey = ctypes.windll.advapi32.CryptDuplicateKey
CryptDuplicateKey.restype = wintypes.BOOL
CryptDuplicateKey.argtypes = [
ctypes.c_size_t, # hKey : UINT_PTR
ctypes.POINTER(wintypes.DWORD), # pdwReserved : DWORD* optional
wintypes.DWORD, # dwFlags : DWORD
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')
CryptDuplicateKey = Fiddle::Function.new(
lib['CryptDuplicateKey'],
[
Fiddle::TYPE_UINTPTR_T, # hKey : UINT_PTR
Fiddle::TYPE_VOIDP, # pdwReserved : DWORD* optional
-Fiddle::TYPE_INT, # dwFlags : DWORD
Fiddle::TYPE_VOIDP, # phKey : UINT_PTR* out
],
Fiddle::TYPE_INT)#[link(name = "advapi32")]
extern "system" {
fn CryptDuplicateKey(
hKey: usize, // UINT_PTR
pdwReserved: *mut u32, // DWORD* optional
dwFlags: u32, // DWORD
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 CryptDuplicateKey(UIntPtr hKey, IntPtr pdwReserved, uint dwFlags, out UIntPtr phKey);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CryptDuplicateKey' -Namespace Win32 -PassThru
# $api::CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey)#uselib "ADVAPI32.dll"
#func global CryptDuplicateKey "CryptDuplicateKey" sptr, sptr, sptr, sptr
; CryptDuplicateKey hKey, varptr(pdwReserved), dwFlags, varptr(phKey) ; 戻り値は stat
; hKey : UINT_PTR -> "sptr"
; pdwReserved : DWORD* optional -> "sptr"
; dwFlags : DWORD -> "sptr"
; phKey : UINT_PTR* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "ADVAPI32.dll" #cfunc global CryptDuplicateKey "CryptDuplicateKey" sptr, var, int, var ; res = CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey) ; hKey : UINT_PTR -> "sptr" ; pdwReserved : DWORD* optional -> "var" ; dwFlags : DWORD -> "int" ; phKey : UINT_PTR* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ADVAPI32.dll" #cfunc global CryptDuplicateKey "CryptDuplicateKey" sptr, sptr, int, sptr ; res = CryptDuplicateKey(hKey, varptr(pdwReserved), dwFlags, varptr(phKey)) ; hKey : UINT_PTR -> "sptr" ; pdwReserved : DWORD* optional -> "sptr" ; dwFlags : DWORD -> "int" ; phKey : UINT_PTR* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL CryptDuplicateKey(UINT_PTR hKey, DWORD* pdwReserved, DWORD dwFlags, UINT_PTR* phKey) #uselib "ADVAPI32.dll" #cfunc global CryptDuplicateKey "CryptDuplicateKey" intptr, var, int, var ; res = CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey) ; hKey : UINT_PTR -> "intptr" ; pdwReserved : DWORD* optional -> "var" ; dwFlags : DWORD -> "int" ; phKey : UINT_PTR* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL CryptDuplicateKey(UINT_PTR hKey, DWORD* pdwReserved, DWORD dwFlags, UINT_PTR* phKey) #uselib "ADVAPI32.dll" #cfunc global CryptDuplicateKey "CryptDuplicateKey" intptr, intptr, int, intptr ; res = CryptDuplicateKey(hKey, varptr(pdwReserved), dwFlags, varptr(phKey)) ; hKey : UINT_PTR -> "intptr" ; pdwReserved : DWORD* optional -> "intptr" ; dwFlags : DWORD -> "int" ; phKey : UINT_PTR* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procCryptDuplicateKey = advapi32.NewProc("CryptDuplicateKey")
)
// hKey (UINT_PTR), pdwReserved (DWORD* optional), dwFlags (DWORD), phKey (UINT_PTR* out)
r1, _, err := procCryptDuplicateKey.Call(
uintptr(hKey),
uintptr(pdwReserved),
uintptr(dwFlags),
uintptr(phKey),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction CryptDuplicateKey(
hKey: NativeUInt; // UINT_PTR
pdwReserved: Pointer; // DWORD* optional
dwFlags: DWORD; // DWORD
phKey: Pointer // UINT_PTR* out
): BOOL; stdcall;
external 'ADVAPI32.dll' name 'CryptDuplicateKey';result := DllCall("ADVAPI32\CryptDuplicateKey"
, "UPtr", hKey ; UINT_PTR
, "Ptr", pdwReserved ; DWORD* optional
, "UInt", dwFlags ; DWORD
, "Ptr", phKey ; UINT_PTR* out
, "Int") ; return: BOOL●CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey) = DLL("ADVAPI32.dll", "bool CryptDuplicateKey(int, void*, dword, void*)")
# 呼び出し: CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey)
# hKey : UINT_PTR -> "int"
# pdwReserved : DWORD* optional -> "void*"
# dwFlags : DWORD -> "dword"
# phKey : UINT_PTR* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "advapi32" fn CryptDuplicateKey(
hKey: usize, // UINT_PTR
pdwReserved: [*c]u32, // DWORD* optional
dwFlags: u32, // DWORD
phKey: [*c]usize // UINT_PTR* out
) callconv(std.os.windows.WINAPI) i32;proc CryptDuplicateKey(
hKey: uint, # UINT_PTR
pdwReserved: ptr uint32, # DWORD* optional
dwFlags: uint32, # DWORD
phKey: ptr uint # UINT_PTR* out
): int32 {.importc: "CryptDuplicateKey", stdcall, dynlib: "ADVAPI32.dll".}pragma(lib, "advapi32");
extern(Windows)
int CryptDuplicateKey(
size_t hKey, // UINT_PTR
uint* pdwReserved, // DWORD* optional
uint dwFlags, // DWORD
size_t* phKey // UINT_PTR* out
);ccall((:CryptDuplicateKey, "ADVAPI32.dll"), stdcall, Int32,
(Csize_t, Ptr{UInt32}, UInt32, Ptr{Csize_t}),
hKey, pdwReserved, dwFlags, phKey)
# hKey : UINT_PTR -> Csize_t
# pdwReserved : DWORD* optional -> Ptr{UInt32}
# dwFlags : DWORD -> UInt32
# phKey : UINT_PTR* out -> Ptr{Csize_t}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t CryptDuplicateKey(
uintptr_t hKey,
uint32_t* pdwReserved,
uint32_t dwFlags,
uintptr_t* phKey);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey)
-- hKey : UINT_PTR
-- pdwReserved : DWORD* optional
-- dwFlags : DWORD
-- phKey : UINT_PTR* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const CryptDuplicateKey = lib.func('__stdcall', 'CryptDuplicateKey', 'int32_t', ['uintptr_t', 'uint32_t *', 'uint32_t', 'uintptr_t *']);
// CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey)
// hKey : UINT_PTR -> 'uintptr_t'
// pdwReserved : DWORD* optional -> 'uint32_t *'
// dwFlags : DWORD -> 'uint32_t'
// phKey : UINT_PTR* out -> 'uintptr_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
CryptDuplicateKey: { parameters: ["usize", "pointer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey)
// hKey : UINT_PTR -> "usize"
// pdwReserved : DWORD* optional -> "pointer"
// dwFlags : DWORD -> "u32"
// phKey : UINT_PTR* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t CryptDuplicateKey(
size_t hKey,
uint32_t* pdwReserved,
uint32_t dwFlags,
size_t* phKey);
C, "ADVAPI32.dll");
// $ffi->CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey);
// hKey : UINT_PTR
// pdwReserved : DWORD* optional
// dwFlags : DWORD
// phKey : UINT_PTR* 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 Advapi32 extends StdCallLibrary {
Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class);
boolean CryptDuplicateKey(
long hKey, // UINT_PTR
IntByReference pdwReserved, // DWORD* optional
int dwFlags, // DWORD
LongByReference phKey // UINT_PTR* out
);
}@[Link("advapi32")]
lib LibADVAPI32
fun CryptDuplicateKey = CryptDuplicateKey(
hKey : LibC::SizeT, # UINT_PTR
pdwReserved : UInt32*, # DWORD* optional
dwFlags : UInt32, # DWORD
phKey : LibC::SizeT* # UINT_PTR* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CryptDuplicateKeyNative = Int32 Function(UintPtr, Pointer<Uint32>, Uint32, Pointer<UintPtr>);
typedef CryptDuplicateKeyDart = int Function(int, Pointer<Uint32>, int, Pointer<UintPtr>);
final CryptDuplicateKey = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<CryptDuplicateKeyNative, CryptDuplicateKeyDart>('CryptDuplicateKey');
// hKey : UINT_PTR -> UintPtr
// pdwReserved : DWORD* optional -> Pointer<Uint32>
// dwFlags : DWORD -> Uint32
// phKey : UINT_PTR* out -> Pointer<UintPtr>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CryptDuplicateKey(
hKey: NativeUInt; // UINT_PTR
pdwReserved: Pointer; // DWORD* optional
dwFlags: DWORD; // DWORD
phKey: Pointer // UINT_PTR* out
): BOOL; stdcall;
external 'ADVAPI32.dll' name 'CryptDuplicateKey';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CryptDuplicateKey"
c_CryptDuplicateKey :: CUIntPtr -> Ptr Word32 -> Word32 -> Ptr CUIntPtr -> IO CInt
-- hKey : UINT_PTR -> CUIntPtr
-- pdwReserved : DWORD* optional -> Ptr Word32
-- dwFlags : DWORD -> Word32
-- phKey : UINT_PTR* out -> Ptr CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let cryptduplicatekey =
foreign "CryptDuplicateKey"
(size_t @-> (ptr uint32_t) @-> uint32_t @-> (ptr size_t) @-> returning int32_t)
(* hKey : UINT_PTR -> size_t *)
(* pdwReserved : DWORD* optional -> (ptr uint32_t) *)
(* dwFlags : DWORD -> uint32_t *)
(* phKey : UINT_PTR* out -> (ptr size_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)
(cffi:defcfun ("CryptDuplicateKey" crypt-duplicate-key :convention :stdcall) :int32
(h-key :uint64) ; UINT_PTR
(pdw-reserved :pointer) ; DWORD* optional
(dw-flags :uint32) ; DWORD
(ph-key :pointer)) ; UINT_PTR* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CryptDuplicateKey = Win32::API::More->new('ADVAPI32',
'BOOL CryptDuplicateKey(WPARAM hKey, LPVOID pdwReserved, DWORD dwFlags, LPVOID phKey)');
# my $ret = $CryptDuplicateKey->Call($hKey, $pdwReserved, $dwFlags, $phKey);
# hKey : UINT_PTR -> WPARAM
# pdwReserved : DWORD* optional -> LPVOID
# dwFlags : DWORD -> DWORD
# phKey : UINT_PTR* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
公式の関連項目
- f CryptDestroyKey — 暗号鍵ハンドルを破棄する。
- f CryptSetKeyParam — 暗号鍵オブジェクトのパラメータを設定する。