Sample code for 30+ languages & platforms
Swift

Serialize a PFX to an Encoded String

See more PFX/P12 Examples

Demonstrates Pfx.ToEncodedString, which serializes the current PFX contents and returns the PKCS#12 bytes encoded as text using a named encoding.

Background. Common encodings are base64 and hex. This is convenient when the PKCS#12 data must be stored or transmitted as printable text.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let pfx = CkoPfx()!

    //  The file path is relative to the application's current working directory.  An absolute path
    //  may also be used.  Supply the path appropriate to your own environment.
    //  The PFX password should come from a secure source rather than being hard-coded.
    var password: String? = "myPfxPassword"
    success = pfx.loadFile(path: "qa_data/identity.pfx", password: password)
    if success == false {
        print("\(pfx.lastErrorText!)")
        return
    }

    //  Serialize the PFX contents using the given output password and return the PKCS#12 bytes encoded as
    //  text.  Common encodings include base64 and hex.
    var outputPassword: String? = "myOutputPassword"
    var encoded: String? = pfx.toEncodedString(password: outputPassword, encoding: "base64")
    if pfx.lastMethodSuccess == false {
        print("\(pfx.lastErrorText!)")
        return
    }

    print("\(encoded!)")

}