Sample code for 30+ languages & platforms
SQL Server

Load a Public Key from Base64 DER

Demonstrates the Chilkat PublicKey.LoadBase64 method, which loads a public key from standard Base64-encoded DER. The only argument is the Base64 string; whitespace within it is permitted.

Background: A public key in DER (binary) form is often carried as Base64 text so it can travel through channels that expect a string — a config value, a JSON field, a certificate export. This loader decodes that text and inspects the DER to determine the key type and structure automatically. It is the plain-Base64 counterpart to loading a full PEM block, which additionally has the BEGIN/END armor.

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 PublicKey.LoadBase64 method, which loads a public key from standard
    --  Base64-encoded DER.  The only argument is the Base64 string (whitespace is allowed).

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

    --  The Base64 of the DER-encoded public key.
    DECLARE @keyStr nvarchar(4000)
    SELECT @keyStr = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...IDAQAB'

    EXEC sp_OAMethod @pubKey, 'LoadBase64', @success OUT, @keyStr
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pubKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pubKey
        RETURN
      END


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

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

    EXEC @hr = sp_OADestroy @pubKey


END
GO