Sample code for 30+ languages & platforms
C#

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 C# Downloads

C#
bool success = false;

//  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.

Chilkat.SshKey key = new Chilkat.SshKey();

success = key.GenerateEd25519Key();
if (success == false) {
    Debug.WriteLine(key.LastErrorText);
    return;
}

//  A comment may optionally be included in the exported key.
key.Comment = "This is my new ed25519 key.";

//  Export to unencrypted PuTTY format.
bool exportEncrypted = false;
string exportedKey = key.ToPuttyPrivateKey(exportEncrypted);
if (key.LastMethodSuccess == false) {
    Debug.WriteLine(key.LastErrorText);
    return;
}

success = key.SaveText(exportedKey,"qa_output/privkey_putty_unencrypted.ppk");
if (success == false) {
    Debug.WriteLine(key.LastErrorText);
    return;
}

//  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.
key.Password = "myKeyPassword";
exportEncrypted = true;
exportedKey = key.ToPuttyPrivateKey(exportEncrypted);
if (key.LastMethodSuccess == false) {
    Debug.WriteLine(key.LastErrorText);
    return;
}

success = key.SaveText(exportedKey,"qa_output/privkey_putty_encrypted.ppk");
if (success == false) {
    Debug.WriteLine(key.LastErrorText);
    return;
}

Debug.WriteLine("Success!");