Sample code for 30+ languages & platforms
SQL Server

Load a Private Key from a PKCS #1 File

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.LoadPkcs1File method, which loads a private key from a file. The only argument is the file path. Despite the historical PKCS #1 name, it accepts any supported unencrypted private-key representation.

Note: The file path is relative to the application's current working directory. Absolute paths may also be used. Supply the path to your own key file.

Background: PKCS #1 is the older, RSA-specific key format — the BEGIN RSA PRIVATE KEY block still common from OpenSSL. This method reads it, and like the other content-inspecting loaders it also accepts other unencrypted representations, so it is forgiving of what is actually in the file. For unknown or mixed formats, prefer LoadAnyFormatFile.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the PrivateKey.LoadPkcs1File method, which loads a private key from a file.  The
    --  only argument is the file path.  Despite the historical PKCS #1 name, it accepts any supported
    --  unencrypted private-key representation.

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

    EXEC sp_OAMethod @privKey, 'LoadPkcs1File', @success OUT, 'qa_data/private_pkcs1.pem'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @privKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END


    EXEC sp_OAGetProperty @privKey, 'KeyType', @sTmp0 OUT

    PRINT 'Loaded a ' + @sTmp0 + ' private key.'

    EXEC @hr = sp_OADestroy @privKey


END
GO