Sample code for 30+ languages & platforms
PowerBuilder

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

See more SSH Examples

Demonstrates converting a PuTTY format private key to OpenSSH format. The .ppk is imported with FromPuttyPrivateKey and re-exported with ToOpenSshPrivateKey, both unencrypted and encrypted.

Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.

Background: PuTTY and OpenSSH store the same underlying key material in different container formats, so converting between them is a re-encoding rather than a new key — the corresponding public key, and therefore the server-side authorized_keys entry, is unchanged. This matters when moving between Windows tooling built around PuTTY and Unix tooling that expects PEM. Note that the Password property serves double duty: it decrypts the key on import and encrypts it on export, so set it appropriately for each step.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Key
string ls_KeyStr
integer li_BEncrypt
string ls_UnencryptedKeyStr
string ls_EncryptedKeyStr

li_Success = 0

//  Demonstrates converting a PuTTY format private key (.ppk) to OpenSSH (.pem) format.

loo_Key = create oleobject
li_rc = loo_Key.ConnectToNewObject("Chilkat.SshKey")
if li_rc < 0 then
    destroy loo_Key
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Set the password before importing an encrypted PuTTY key.  If the key is not encrypted it
//  makes no difference whether Password is set.  This should come from a secure source rather
//  than being hard-coded.
loo_Key.Password = "myKeyPassword"

//  LoadText is a convenience method that reads any text file into a string.  It does not itself
//  load the key.
ls_KeyStr = loo_Key.LoadText("qa_data/putty_private_key.ppk")
if loo_Key.LastMethodSuccess = 0 then
    Write-Debug loo_Key.LastErrorText
    destroy loo_Key
    return
end if

li_Success = loo_Key.FromPuttyPrivateKey(ls_KeyStr)
if li_Success = 0 then
    Write-Debug loo_Key.LastErrorText
    destroy loo_Key
    return
end if

//  Export to an unencrypted OpenSSH key.
li_BEncrypt = 0
ls_UnencryptedKeyStr = loo_Key.ToOpenSshPrivateKey(li_BEncrypt)
if loo_Key.LastMethodSuccess = 0 then
    Write-Debug loo_Key.LastErrorText
    destroy loo_Key
    return
end if

li_Success = loo_Key.SaveText(ls_UnencryptedKeyStr,"qa_output/unencrypted_openssh.pem")
if li_Success = 0 then
    Write-Debug loo_Key.LastErrorText
    destroy loo_Key
    return
end if

//  Export to an encrypted OpenSSH key.  The Password property supplies the passphrase used to
//  encrypt the output.
li_BEncrypt = 1
loo_Key.Password = "myExportPassword"
ls_EncryptedKeyStr = loo_Key.ToOpenSshPrivateKey(li_BEncrypt)
if loo_Key.LastMethodSuccess = 0 then
    Write-Debug loo_Key.LastErrorText
    destroy loo_Key
    return
end if

li_Success = loo_Key.SaveText(ls_EncryptedKeyStr,"qa_output/encrypted_openssh.pem")
if li_Success = 0 then
    Write-Debug loo_Key.LastErrorText
    destroy loo_Key
    return
end if

Write-Debug "Done!"


destroy loo_Key