Sample code for 30+ languages & platforms
DataFlex

Generate an ed25519 Key and Save in PuTTY Format

See more SSH Examples

Demonstrates generating an ed25519 SSH key and saving it in PuTTY (.ppk) format, both unencrypted and encrypted. Setting the Password property and passing true to ToPuttyPrivateKey produces the encrypted form.

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

Background: Ed25519 is the modern default for SSH keys: it offers strong security with very small keys and fast signing, and unlike RSA it has no key-size decision to get wrong. Encrypting the exported private key protects it at rest, so a stolen file is not immediately usable — the trade-off being that the passphrase must be supplied whenever the key is used, which is why unattended jobs often pair an unencrypted key with strict file permissions instead.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoKey
    Boolean iExportEncrypted
    String sExportedKey
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  Demonstrates generating an ed25519 SSH key and saving it in PuTTY (.ppk) format, both
    //  unencrypted and encrypted.

    Get Create (RefClass(cComChilkatSshKey)) To hoKey
    If (Not(IsComObjectCreated(hoKey))) Begin
        Send CreateComObject of hoKey
    End

    Get ComGenerateEd25519Key Of hoKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  A comment may optionally be included in the exported key.
    Set ComComment Of hoKey To "This is my new ed25519 key."

    //  Export to unencrypted PuTTY format.
    Move False To iExportEncrypted
    Get ComToPuttyPrivateKey Of hoKey iExportEncrypted To sExportedKey
    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 sExportedKey "qa_output/privkey_putty_unencrypted.ppk" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Export to encrypted PuTTY format.  The passphrase protects the private key at rest and
    //  should come from a secure source rather than being hard-coded.
    Set ComPassword Of hoKey To "myKeyPassword"
    Move True To iExportEncrypted
    Get ComToPuttyPrivateKey Of hoKey iExportEncrypted To sExportedKey
    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 sExportedKey "qa_output/privkey_putty_encrypted.ppk" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Success!"


End_Procedure