DataFlex
DataFlex
Export a Private Key as DER into a BinData
See more Private Key Examples
Demonstrates the Chilkat PrivateKey.GetPkcsBd method, which exports the key as DER into a BinData. The first argument selects PKCS #1 (true) versus PKCS #8 (false), the second is the password (empty for unencrypted), and the third is the BinData that receives the bytes.
Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background: This is the in-memory binary export — the raw DER bytes land in a
BinData ready to hash, encrypt, or hand to another API without a temporary file. One method covers several cases: the boolean picks the traditional (PKCS #1) versus modern (PKCS #8) structure, and a nonempty password produces an encrypted PKCS #8 export (with the cipher from Pkcs8EncryptAlg). Source any password securely.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPrivKey
String sPassword
Boolean iUsePkcs1
Variant vBd
Handle hoBd
String sTemp1
Integer iTemp1
Move False To iSuccess
// Load a private key to export.
Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
If (Not(IsComObjectCreated(hoPrivKey))) Begin
Send CreateComObject of hoPrivKey
End
Get ComLoadPemFile Of hoPrivKey "qa_data/private.pem" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPrivKey To sTemp1
Showln sTemp1
Procedure_Return
End
// The key password is not hard-coded in a real application; obtain it from an interactive
// prompt, environment variable, or a secrets vault.
Move "myKeyPassword" To sPassword
// Export the key as DER into a BinData. The 1st argument selects PKCS #1 (True) versus PKCS #8
// (False); the 2nd is the password (empty string for an unencrypted export); the 3rd is the
// BinData that receives the DER bytes.
Move False To iUsePkcs1
Get Create (RefClass(cComChilkatBinData)) To hoBd
If (Not(IsComObjectCreated(hoBd))) Begin
Send CreateComObject of hoBd
End
Get pvComObject of hoBd to vBd
Get ComGetPkcsBd Of hoPrivKey iUsePkcs1 sPassword vBd To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPrivKey To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComNumBytes Of hoBd To iTemp1
Showln "Exported " iTemp1 " DER bytes."
End_Procedure