Reads an EC private key from a file into an internal key string.
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)
long __stdcall ECC_ReadPrivateKey(char *szOutput, long nOutChars, const char *szKeyFileOrString, const char *szPassword, long nOptions);
""
.If successful, the return value is the number of characters in or required for the output string; otherwise it returns a nonzero error code.
Public Function eccReadPrivateKey
(szKeyFileOrString As String, Optional szPassword As String = "", Optional nOptions As Long = 0) As String
static std::string dipki::Ecc::ReadPrivateKey (std::string keyFileOrString, std::string password="")
static Ecc.read_private_key(keyfileorstr, password="")
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.
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-----
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))
ECC_ReadPublicKey ECC_QueryKey