Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.SshKey;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  key: TSshKey;
  keyStr: string;
  bEncrypt: Boolean;
  unencryptedKeyStr: string;
  encryptedKeyStr: string;

begin
  success := False;

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

  key := TSshKey.Create;

  //  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.
  key.Password := 'myKeyPassword';

  //  LoadText is a convenience method that reads any text file into a string.  It does not itself
  //  load the key.
  keyStr := key.LoadText('qa_data/putty_private_key.ppk');
  if (key.LastMethodSuccess = False) then
    begin
      WriteLn(key.LastErrorText);
      Exit;
    end;

  success := key.FromPuttyPrivateKey(keyStr);
  if (success = False) then
    begin
      WriteLn(key.LastErrorText);
      Exit;
    end;

  //  Export to an unencrypted OpenSSH key.
  bEncrypt := False;
  unencryptedKeyStr := key.ToOpenSshPrivateKey(bEncrypt);
  if (key.LastMethodSuccess = False) then
    begin
      WriteLn(key.LastErrorText);
      Exit;
    end;

  success := key.SaveText(unencryptedKeyStr,'qa_output/unencrypted_openssh.pem');
  if (success = False) then
    begin
      WriteLn(key.LastErrorText);
      Exit;
    end;

  //  Export to an encrypted OpenSSH key.  The Password property supplies the passphrase used to
  //  encrypt the output.
  bEncrypt := True;
  key.Password := 'myExportPassword';
  encryptedKeyStr := key.ToOpenSshPrivateKey(bEncrypt);
  if (key.LastMethodSuccess = False) then
    begin
      WriteLn(key.LastErrorText);
      Exit;
    end;

  success := key.SaveText(encryptedKeyStr,'qa_output/encrypted_openssh.pem');
  if (success = False) then
    begin
      WriteLn(key.LastErrorText);
      Exit;
    end;

  WriteLn('Done!');


  key.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.