Sample code for 30+ languages & platforms
SQL Server

Load PuTTY Private Key

See more SSH Key Examples

Loads a PuTTY private key into an SSH key object.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @bCrlf int
    SELECT @bCrlf = 1
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'PuTTY-User-Key-File-2: ssh-rsa', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'Encryption: aes256-cbc', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'Comment: rsa-key-20161121', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'Public-Lines: 6', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'AAAAB3NzaC1yc2EAAAABJQAAAQEArIVxZTGYjqqNegJeMqgZzXKuX3YtxmD4DPnQ', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'K01SXoR9UA4wl6RJK5FGonI5spVnddr6HJny0eY5/RmV1dyL0fNdH2pfnHHoTd9B', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'uXG0PcTn4BqJFlybC6NNaGaByqpNPaOgjJIMHXTaeCuDuzwP02f3UKSfLAFdvUNP', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'fX3yrMRabjVcHJpgUn535Y9Y3ZphSFssiElLpp1vo6QLsdiAl0wgU4HLw9Dfg8sh', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'p3zgAufYUUIx2HChITJ7OO/PH5GEbPvJFbYH4Wx33GEe56JCF9QxxDCqNxsPMrvu', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'mqkSIPIELFl5zVYSFD9PiL8RZQcPJGq5zbF9+szsS/6DSH8biw==', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'Private-Lines: 14', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'SVqUm9zBUN6zaAaUY6TLnIXAhxqv4s1UhzukcN7Ns0Fs1EqdDP4nVMKgmiDxHaAj', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'ucb1PLv7uj2eGt69E+HKH4AC2LtJZNH4qLw+p8QKqdN79oRAMHwtS4cU88eeQ91t', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, '2W3wzmTzvYFlE6VbUr3bNl3f68OCQHz+7YJuL8g0eclNuveHB/VmBqnek1LL/cRY', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'wmk8wyzHRl7X8fyjGX0PiOPyVwXBDJFNjjKAdrRCRS0Cc4PWAb/18qrA9ApArQVk', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'a5miOMfrXFF2Vg3JyNgfdDilNKkry9x5oTtfueZkFgu/6pofP5qrTstr+BmB16mu', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'MxcDRF+jOm9XvU1OnrzQ9rBzuaHEfhSHqlKyMGTDuzRtNyxlDBgK4DaZ2z1FJQGF', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, '63p36KgVqx571PUJ8P/28VUb6s/OtXcjhxxvhC8Eo0wrTdjzDiX+IovdjOhWgn4v', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'xugaYocotTbhqq+O1w9xYtmG8fP921+TIpHzCkE8QTJzLJ40pemKZ+TAOmhb0OUQ', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'jemqmRohcxx7NEoZ0TsoAe6i+GQJ2cEE7LcNcSX3V66AkyTsTieqk1nSxyHyoF+l', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'rTaVOhFM5H8Etzv94SP3c1if3Qa9cmZaEmfaqrS94bzX+OY/zRIFweT0BeMo3Uac', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'fP6gBDBiLYZeRURcx0mYRdNYlssaKoYLA6c7F3ggo34VfgD6LP+ZYnG8yAXGXSPr', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'lgrfdg/szqSsOKVdkoGTkVy+BgrhITnFIs8TLZsrtSEPmXjEa+QvdP6FPsyN5u6P', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, '8s0i8EC0KT0YA77Ps29DEU3fxsXVSc9Xd9qdiWfJ0ce2hOsTwxL1gxtXUKP4xCzm', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'oXaWLFajiws2EjgBbwREj12v0I2N4xrpf/PeJq22EvWLYm9H94DdFrkm0b4p6Kmu', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'Private-MAC: ef04289f158f498b8050e19f1630b4764fe64ab9', @bCrlf

    DECLARE @sshKey int
    EXEC @hr = sp_OACreate 'Chilkat.SshKey', @sshKey OUT

    -- Must specify a password for this PuTTY private key because it is encrypted.
    EXEC sp_OASetProperty @sshKey, 'Password', 'secret'

    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @sshKey, 'FromPuttyPrivateKey', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @sshKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
      END


    PRINT 'success = ' + @success

    EXEC @hr = sp_OADestroy @sb
    EXEC @hr = sp_OADestroy @sshKey


END
GO