Converts an internal EC private key string into an internal EC public key string.
Public Declare Function ECC_PublicKeyFromPrivate Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strIntKeyString As String, ByVal nOptions As Long) As Long
nRet = ECC_PublicKeyFromPrivate(strOutput, nOutChars, strIntKeyString, nOptions)
long __stdcall ECC_PublicKeyFromPrivate(char *szOutput, long nOutChars, const char *szIntKeyString, 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 eccPublicKeyFromPrivate
(szIntKeyString As String, Optional nOptions As Long = 0) As String
Ecc.PublicKeyFromPrivate Method
static std::string dipki::Ecc::PublicKeyFromPrivate (std::string internalKey)
static Ecc.publickey_from_private(intkeystr)
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.
Use this to derive the public key from the EC private key, where both values are represented as "internal" key strings.
Dim nRet As Long Dim nChars As Long Dim strIntPriKey As String Dim strIntPubKey As String Dim strKeyFile As String Dim strNewKeyFile As String Dim strPassword As String Dim strTypeName As String Dim strFileName As String strKeyFile = "myeckeyp521.p8" strPassword = "password" ' 1. READ IN THE PRIVATE KEY: int_string <- file 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)" If (nChars <= 0) Then Exit Sub ' CATCH ERROR HERE ' Dimension the string to receive output strIntPriKey = String(nChars, " ") ' Read into internal key string nChars = ECC_ReadPrivateKey(strIntPriKey, Len(strIntPriKey), strKeyFile, strPassword, 0) Debug.Print "[" & strIntPriKey & "]" ' 2. CONVERT TO PUBLIC: int_string <-- int_string nChars = ECC_PublicKeyFromPrivate("", 0, strIntPriKey, 0) Debug.Print "ECC_PublicKeyFromPrivate returns " & nChars & " (expected +ve)" If (nChars <= 0) Then Exit Sub ' CATCH ERROR HERE strIntPubKey = String(nChars, " ") nChars = ECC_PublicKeyFromPrivate(strIntPubKey, Len(strIntPubKey), strIntPriKey, 0) Debug.Print "[" & strIntPubKey & "]" ' 3. SAVE AS NEW PUBLIC KEY FILE: file <-- int_string strNewKeyFile = "myeckeyp521_new.pub" nRet = ECC_SaveKey(strNewKeyFile, strIntPubKey, 0) 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: myeckeyp521.p8 ECC_ReadPrivateKey returns 336 (expected +ve) [PVECRDe...PmEAIGm5Uj1HI=] ECC_PublicKeyFromPrivate returns 212 (expected +ve) [MIGbMBA...sxHXznms=] ECC_SaveKey returns 0 (expected 0) myeckeyp521_new.pub: PUBLIC KEY INFO
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_ReadPrivateKey ECC_ReadPublicKey