Sample code for 30+ languages & platforms
SQL Server

RSA Sign with PKCS8 Encrypted Key

See more RSA Examples

Demonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature (and then verify it).

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
    -- 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 assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

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

    -- Load the private key from an RSA PEM file:
    EXEC sp_OAMethod @privKey, 'LoadAnyFormatFile', @success OUT, 'raul_privateKey.key', 'a0123456789'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @privKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END

    DECLARE @rsa int
    EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa OUT

    -- Import the private key into the RSA component:
    EXEC sp_OAMethod @rsa, 'UsePrivateKey', @success OUT, @privKey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privKey
        EXEC @hr = sp_OADestroy @rsa
        RETURN
      END

    -- This example will sign a string, and receive the signature
    -- in a hex-encoded string.  Therefore, set the encoding mode
    -- to "hex":
    EXEC sp_OASetProperty @rsa, 'EncodingMode', 'hex'

    DECLARE @strData nvarchar(4000)
    SELECT @strData = 'This is the string to be signed.'

    -- Sign the string using the sha256 hash algorithm.
    -- Other valid choices are sha1, sha384, sha512 and others.
    DECLARE @hexSig nvarchar(4000)
    EXEC sp_OAMethod @rsa, 'SignStringENC', @hexSig OUT, @strData, 'sha256'
    EXEC sp_OAGetProperty @rsa, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privKey
        EXEC @hr = sp_OADestroy @rsa
        RETURN
      END


    PRINT @hexSig

    -- Now verify with the public key.
    -- This example shows how to use the public key from 
    -- a digital certificate (.cer file)
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'raul_publicKey.cer'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privKey
        EXEC @hr = sp_OADestroy @rsa
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    DECLARE @pubKey int
    EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @pubKey OUT

    EXEC sp_OAMethod @cert, 'GetPublicKey', @success OUT, @pubKey

    DECLARE @rsa2 int
    EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa2 OUT

    EXEC sp_OAMethod @rsa2, 'UsePublicKey', @success OUT, @pubKey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rsa2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privKey
        EXEC @hr = sp_OADestroy @rsa
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @pubKey
        EXEC @hr = sp_OADestroy @rsa2
        RETURN
      END

    -- Verify the signature against the original data:
    EXEC sp_OASetProperty @rsa2, 'EncodingMode', 'hex'
    EXEC sp_OAMethod @rsa2, 'VerifyStringENC', @success OUT, @strData, 'sha256', @hexSig
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rsa2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privKey
        EXEC @hr = sp_OADestroy @rsa
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @pubKey
        EXEC @hr = sp_OADestroy @rsa2
        RETURN
      END


    PRINT 'Signature verified!'

    -- Verify with incorrect data:
    EXEC sp_OAMethod @rsa2, 'VerifyStringENC', @success OUT, 'something else', 'sha256', @hexSig
    IF @success <> 1
      BEGIN

        PRINT 'Signature not verified! (which was expected in this case)'
      END
    ELSE
      BEGIN

        PRINT 'Hmmm... that''s not right...'
      END

    EXEC @hr = sp_OADestroy @privKey
    EXEC @hr = sp_OADestroy @rsa
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @pubKey
    EXEC @hr = sp_OADestroy @rsa2


END
GO