Sample code for 30+ languages & platforms
SQL Server

Load a Private Key from PEM Text

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.LoadPem method, which loads a private key from PEM text. The only argument is the PEM string. It has no password argument, so it cannot load an encrypted PEM — use LoadEncryptedPem for that.

Background: PEM is the Base64 text format (delimited by -----BEGIN----- lines) that most tooling uses for keys. This method is for the plain, unencrypted case where the key material sits in the clear inside the PEM — convenient, but it means the file itself is the only thing protecting the key. When the PEM is password-protected (a BEGIN ENCRYPTED PRIVATE KEY block or a legacy encrypted header), reach for the Encrypted loader instead.

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 @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the PrivateKey.LoadPem method, which loads a private key from PEM text.  The only
    --  argument is the PEM string.  This method has no password argument, so it cannot load an
    --  encrypted PEM -- use LoadEncryptedPem for that.

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

    --  The PEM text of an unencrypted private key.
    DECLARE @pemText nvarchar(4000)
    SELECT @pemText = '-----BEGIN PRIVATE KEY-----' + CHAR(10) + 'MIIEvAIBADANBgkq...' + CHAR(10) + '-----END PRIVATE KEY-----'

    EXEC sp_OAMethod @privKey, 'LoadPem', @success OUT, @pemText
    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