CryptoSys PKI Toolkit Manual

RSA_ToXMLString

RSA_ToXMLString creates an XML string representation of an RSA internal key string.

VB6/VBA Syntax

Public Declare Function RSA_ToXMLString Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strKeyString As String, ByVal nOptions As Long) As Long

nRet = RSA_ToXMLString(strOutput, nOutChars, strKeyString, nOptions) As Long

Parameters

strOutput
[out] String to receive XML data.
nOutChars
[in] Long specifying the maximum number of characters to be received.
strKeyString
[in] String containing the RSA public or private key in internal format
nOptions
[in] Long option flags: Select and combine:
PKI_DEFAULT (0) to output in appropriate W3C standard format (RSAKeyValue for public key and RSAKeyPair for private key)
PKI_XML_RSAKEYVALUE (1) to force output as .NET-compatible RSAKeyValue format
PKI_XML_EXCLPRIVATE (0x10) to exclude the private key
PKI_XML_HEXBINARY (0x100) to output with data in non-conforming hexBinary format

C/C++ Syntax

long _stdcall RSA_ToXMLString(char *szOutput, long nOutChars, const char *szKeyString, long nOptions);

Returns (VB6/C)

Long: If successful, the return value is the number of characters in the output string; otherwise it returns a negative error code.

.NET Equivalent

Rsa.ToXMLString Method

Remarks

Caution: the private key is saved in unencrypted form. Do not use for a production key.

Call the function with a zero value of nOutChars to find out the required length of the string. C/C++ users should add one to this value before allocating memory. Both public and private key data can be output. The key must have been read first into an internal key string using one of the other RSA key input functions in this toolkit.

If the internal key is a public key, or if the PKI_XML_EXCLPRIVATE option is used with a private key, the output will always be a RSAKeyValue element containing just <Modulus> and <Exponent> elements as per [XMLSIG].

If the internal key is a private key and the PKI_XML_EXCLPRIVATE option is not used, the default output will be a a XKMS-conforming RSAKeyPair element with the private key parameters included. Including the PKI_XML_RSAKEYVALUE option will force a .NET-compatible RSAKeyValue element instead. The only difference between RSAKeyPair and RSAKeyValue is in the name of the outer XML element. The default behaviour is to comply with the W3C standards XKMS and [XMLSIG]. Users who wish to export a private key to use in the .NET world will probably want to use the PKI_XML_RSAKEYVALUE option.

The PKI_XML_HEXBINARY option will output the binary data in hexBinary encoding format instead of base64. This latter format is not in conformance with any W3C standard, but is provided to allow users to see the data in more readable hex format. Such a format can be read by this toolkit's RSA_FromXMLString function, but don't try using it anywhere else.

Example

This example reads in a private key from a encrypted private key file and then converts to an XML string in the .NET-compatible format.

    Dim strEPKFile As String
    Dim strPassword As String
    Dim strPrivateKey As String
    Dim strXML As String
    Dim nLen As Long

    strEPKFile = "AlicePrivRSASign.epk"
    strPassword = "password"
    
    ' Read in the deciphered private key string in our internal format
    strPrivateKey = rsaReadPrivateKey(strEPKFile, strPassword)
    If Len(strPrivateKey) = 0 Then
        MsgBox "Unable to retrieve private key"
        Exit Function
    End If
    Debug.Print "INTKEY=" & strPrivateKey
    
    ' Convert to XML
    nLen = RSA_ToXMLString("", 0, strPrivateKey, PKI_XML_RSAKEYVALUE)
    ' pre-dimension first
    strXML = String(nLen, " ")
    nLen = RSA_ToXMLString(strXML, Len(strXML), strPrivateKey, PKI_XML_RSAKEYVALUE)
    strXML = Left(strXML, nLen)    
    Debug.Print "XML=" & strXML

The output should look like this (only longer):

INTKEY=MIICXAIBAAKBgQDgiXM5jdj19eiHdjl/ ...
XML=<RSAKeyValue><Modulus>4IlzOY3Y9fXoh ... +yRRKt/IQ==</D></RSAKeyValue>

See Also

RSA_FromXMLString

[Contents] [Index]

[HOME]   [NEXT: TDEA_B64Mode...]

Copyright © 2004-10 D.I. Management Services Pty Ltd. All rights reserved.