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