Sample code for 30+ languages & platforms
Unicode C

Export a Private Key as XML

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.GetXml method, which exports the key in Chilkat-compatible XML (for example an RSAKeyValue element). It takes no arguments; the XML is not encrypted.

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: The XML key format belongs to the .NET/XML-DSIG world, where keys are represented as XML elements. Exporting to it is what you do to hand a key to .NET code or a system built around XML signatures — the inverse of LoadXml. It is plaintext, so the exported XML must be protected like any unencrypted key.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkPrivateKeyW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPrivateKeyW privKey;
    const wchar_t *xml;

    success = FALSE;

    //  Load a private key to export.
    privKey = CkPrivateKeyW_Create();
    success = CkPrivateKeyW_LoadPemFile(privKey,L"qa_data/private.pem");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey));
        CkPrivateKeyW_Dispose(privKey);
        return;
    }

    //  Export in Chilkat-compatible XML (for example an RSAKeyValue element).  The XML is not
    //  encrypted.
    xml = CkPrivateKeyW_getXml(privKey);
    if (CkPrivateKeyW_getLastMethodSuccess(privKey) == FALSE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey));
        CkPrivateKeyW_Dispose(privKey);
        return;
    }

    wprintf(L"%s\n",xml);


    CkPrivateKeyW_Dispose(privKey);

    }