Sample code for 30+ languages & platforms
Xbase++

Convert ASN.1 to/from Binary DER, XML, and Base64

Demonstrates how to convert ASN.1 from and to any of the following formats: binary DER, Base64, and XML.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oAsn
LOCAL cStrXml
LOCAL oXml
LOCAL cStrBase64
LOCAL oAsn2
LOCAL oAsn3

nSuccess := 0

oAsn := CreateObject("Chilkat.Asn")

//  Begin with loading ASN.1 from a binary DER/BER format file.
nSuccess := oAsn:LoadBinaryFile("/Users/chilkat/testData/p7b/test.p7b")
IF (nSuccess != 1)
    ? oAsn:LastErrorText
    oAsn:destroy()
    RETURN
ENDIF

//  Convert ASN.1 to XML:
cStrXml := oAsn:AsnToXml()
IF (oAsn:LastMethodSuccess != 1)
    ? oAsn:LastErrorText
    oAsn:destroy()
    RETURN
ENDIF

//  The XML returned by AsnToXml will be compact and not pretty-formatted.
//  Use Chilkat XML to format the XML better:
oXml := CreateObject("Chilkat.Xml")
nSuccess := oXml:LoadXml(cStrXml)
//  Assuming success for this example..
//  This is formatted better for human viewing:
? oXml:GetXml()

//  Now get the ASN.1 in base64 format.  Any encoding supported
//  by Chilkat can be passed, such as "hex", "uu", "quoted-printable", "base32", "modbase64", etc.
cStrBase64 := oAsn:GetEncodedDer("base64")

//  Load the ASN.1 from XML:
oAsn2 := CreateObject("Chilkat.Asn")
nSuccess := oAsn2:LoadAsnXml(oXml:GetXml())
IF (nSuccess != 1)
    ? oAsn2:LastErrorText
    oAsn:destroy()
    oXml:destroy()
    oAsn2:destroy()
    RETURN
ENDIF

//  Load the ASN.1 from an encoded string, such as base64:
oAsn3 := CreateObject("Chilkat.Asn")
nSuccess := oAsn3:LoadEncoded(cStrBase64, "base64")
IF (nSuccess != 1)
    ? oAsn3:LastErrorText
    oAsn:destroy()
    oXml:destroy()
    oAsn2:destroy()
    oAsn3:destroy()
    RETURN
ENDIF

oAsn:destroy()
oXml:destroy()
oAsn2:destroy()
oAsn3:destroy()