SQL Server
SQL Server
Convert DSA DER Private Key to PEM
See more DSA Examples
Converts a DSA private key from DER format to PEM. Demonstrates how to write both encrypted and unencrypted PEM formatted private keys.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
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
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.
DECLARE @dsa int
EXEC @hr = sp_OACreate 'Chilkat.Dsa', @dsa OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Load a DER private key.
EXEC sp_OAMethod @dsa, 'FromDerFile', @success OUT, 'dsa_priv.der'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @dsa, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @dsa
RETURN
END
DECLARE @pemStr nvarchar(4000)
-- Save to unencrypted PEM:
EXEC sp_OAMethod @dsa, 'ToPem', @pemStr OUT
EXEC sp_OAMethod @dsa, 'SaveText', @success OUT, @pemStr, 'dsa_priv.pem'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @dsa, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @dsa
RETURN
END
-- Save to encrypted PEM:
EXEC sp_OAMethod @dsa, 'ToEncryptedPem', @pemStr OUT, 'myPassword'
EXEC sp_OAMethod @dsa, 'SaveText', @success OUT, @pemStr, 'dsa_privEncrypted.pem'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @dsa, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @dsa
RETURN
END
PRINT 'Finished!'
EXEC @hr = sp_OADestroy @dsa
END
GO