Sample code for 30+ languages & platforms
Swift

Convert PuTTY Private Key (ppk) to OpenSSH (pem)

See more SSH Key Examples

Convert a PuTTY format private key file (.ppk) to OpenSSH (.pem).

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let key = CkoSshKey()!

    // Load an unencrypted or encrypted PuTTY private key.

    // If  your PuTTY private key is encrypted, set the Password
    // property before calling FromPuttyPrivateKey.
    // If your PuTTY private key is not encrypted, it makes no diffference
    // if Password is set or not set.
    key.password = "secret"

    // First load the .ppk file into a string:
    var keyStr: String?
    keyStr = key.loadText(path: "putty_private_key.ppk")

    // Import into the SSH key object:
    success = key.fromPuttyPrivateKey(keyStr: keyStr)
    if success != true {
        print("\(key.lastErrorText!)")
        return
    }

    // Convert to an encrypted or unencrypted OpenSSH key.

    // First demonstrate converting to an unencrypted OpenSSH key
    var unencryptedKeyStr: String?
    var bEncrypt: Bool = false
    unencryptedKeyStr = key.toOpenSshPrivateKey(bEncrypt: bEncrypt)
    success = key.saveText(strToSave: unencryptedKeyStr, path: "unencrypted_openssh.pem")
    if success != true {
        print("\(key.lastErrorText!)")
        return
    }

    // Save to an encrypted OpenSSH PEM file:
    var encryptedKeyStr: String?
    bEncrypt = true
    key.password = "myPassword"
    encryptedKeyStr = key.toOpenSshPrivateKey(bEncrypt: bEncrypt)
    success = key.saveText(strToSave: encryptedKeyStr, path: "encrypted_openssh.pem")
    if success != true {
        print("\(key.lastErrorText!)")
        return
    }

    print("Done!")

}