(Swift) Convert DSA DER Private Key to PEM
Converts a DSA private key from DER format to PEM. Demonstrates how to write both encrypted and unencrypted PEM formatted private keys.
func chilkatTest() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let dsa = CkoDsa()!
// Load a DER private key.
var success: Bool = dsa.fromDerFile("dsa_priv.der")
if success != true {
print("\(dsa.lastErrorText!)")
return
}
var pemStr: String?
// Save to unencrypted PEM:
pemStr = dsa.toPem()
success = dsa.saveText(pemStr, path: "dsa_priv.pem")
if success != true {
print("\(dsa.lastErrorText!)")
return
}
// Save to encrypted PEM:
pemStr = dsa.toEncryptedPem("myPassword")
success = dsa.saveText(pemStr, path: "dsa_privEncrypted.pem")
if success != true {
print("\(dsa.lastErrorText!)")
return
}
print("Finished!")
}
|