Sample code for 30+ languages & platforms
SQL Server

RSA Import Private Key

See more RSA Examples

Shows how to select/import a private key for RSA signing or decryption.

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 @privKey int
    EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @password nvarchar(4000)
    SELECT @password = 'secret'
    -- In all Chilkat methods expecting a path, you pass either absolute or relative paths.
    EXEC sp_OAMethod @privKey, 'LoadAnyFormatFile', @success OUT, 'rsaKeys/myTestRsaPrivate.pem', @password
    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

    -- Tell the RSA object to use the private key (i.e. import the private key)
    EXEC sp_OAMethod @rsa, 'UsePrivateKey', @success OUT, @privKey

    EXEC @hr = sp_OADestroy @privKey
    EXEC @hr = sp_OADestroy @rsa


END
GO