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

CryptBinaryToStringA

関数
バイナリデータをBase64などのANSI文字列に変換する。
DLLCRYPT32.dll文字セットANSI (-A)呼出規約winapi対応OSWindows XP 以降

シグネチャ

// CRYPT32.dll  (ANSI / -A)
#include <windows.h>

BOOL CryptBinaryToStringA(
    const BYTE* pbBinary,
    DWORD cbBinary,
    CRYPT_STRING dwFlags,
    LPSTR pszString,   // optional
    DWORD* pcchString
);

パラメーター

名前方向説明
pbBinaryBYTE*in文字列に変換するバイト配列へのポインター。
cbBinaryDWORDinpbBinary 配列の要素数。
dwFlagsCRYPT_STRINGin

変換後の書式付き文字列の形式を指定します。このパラメーターには次のいずれかの値を指定できます。

意味
CRYPT_STRING_BASE64HEADER
0x00000000
Base64。証明書の開始ヘッダーと終了ヘッダーを付加します。
CRYPT_STRING_BASE64
0x00000001
Base64。ヘッダーは付加しません。
CRYPT_STRING_BINARY
0x00000002
純粋なバイナリのコピー。
CRYPT_STRING_BASE64REQUESTHEADER
0x00000003
Base64。要求の開始ヘッダーと終了ヘッダーを付加します。
CRYPT_STRING_HEX
0x00000004
16 進数のみ。
CRYPT_STRING_HEXASCII
0x00000005
16 進数。ASCII 文字表示付き。
CRYPT_STRING_BASE64X509CRLHEADER
0x00000009
Base64。X.509 CRL の開始ヘッダーと終了ヘッダーを付加します。
CRYPT_STRING_HEXADDR
0x0000000a
16 進数。アドレス表示付き。
CRYPT_STRING_HEXASCIIADDR
0x0000000b
16 進数。ASCII 文字表示およびアドレス表示付き。
CRYPT_STRING_HEXRAW
0x0000000c
生の 16 進数文字列。

Windows Server 2003 および Windows XP: この値はサポートされていません。

CRYPT_STRING_BASE64URI
0x0000000d
Base64。ヘッダーなしで、RFC 4648 のセクション 5 に定義されているとおり "+" を "-" に、"/" を "_" に置き換えます。
CRYPT_STRING_STRICT
0x20000000
ASN.1 テキスト形式の厳密なデコードを強制します。一部の ASN.1 バイナリ BLOB では、BLOB の先頭数バイトが Base64 テキストとして誤って解釈されることがあります。この場合、残りのテキストは無視されます。このフラグを使用すると、BLOB の完全なデコードが強制されます。

Windows Server 2008、Windows Vista、Windows Server 2003 および Windows XP: この値はサポートされていません。

上記の値に加えて、次の値を 1 つ以上指定して関数の動作を変更できます。

意味
CRYPT_STRING_NOCRLF
0x40000000
エンコードされた文字列に改行文字を付加しません。既定の動作では、改行を表すためにキャリッジリターン/ラインフィード (CR/LF) のペア (0x0D/0x0A) が使用されます。

Windows Server 2003 および Windows XP: この値はサポートされていません。

CRYPT_STRING_NOCR
0x80000000
改行にラインフィード (LF) 文字 (0x0A) のみを使用します。既定の動作では、改行を表すために CR/LF のペア (0x0D/0x0A) が使用されます。
pszStringLPSTRoutoptional変換後の文字列を受け取るバッファーへのポインター。返される文字列を格納するために割り当てる必要がある文字数を計算するには、このパラメーターを NULL に設定します。関数は、終端の NULL 文字を含む必要な文字数を、pcchString が指す値に格納します。
pcchStringDWORD*inoutpszString バッファーのサイズを TCHAR 単位で格納する DWORD 変数へのポインター。pszStringNULL の場合、関数は返される文字列の長さ (終端の null 文字を含む) を TCHAR 単位で計算し、このパラメーターに返します。pszStringNULL でなく、かつ十分な大きさがある場合、関数はバイナリデータを終端の null 文字を含む指定した文字列形式に変換しますが、pcchString には終端の null 文字を含まない TCHAR 単位の長さが返されます。

戻り値の型: BOOL

公式ドキュメント

バイト配列を書式付き文字列に変換します。(ANSI)

戻り値

関数が成功した場合、関数は 0 以外の値 (TRUE) を返します。

関数が失敗した場合は、0 (FALSE) を返します。

解説(Remarks)

CRYPT_STRING_BINARY エンコードが使用される場合を除き、すべての文字列には改行シーケンスが付加されます。既定では、改行シーケンスは CR/LF のペア (0x0D/0x0A) です。dwFlags パラメーターに CRYPT_STRING_NOCR フラグが含まれている場合、改行シーケンスは LF 文字 (0x0A) になります。dwFlags パラメーターに CRYPT_STRING_NOCRLF フラグが含まれている場合、文字列に改行シーケンスは付加されません。

メモ

wincrypt.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして CryptBinaryToString を定義しています。エンコードに依存しないこのエイリアスを、エンコードに依存しないわけではないコードと混在させて使用すると、不一致が生じ、コンパイルエラーや実行時エラーの原因となることがあります。詳細については、「関数プロトタイプの規則」を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// CRYPT32.dll  (ANSI / -A)
#include <windows.h>

BOOL CryptBinaryToStringA(
    const BYTE* pbBinary,
    DWORD cbBinary,
    CRYPT_STRING dwFlags,
    LPSTR pszString,   // optional
    DWORD* pcchString
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern bool CryptBinaryToStringA(
    IntPtr pbBinary,   // BYTE*
    uint cbBinary,   // DWORD
    uint dwFlags,   // CRYPT_STRING
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszString,   // LPSTR optional, out
    ref uint pcchString   // DWORD* in/out
);
<DllImport("CRYPT32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function CryptBinaryToStringA(
    pbBinary As IntPtr,   ' BYTE*
    cbBinary As UInteger,   ' DWORD
    dwFlags As UInteger,   ' CRYPT_STRING
    <MarshalAs(UnmanagedType.LPStr)> pszString As System.Text.StringBuilder,   ' LPSTR optional, out
    ByRef pcchString As UInteger   ' DWORD* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' pbBinary : BYTE*
' cbBinary : DWORD
' dwFlags : CRYPT_STRING
' pszString : LPSTR optional, out
' pcchString : DWORD* in/out
Declare PtrSafe Function CryptBinaryToStringA Lib "crypt32" ( _
    ByVal pbBinary As LongPtr, _
    ByVal cbBinary As Long, _
    ByVal dwFlags As Long, _
    ByVal pszString As String, _
    ByRef pcchString As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptBinaryToStringA = ctypes.windll.crypt32.CryptBinaryToStringA
CryptBinaryToStringA.restype = wintypes.BOOL
CryptBinaryToStringA.argtypes = [
    ctypes.POINTER(ctypes.c_ubyte),  # pbBinary : BYTE*
    wintypes.DWORD,  # cbBinary : DWORD
    wintypes.DWORD,  # dwFlags : CRYPT_STRING
    wintypes.LPSTR,  # pszString : LPSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchString : DWORD* in/out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('CRYPT32.dll')
CryptBinaryToStringA = Fiddle::Function.new(
  lib['CryptBinaryToStringA'],
  [
    Fiddle::TYPE_VOIDP,  # pbBinary : BYTE*
    -Fiddle::TYPE_INT,  # cbBinary : DWORD
    -Fiddle::TYPE_INT,  # dwFlags : CRYPT_STRING
    Fiddle::TYPE_VOIDP,  # pszString : LPSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcchString : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "crypt32")]
extern "system" {
    fn CryptBinaryToStringA(
        pbBinary: *const u8,  // BYTE*
        cbBinary: u32,  // DWORD
        dwFlags: u32,  // CRYPT_STRING
        pszString: *mut u8,  // LPSTR optional, out
        pcchString: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", CharSet = CharSet.Ansi)]
public static extern bool CryptBinaryToStringA(IntPtr pbBinary, uint cbBinary, uint dwFlags, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszString, ref uint pcchString);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptBinaryToStringA' -Namespace Win32 -PassThru
# $api::CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString)
#uselib "CRYPT32.dll"
#func global CryptBinaryToStringA "CryptBinaryToStringA" sptr, sptr, sptr, sptr, sptr
; CryptBinaryToStringA varptr(pbBinary), cbBinary, dwFlags, varptr(pszString), varptr(pcchString)   ; 戻り値は stat
; pbBinary : BYTE* -> "sptr"
; cbBinary : DWORD -> "sptr"
; dwFlags : CRYPT_STRING -> "sptr"
; pszString : LPSTR optional, out -> "sptr"
; pcchString : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "CRYPT32.dll"
#cfunc global CryptBinaryToStringA "CryptBinaryToStringA" var, int, int, var, var
; res = CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString)
; pbBinary : BYTE* -> "var"
; cbBinary : DWORD -> "int"
; dwFlags : CRYPT_STRING -> "int"
; pszString : LPSTR optional, out -> "var"
; pcchString : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptBinaryToStringA(BYTE* pbBinary, DWORD cbBinary, CRYPT_STRING dwFlags, LPSTR pszString, DWORD* pcchString)
#uselib "CRYPT32.dll"
#cfunc global CryptBinaryToStringA "CryptBinaryToStringA" var, int, int, var, var
; res = CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString)
; pbBinary : BYTE* -> "var"
; cbBinary : DWORD -> "int"
; dwFlags : CRYPT_STRING -> "int"
; pszString : LPSTR optional, out -> "var"
; pcchString : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procCryptBinaryToStringA = crypt32.NewProc("CryptBinaryToStringA")
)

// pbBinary (BYTE*), cbBinary (DWORD), dwFlags (CRYPT_STRING), pszString (LPSTR optional, out), pcchString (DWORD* in/out)
r1, _, err := procCryptBinaryToStringA.Call(
	uintptr(pbBinary),
	uintptr(cbBinary),
	uintptr(dwFlags),
	uintptr(pszString),
	uintptr(pcchString),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptBinaryToStringA(
  pbBinary: Pointer;   // BYTE*
  cbBinary: DWORD;   // DWORD
  dwFlags: DWORD;   // CRYPT_STRING
  pszString: PAnsiChar;   // LPSTR optional, out
  pcchString: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'CRYPT32.dll' name 'CryptBinaryToStringA';
result := DllCall("CRYPT32\CryptBinaryToStringA"
    , "Ptr", pbBinary   ; BYTE*
    , "UInt", cbBinary   ; DWORD
    , "UInt", dwFlags   ; CRYPT_STRING
    , "Ptr", pszString   ; LPSTR optional, out
    , "Ptr", pcchString   ; DWORD* in/out
    , "Int")   ; return: BOOL
●CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString) = DLL("CRYPT32.dll", "bool CryptBinaryToStringA(void*, dword, dword, char*, void*)")
# 呼び出し: CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString)
# pbBinary : BYTE* -> "void*"
# cbBinary : DWORD -> "dword"
# dwFlags : CRYPT_STRING -> "dword"
# pszString : LPSTR optional, out -> "char*"
# pcchString : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "crypt32" fn CryptBinaryToStringA(
    pbBinary: [*c]u8, // BYTE*
    cbBinary: u32, // DWORD
    dwFlags: u32, // CRYPT_STRING
    pszString: [*c]u8, // LPSTR optional, out
    pcchString: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
proc CryptBinaryToStringA(
    pbBinary: ptr uint8,  # BYTE*
    cbBinary: uint32,  # DWORD
    dwFlags: uint32,  # CRYPT_STRING
    pszString: ptr char,  # LPSTR optional, out
    pcchString: ptr uint32  # DWORD* in/out
): int32 {.importc: "CryptBinaryToStringA", stdcall, dynlib: "CRYPT32.dll".}
pragma(lib, "crypt32");
extern(Windows)
int CryptBinaryToStringA(
    ubyte* pbBinary,   // BYTE*
    uint cbBinary,   // DWORD
    uint dwFlags,   // CRYPT_STRING
    char* pszString,   // LPSTR optional, out
    uint* pcchString   // DWORD* in/out
);
ccall((:CryptBinaryToStringA, "CRYPT32.dll"), stdcall, Int32,
      (Ptr{UInt8}, UInt32, UInt32, Ptr{UInt8}, Ptr{UInt32}),
      pbBinary, cbBinary, dwFlags, pszString, pcchString)
# pbBinary : BYTE* -> Ptr{UInt8}
# cbBinary : DWORD -> UInt32
# dwFlags : CRYPT_STRING -> UInt32
# pszString : LPSTR optional, out -> Ptr{UInt8}
# pcchString : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t CryptBinaryToStringA(
    uint8_t* pbBinary,
    uint32_t cbBinary,
    uint32_t dwFlags,
    char* pszString,
    uint32_t* pcchString);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString)
-- pbBinary : BYTE*
-- cbBinary : DWORD
-- dwFlags : CRYPT_STRING
-- pszString : LPSTR optional, out
-- pcchString : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CryptBinaryToStringA = lib.func('__stdcall', 'CryptBinaryToStringA', 'int32_t', ['uint8_t *', 'uint32_t', 'uint32_t', 'char *', 'uint32_t *']);
// CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString)
// pbBinary : BYTE* -> 'uint8_t *'
// cbBinary : DWORD -> 'uint32_t'
// dwFlags : CRYPT_STRING -> 'uint32_t'
// pszString : LPSTR optional, out -> 'char *'
// pcchString : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("CRYPT32.dll", {
  CryptBinaryToStringA: { parameters: ["pointer", "u32", "u32", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString)
// pbBinary : BYTE* -> "pointer"
// cbBinary : DWORD -> "u32"
// dwFlags : CRYPT_STRING -> "u32"
// pszString : LPSTR optional, out -> "buffer"
// pcchString : DWORD* in/out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CryptBinaryToStringA(
    uint8_t* pbBinary,
    uint32_t cbBinary,
    uint32_t dwFlags,
    char* pszString,
    uint32_t* pcchString);
C, "CRYPT32.dll");
// $ffi->CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, pszString, pcchString);
// pbBinary : BYTE*
// cbBinary : DWORD
// dwFlags : CRYPT_STRING
// pszString : LPSTR optional, out
// pcchString : DWORD* 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 Crypt32 extends StdCallLibrary {
    Crypt32 INSTANCE = Native.load("crypt32", Crypt32.class, W32APIOptions.ASCII_OPTIONS);
    boolean CryptBinaryToStringA(
        byte[] pbBinary,   // BYTE*
        int cbBinary,   // DWORD
        int dwFlags,   // CRYPT_STRING
        byte[] pszString,   // LPSTR optional, out
        IntByReference pcchString   // DWORD* in/out
    );
}
@[Link("crypt32")]
lib LibCRYPT32
  fun CryptBinaryToStringA = CryptBinaryToStringA(
    pbBinary : UInt8*,   # BYTE*
    cbBinary : UInt32,   # DWORD
    dwFlags : UInt32,   # CRYPT_STRING
    pszString : UInt8*,   # LPSTR optional, out
    pcchString : UInt32*   # DWORD* 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 CryptBinaryToStringANative = Int32 Function(Pointer<Uint8>, Uint32, Uint32, Pointer<Utf8>, Pointer<Uint32>);
typedef CryptBinaryToStringADart = int Function(Pointer<Uint8>, int, int, Pointer<Utf8>, Pointer<Uint32>);
final CryptBinaryToStringA = DynamicLibrary.open('CRYPT32.dll')
    .lookupFunction<CryptBinaryToStringANative, CryptBinaryToStringADart>('CryptBinaryToStringA');
// pbBinary : BYTE* -> Pointer<Uint8>
// cbBinary : DWORD -> Uint32
// dwFlags : CRYPT_STRING -> Uint32
// pszString : LPSTR optional, out -> Pointer<Utf8>
// pcchString : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CryptBinaryToStringA(
  pbBinary: Pointer;   // BYTE*
  cbBinary: DWORD;   // DWORD
  dwFlags: DWORD;   // CRYPT_STRING
  pszString: PAnsiChar;   // LPSTR optional, out
  pcchString: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'CRYPT32.dll' name 'CryptBinaryToStringA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CryptBinaryToStringA"
  c_CryptBinaryToStringA :: Ptr Word8 -> Word32 -> Word32 -> CString -> Ptr Word32 -> IO CInt
-- pbBinary : BYTE* -> Ptr Word8
-- cbBinary : DWORD -> Word32
-- dwFlags : CRYPT_STRING -> Word32
-- pszString : LPSTR optional, out -> CString
-- pcchString : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let cryptbinarytostringa =
  foreign "CryptBinaryToStringA"
    ((ptr uint8_t) @-> uint32_t @-> uint32_t @-> string @-> (ptr uint32_t) @-> returning int32_t)
(* pbBinary : BYTE* -> (ptr uint8_t) *)
(* cbBinary : DWORD -> uint32_t *)
(* dwFlags : CRYPT_STRING -> uint32_t *)
(* pszString : LPSTR optional, out -> string *)
(* pcchString : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library crypt32 (t "CRYPT32.dll"))
(cffi:use-foreign-library crypt32)

(cffi:defcfun ("CryptBinaryToStringA" crypt-binary-to-string-a :convention :stdcall) :int32
  (pb-binary :pointer)   ; BYTE*
  (cb-binary :uint32)   ; DWORD
  (dw-flags :uint32)   ; CRYPT_STRING
  (psz-string :pointer)   ; LPSTR optional, out
  (pcch-string :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CryptBinaryToStringA = Win32::API::More->new('CRYPT32',
    'BOOL CryptBinaryToStringA(LPVOID pbBinary, DWORD cbBinary, DWORD dwFlags, LPSTR pszString, LPVOID pcchString)');
# my $ret = $CryptBinaryToStringA->Call($pbBinary, $cbBinary, $dwFlags, $pszString, $pcchString);
# pbBinary : BYTE* -> LPVOID
# cbBinary : DWORD -> DWORD
# dwFlags : CRYPT_STRING -> DWORD
# pszString : LPSTR optional, out -> LPSTR
# pcchString : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
公式の関連項目
使用する型