Write an internal EC key to an output string in PEM format.
Public Declare Function ECC_WriteKey Lib "diCrPKI.dll" (ByVal szOutput As String, ByVal nOutChars As Long, ByVal szIntKeyString As String, ByVal nOptions As Long) As Long
nRet = ECC_WriteKey(strOutput, nOutChars, strIntKeyString, nOptions)
long __stdcall ECC_WriteKey(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 negative error code.
static std::string dipki::Ecc::WriteKey (const std::string &internalKey, KeyType keyType=KeyType::Default)
This works the same as ECC_SaveKey except it outputs to a string rather than a file. The output is always in textual PEM form. See the remarks for ECC_SaveKey for details of key format.
Use this unencrypted PEM string as input for any ECC, signature, X.509 or ASN.1 function expecting a public or private ECC key. Exercise caution due to the lack of encryption and prefer the internal key string format for private keys to ensure better security.
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.
Dim strCurveName As String Dim strPriKeyHex As String Dim strPubKeyHex As String Dim strPriKeyInt As String Dim strPubKeyInt As String Dim strKeyPem As String ' Ref: https://www.ietf.org/archive/id/draft-ietf-cose-cbor-encoded-cert-20.html#appendix-A.1.4 strCurveName = "secp256r1" strPriKeyHex = "DC66B3415456D649429B53223DF7532B942D6B0E0842C30BCA4C0ACF91547BB2" strPubKeyHex = "02AE4CDB01F614DEFC7121285FDC7F5C6D1D42C95647F061BA0080DF678867845E" ' Read in keys to ephemeral internal representation strPriKeyInt = eccReadKeyByCurve(strPriKeyHex, strCurveName) strPubKeyInt = eccReadKeyByCurve(strPubKeyHex, strCurveName) ' Output private key in PEM form strKeyPem = eccWriteKey(strPriKeyInt) ' Default Debug.Print "PRI KEY default:" Debug.Print strKeyPem ' Show the ASN.1 structure... Debug.Print vbCrLf & "ASN.1 structure..." Debug.Print asn1TextDumpToString(strKeyPem) ' Output public key in compressed form... strKeyPem = eccWriteKey(strPubKeyInt, PKI_KEY_COMPRESS) Debug.Print "PUB KEY compressed:" Debug.Print strKeyPem ' Show the ASN.1 structure... Debug.Print vbCrLf & "ASN.1 structure..." Debug.Print asn1TextDumpToString(strKeyPem)
Note that the output values from eccWriteKey are the PEM strings beginning "-----BEGIN EC PRIVATE KEY"
and "-----BEGIN PUBLIC KEY".
The ASN.1 text dumps show the underlying structure.
TESTING ECC_WRITE_KEY... PRI KEY default: -----BEGIN EC PRIVATE KEY----- MHcCAQEEINxms0FUVtZJQptTIj33UyuULWsOCELDC8pMCs+RVHuyoAoGCCqGSM49 AwEHoUQDQgAErkzbAfYU3vxxIShf3H9cbR1CyVZH8GG6AIDfZ4hnhF7ppp/UiTFJ 2uPTsVQW11MsOHFSuAsN8+GvQIqV0wceWA== -----END EC PRIVATE KEY----- ASN.1 structure... 30 77 --SEQUENCE/119 bytes 02 01 --INTEGER/1 bytes 01 04 20 --OCTETSTRING/32 bytes dc 66 b3 41 54 56 d6 49 42 9b 53 22 3d f7 53 2b 94 2d 6b 0e 08 42 c3 0b ca 4c 0a cf 91 54 7b b2 a0 0a --[0]/10 bytes 06 08 --OBJECTIDENTIFIER/8 bytes 2a 86 48 ce 3d 03 01 07 --secp256r1 (1.2.840.10045.3.1.7) a1 44 --[1]/68 bytes 03 42 --BITSTRING/66 bytes 00 --0 unused bits 04 ae 4c db 01 f6 14 de fc 71 21 28 5f dc 7f 5c 6d 1d 42 c9 56 47 f0 61 ba 00 80 df 67 88 67 84 5e e9 a6 9f d4 89 31 49 da e3 d3 b1 54 16 d7 53 2c 38 71 52 b8 0b 0d f3 e1 af 40 8a 95 d3 07 1e 58 --(121 bytes) PUB KEY compressed: -----BEGIN PUBLIC KEY----- MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgACrkzbAfYU3vxxIShf3H9cbR1CyVZH 8GG6AIDfZ4hnhF4= -----END PUBLIC KEY----- ASN.1 structure... 30 39 --SEQUENCE/57 bytes 30 13 --SEQUENCE/19 bytes 06 07 --OBJECTIDENTIFIER/7 bytes 2a 86 48 ce 3d 02 01 --ecPublicKey (1.2.840.10045.2.1) 06 08 --OBJECTIDENTIFIER/8 bytes 2a 86 48 ce 3d 03 01 07 --secp256r1 (1.2.840.10045.3.1.7) 03 22 --BITSTRING/34 bytes 00 --0 unused bits 02 ae 4c db 01 f6 14 de fc 71 21 28 5f dc 7f 5c 6d 1d 42 c9 56 47 f0 61 ba 00 80 df 67 88 67 84 5e --(59 bytes)
ECC_ReadPrivateKey ECC_ReadPublicKey ECC_SaveKey ECC_SaveEncKey ECC_PublicKeyFromPrivate