SQL Server
SQL Server
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 SQL Server Downloads
-- 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
-- Demonstrates converting a PuTTY format private key (.ppk) to OpenSSH (.pem) format.
DECLARE @key int
EXEC @hr = sp_OACreate 'Chilkat.SshKey', @key OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- 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.
EXEC sp_OASetProperty @key, 'Password', 'myKeyPassword'
-- LoadText is a convenience method that reads any text file into a string. It does not itself
-- load the key.
DECLARE @keyStr nvarchar(4000)
EXEC sp_OAMethod @key, 'LoadText', @keyStr OUT, 'qa_data/putty_private_key.ppk'
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, 'FromPuttyPrivateKey', @success OUT, @keyStr
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @key
RETURN
END
-- Export to an unencrypted OpenSSH key.
DECLARE @bEncrypt int
SELECT @bEncrypt = 0
DECLARE @unencryptedKeyStr nvarchar(4000)
EXEC sp_OAMethod @key, 'ToOpenSshPrivateKey', @unencryptedKeyStr OUT, @bEncrypt
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, @unencryptedKeyStr, 'qa_output/unencrypted_openssh.pem'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @key
RETURN
END
-- Export to an encrypted OpenSSH key. The Password property supplies the passphrase used to
-- encrypt the output.
SELECT @bEncrypt = 1
EXEC sp_OASetProperty @key, 'Password', 'myExportPassword'
DECLARE @encryptedKeyStr nvarchar(4000)
EXEC sp_OAMethod @key, 'ToOpenSshPrivateKey', @encryptedKeyStr OUT, @bEncrypt
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, @encryptedKeyStr, 'qa_output/encrypted_openssh.pem'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @key
RETURN
END
PRINT 'Done!'
EXEC @hr = sp_OADestroy @key
END
GO