Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @key int
    EXEC @hr = sp_OACreate 'Chilkat.SshKey', @key OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @key, 'GenerateEd25519Key', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @key
        RETURN
      END

    --  A comment may optionally be included in the exported key.
    EXEC sp_OASetProperty @key, 'Comment', 'This is my new ed25519 key.'

    --  Export to unencrypted PuTTY format.
    DECLARE @exportEncrypted int
    SELECT @exportEncrypted = 0
    DECLARE @exportedKey nvarchar(4000)
    EXEC sp_OAMethod @key, 'ToPuttyPrivateKey', @exportedKey OUT, @exportEncrypted
    EXEC sp_OAGetProperty @key, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @key
        RETURN
      END

    EXEC sp_OAMethod @key, 'SaveText', @success OUT, @exportedKey, 'qa_output/privkey_putty_unencrypted.ppk'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @key
        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.
    EXEC sp_OASetProperty @key, 'Password', 'myKeyPassword'
    SELECT @exportEncrypted = 1
    EXEC sp_OAMethod @key, 'ToPuttyPrivateKey', @exportedKey OUT, @exportEncrypted
    EXEC sp_OAGetProperty @key, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @key
        RETURN
      END

    EXEC sp_OAMethod @key, 'SaveText', @success OUT, @exportedKey, 'qa_output/privkey_putty_encrypted.ppk'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @key
        RETURN
      END


    PRINT 'Success!'

    EXEC @hr = sp_OADestroy @key


END
GO