CryptoSys PKI Pro Manual


Saves an internal EC key string to an unencrypted key file.

VBA/VB6 Syntax

Public Declare Function ECC_SaveKey Lib "diCrPKI.dll" (ByVal strFileOut As String, ByVal strIntKeyString As String, ByVal nOptions As Long) As Long

nRet = ECC_SaveKey(strFileOut, strIntKeyString, nOptions)

C/C++ Syntax

long __stdcall ECC_SaveKey(const char *szFileOut, const char *szIntKeyString, long nOptions);


[in] name of key file to be created.
[in] the private or public EC key in an internal key string.
[in] choose one of and optionally add any of
PKI_KEY_FORMAT_PEM to save the key file in PEM form (default is binary DER-encoded format).
PKI_KEY_LEGACY to save a safe key in "legacy" PKCS#8 v1 format (default is v2 OneAsymmetricKey).

Returns (VBA/C)

If successful, the return value is zero; otherwise it returns a nonzero error code.

VBA Wrapper Syntax

Public Function eccSaveKey (szOutputFile As String, szKeyStr As String, Optional nOptions As Long = 0) As Long

.NET Equivalent

Ecc.SaveKey Method

C++ (STL) Equivalent

static int dipki::Ecc::SaveKey (std::string outputFile, std::string internalKey, KeyType keyType=KeyType::Default, Format fileFormat=Format::Binary)

Python Equivalent

static Ecc.save_key(outputfile, intkeystr, keytype=0, fileformat=0)


EC public keys are always saved in SubjectPublicKeyInfo format [RFC5480]. By default, NIST/SEC curve private keys are saved in ECPrivateKey format [RFC5915]. Use the PKI_KEY_TYPE_PKCS8 option to save in PKCS#8 PrivateKeyInfo format [RFC5208].

[New in v22.0] Safe curve private keys (X25519, Ed25519, X448 and Ed448) are always saved in PKCS#8 v2 OneAsymmetricKey format including the public key [RFC5958]. Add the option PKI_KEY_LEGACY to save in older PKCS#8 v1 PrivateKeyInfo format [RFC5208] excluding the public key.

To save a private key in encrypted form, use ECC_SaveEncKey.


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"

Debug.Print "FILE: " & strKeyFile
' 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 type 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)
FILE: myeckeyp256.p8
ECC_ReadPrivateKey returns 100 (expected +ve)
ECC_SaveKey returns 0 (expected 0)
myeckey.pem: EC PRIVATE KEY

See Also

ECC_ReadPrivateKey ECC_ReadPublicKey ECC_SaveEncKey ECC_PublicKeyFromPrivate

[Contents] [Index]

[PREV: ECC_SaveEncKey...]   [Contents]   [Index]   
   [NEXT: HASH_Bytes...]

Copyright © 2004-23 D.I. Management Services Pty Ltd. All rights reserved. Generated 2023-10-22T11:11:11Z.