CryptoSys PKI Pro Manual

ECC_ReadPrivateKey

Reads an EC private key from a file into an internal key string.

VBA/VB6 Syntax

Public Declare Function ECC_ReadPrivateKey Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strKeyFileOrString As String, ByVal strPassword As String, ByVal nOptions As Long) As Long

nRet = ECC_ReadPrivateKey(strOutput, Len(strOutput), strKeyFileOrString, strPassword, nOptions)

C/C++ Syntax

long __stdcall ECC_ReadPrivateKey(char *szOutput, long nOutChars, const char *szKeyFileOrString, const char *szPassword, long nOptions);

Parameters

szOutput
[out] string of sufficient length to receive the output.
nOutChars
[in] specifying the maximum number of characters to be received.
szKeyFileOrString
[in] specifying either the name of file containing the key or a string containing the key in PEM format.
szPassword
[in] password for the key file, if encrypted; otherwise set as "".
nOptions
[in] not used in this release. Specify zero.

Returns (VBA/C)

If successful, the return value is the number of characters in or required for the output string; otherwise it returns a nonzero error code.

VBA Wrapper Syntax

Public Function eccReadPrivateKey (szKeyFileOrString As String, Optional szPassword As String = "", Optional nOptions As Long = 0) As String

.NET Equivalent

Ecc.ReadPrivateKey Method

C++ (STL) Equivalent

static std::string dipki::Ecc::ReadPrivateKey (std::string keyFileOrString, std::string password="")

Python Equivalent

static Ecc.read_private_key(keyfileorstr, password="")

Remarks

For the "raw" VBA/C function, the user must allocate an output string buffer szOutput of the required length. Specify a zero nOutChars or an empty string for szOutput to find the required length. ANSI C users must add one to this value when allocating memory.

This reads both encrypted private keys and the unencrypted PKCS#8 PrivateKeyInfo and ECPrivateKey formats. Set szPassword as the empty string "" if not required.

Example (VBA core function)

Dim nRet As Long
Dim nChars As Long
Dim strIntKey As String
Dim strKeyFile As String
Dim strPassword As String
Dim strNewKeyFile As String
Dim strTypeName As String
Dim strFileName As String

strKeyFile = "myeckeyp256.p8"
strPassword = "password"

' Find required length of internal key string
nChars = ECC_ReadPrivateKey("", 0, strKeyFile, strPassword, 0)
Debug.Print "ECC_ReadPrivateKey returns " & nChars & " (expected +ve)"
' Dimension the string to receive output
strIntKey = String(nChars, " ")
' Read it in
nChars = ECC_ReadPrivateKey(strIntKey, Len(strIntKey), strKeyFile, strPassword, 0)
' Caution: internal key string is only valid for the current session
Debug.Print "[" & strIntKey & "]"

' Now save in a different format: ECPrivatekey in PEM encoding
strNewKeyFile = "myeckey.pem"
nRet = ECC_SaveKey(strNewKeyFile, strIntKey, PKI_KEY_FORMAT_PEM)
Debug.Print "ECC_SaveKey returns " & nRet & " (expected 0)"

' Check the types of file we made
strTypeName = String(PKI_ASN1_TYPE_MAXCHARS, " ")
strFileName = strNewKeyFile
nChars = ASN1_Type(strTypeName, Len(strTypeName), strFileName, 0)
If nChars > 0 Then Debug.Print strFileName & ": " & Left(strTypeName, nChars)
ECC_ReadPrivateKey returns 100 (expected +ve)
[PVECwcBFxO1TkZ3D/O6...nNWDXV9xuIUuVZqCO5]
ECC_SaveKey returns 0 (expected 0)
myeckey.pem: EC PRIVATE KEY

The output file should look similar to the following:

-----BEGIN EC PRIVATE KEY-----
MDECAQEEIAlVzJkEMwIF+gBVnbyuXJeMQqL5b8HWYbZvYXMx7wc4oAoGCCqGSM49
AwEH
-----END EC PRIVATE KEY-----

Example (VBA wrapper function)

Dim strIntKey As String

' Read in public key from file
strIntKey = eccReadPublicKey("myeckeyp256.pub")
Debug.Assert Len(strIntKey) > 0
Debug.Print "Key curve=" & eccQueryKey(strIntKey, "curveName", 0) & " keyBits=" & _
    eccQueryKey(strIntKey, "keyBits", 0) & _
    " and is a " & _
    IIf(eccQueryKey(strIntKey, "isPrivate") = "1", "Private", "Public") & " key"
Debug.Print eccQueryKey(strIntKey, "publicKey")
Debug.Print "KeyHashCode=0x" & Hex(ECC_KeyHashCode(strIntKey))

' Read in private key from file
strIntKey = eccReadPrivateKey("myeckeyp256.p8", "password")
Debug.Assert Len(strIntKey) > 0
Debug.Print "Key curve=" & eccQueryKey(strIntKey, "curveName", 0) & " keyBits=" & _
    eccQueryKey(strIntKey, "keyBits", 0) & _
    " and is a " & _
    IIf(eccQueryKey(strIntKey, "isPrivate") = "1", "Private", "Public") & " key"
Debug.Print eccQueryKey(strIntKey, "publicKey")
Debug.Print "KeyHashCode=0x" & Hex(ECC_KeyHashCode(strIntKey))

' Derive public key from private key
strIntKey = eccPublicKeyFromPrivate(strIntKey)
Debug.Assert Len(strIntKey) > 0
Debug.Print "Key curve=" & eccQueryKey(strIntKey, "curveName", 0) & " keyBits=" & _
    eccQueryKey(strIntKey, "keyBits", 0) & _
    " and is a " & _
    IIf(eccQueryKey(strIntKey, "isPrivate") = "1", "Private", "Public") & " key"
Debug.Print eccQueryKey(strIntKey, "publicKey")
Debug.Print "KeyHashCode=0x" & Hex(ECC_KeyHashCode(strIntKey))

See Also

ECC_ReadPublicKey ECC_QueryKey

[Contents] [Index]

[PREV: ECC_ReadKeyByCurve...]   [Contents]   [Index]   
   [NEXT: ECC_ReadPublicKey...]

Copyright © 2004-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-09-23T07:52:09Z.