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

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

See more SSH Key Examples

Convert a PuTTY format private key file (.ppk) to OpenSSH (.pem).

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;
  unencryptedKeyStr: string;
  bEncrypt: Boolean;
  encryptedKeyStr: string;

begin
  success := False;

  key := TSshKey.Create;

  //  Load an unencrypted or encrypted PuTTY private key.

  //  If  your PuTTY private key is encrypted, set the Password
  //  property before calling FromPuttyPrivateKey.
  //  If your PuTTY private key is not encrypted, it makes no diffference
  //  if Password is set or not set.
  key.Password := 'secret';

  //  First load the .ppk file into a string:

  keyStr := key.LoadText('putty_private_key.ppk');

  //  Import into the SSH key object:
  success := key.FromPuttyPrivateKey(keyStr);
  if (success <> True) then
    begin
      WriteLn(key.LastErrorText);
      Exit;
    end;

  //  Convert to an encrypted or unencrypted OpenSSH key.

  //  First demonstrate converting to an unencrypted OpenSSH key

  bEncrypt := False;
  unencryptedKeyStr := key.ToOpenSshPrivateKey(bEncrypt);
  success := key.SaveText(unencryptedKeyStr,'unencrypted_openssh.pem');
  if (success <> True) then
    begin
      WriteLn(key.LastErrorText);
      Exit;
    end;

  //  Save to an encrypted OpenSSH PEM file:

  bEncrypt := True;
  key.Password := 'myPassword';
  encryptedKeyStr := key.ToOpenSshPrivateKey(bEncrypt);
  success := key.SaveText(encryptedKeyStr,'encrypted_openssh.pem');
  if (success <> True) 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.