Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

SQL Server Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(SQL Server) Get Public Key from CSR

Demonstrates how to get the public key from a CSR.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    --  This requires the Chilkat API to have been previously unlocked.
    --  See Global Unlock Sample for sample code.

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

    --  No password is required.  Pass an empty password string..
    DECLARE @noPassword nvarchar(4000)
    SELECT @noPassword = ''
    DECLARE @success int
    EXEC sp_OAMethod @pem, 'LoadPemFile', @success OUT, 'qa_data/csr/myCsr.pem', @noPassword
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @pem, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pem
        RETURN
      END

    DECLARE @strBase64 nvarchar(4000)
    EXEC sp_OAMethod @pem, 'GetEncodedItem', @strBase64 OUT, 'csr', '', 'base64', 0

    DECLARE @asn int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Asn', @asn OUT

    EXEC sp_OAMethod @asn, 'LoadEncoded', @success OUT, @strBase64, 'base64'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @asn, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pem
        EXEC @hr = sp_OADestroy @asn
        RETURN
      END

    --  Convert the ASN.1 to XML.
    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Xml', @xml OUT

    EXEC sp_OAMethod @asn, 'AsnToXml', @sTmp0 OUT
    EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @sTmp0
    EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

    PRINT '----'

    DECLARE @strModulusHex nvarchar(4000)
    EXEC sp_OAMethod @xml, 'GetChildContent', @strModulusHex OUT, 'bits'

    PRINT 'strModulusHex = ' + @strModulusHex

    PRINT '----'

    --  We need the modulus as base64.
    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.BinData', @bd OUT

    EXEC sp_OAMethod @bd, 'AppendEncoded', @success OUT, @strModulusHex, 'hex'
    DECLARE @modulus64 nvarchar(4000)
    EXEC sp_OAMethod @bd, 'GetEncoded', @modulus64 OUT, 'base64'

    PRINT 'modulus64 = ' + @modulus64

    PRINT '----'

    --  Build the XML for the public key.
    DECLARE @xmlPubKey int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Xml', @xmlPubKey OUT

    EXEC sp_OASetProperty @xmlPubKey, 'Tag', 'RSAPublicKey'
    EXEC sp_OAMethod @xmlPubKey, 'UpdateChildContent', NULL, 'Modulus', @modulus64
    --  The RSA exponent will always be decimal 65537 (base64 = AQAB)
    EXEC sp_OAMethod @xmlPubKey, 'UpdateChildContent', NULL, 'Exponent', 'AQAB'


    PRINT 'RSA public key as XML:'
    EXEC sp_OAMethod @xmlPubKey, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

    PRINT '----'

    --  Load the XML into a Chilkat public key object.
    DECLARE @pubkey int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.PublicKey', @pubkey OUT

    EXEC sp_OAMethod @xmlPubKey, 'GetXml', @sTmp0 OUT
    EXEC sp_OAMethod @pubkey, 'LoadFromString', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @pubkey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pem
        EXEC @hr = sp_OADestroy @asn
        EXEC @hr = sp_OADestroy @xml
        EXEC @hr = sp_OADestroy @bd
        EXEC @hr = sp_OADestroy @xmlPubKey
        EXEC @hr = sp_OADestroy @pubkey
        RETURN
      END

    --  Show the public key as PEM.
    DECLARE @preferPkcs1 int
    SELECT @preferPkcs1 = 1
    EXEC sp_OAMethod @pubkey, 'GetPem', @sTmp0 OUT, @preferPkcs1
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @pem
    EXEC @hr = sp_OADestroy @asn
    EXEC @hr = sp_OADestroy @xml
    EXEC @hr = sp_OADestroy @bd
    EXEC @hr = sp_OADestroy @xmlPubKey
    EXEC @hr = sp_OADestroy @pubkey


END
GO

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.