Sample code for 30+ languages & platforms
SQL Server

Load a Public Key from a String

Demonstrates the Chilkat PublicKey.LoadFromString method, which loads a public key from a string. The only argument is the string. Chilkat examines the content and recognizes PEM public keys, unencrypted PEM private keys (taking only the public portion), XML, JWK, and more.

Background: This is the forgiving text loader: rather than requiring you to know the exact format, it inspects whatever string you provide and figures it out. A useful convenience is that it accepts an unencrypted private key too and keeps only the public half — handy when you have a key pair but need just the public key. Use it whenever the textual format is uncertain or varies by source.

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.LoadFromString method, which loads a public key from a string.  The
    --  only argument is the string.  Chilkat examines the content and recognizes PEM public keys,
    --  unencrypted PEM private keys (using only the public portion), XML, JWK, and more.

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

    --  The PEM text of a public key.
    DECLARE @keyText nvarchar(4000)
    SELECT @keyText = '-----BEGIN PUBLIC KEY-----' + CHAR(10) + 'MIIBIjANBgkq...IDAQAB' + CHAR(10) + '-----END PUBLIC KEY-----'

    EXEC sp_OAMethod @pubKey, 'LoadFromString', @success OUT, @keyText
    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