DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoKey
String sKeyStr
Boolean iBEncrypt
String sUnencryptedKeyStr
String sEncryptedKeyStr
String sTemp1
Boolean bTemp1
Move False To iSuccess
// Demonstrates converting a PuTTY format private key (.ppk) to OpenSSH (.pem) format.
Get Create (RefClass(cComChilkatSshKey)) To hoKey
If (Not(IsComObjectCreated(hoKey))) Begin
Send CreateComObject of hoKey
End
// 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.
Set ComPassword Of hoKey To "myKeyPassword"
// LoadText is a convenience method that reads any text file into a string. It does not itself
// load the key.
Get ComLoadText Of hoKey "qa_data/putty_private_key.ppk" To sKeyStr
Get ComLastMethodSuccess Of hoKey To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoKey To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComFromPuttyPrivateKey Of hoKey sKeyStr To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoKey To sTemp1
Showln sTemp1
Procedure_Return
End
// Export to an unencrypted OpenSSH key.
Move False To iBEncrypt
Get ComToOpenSshPrivateKey Of hoKey iBEncrypt To sUnencryptedKeyStr
Get ComLastMethodSuccess Of hoKey To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoKey To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComSaveText Of hoKey sUnencryptedKeyStr "qa_output/unencrypted_openssh.pem" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoKey To sTemp1
Showln sTemp1
Procedure_Return
End
// Export to an encrypted OpenSSH key. The Password property supplies the passphrase used to
// encrypt the output.
Move True To iBEncrypt
Set ComPassword Of hoKey To "myExportPassword"
Get ComToOpenSshPrivateKey Of hoKey iBEncrypt To sEncryptedKeyStr
Get ComLastMethodSuccess Of hoKey To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoKey To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComSaveText Of hoKey sEncryptedKeyStr "qa_output/encrypted_openssh.pem" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoKey To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Done!"
End_Procedure