Sample code for 30+ languages & platforms
SQL Server

DSA Public Key PEM to DER Conversion

See more DSA Examples

Converts a DSA public key from PEM format to DER.

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

    -- 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 PEM public key.
    DECLARE @pemPublicKey nvarchar(4000)

    EXEC sp_OAMethod @dsa, 'LoadText', @pemPublicKey OUT, 'dsa_pub.pem'
    -- Import the public key PEM into the DSA object.
    EXEC sp_OAMethod @dsa, 'FromPublicPem', @success OUT, @pemPublicKey
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @dsa, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @dsa
        RETURN
      END

    -- Write it out as a DER file:
    EXEC sp_OAMethod @dsa, 'ToPublicDerFile', @success OUT, 'dsa_pub.der'
    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