Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Sign and Encrypt using Cert to produce -----BEGIN PKCS7----- ... -----END PKCS7-----Demonstrates how to first sign content using your own cert + private key to produce PKCS7 binary output. Then encrypt the signed binary output using the recipient's public key to produce output such as: -----BEGIN PKCS7----- MIIHPwYJKoZIhvcNAQcEoIIHMDCCBywC ... ... ... -----END PKCS7-----
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- This example requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @crypt int -- Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @toBeSignedAndEncrypted nvarchar(4000) SELECT @toBeSignedAndEncrypted = 'This string is to be signed and then encrypted.' DECLARE @myCert int -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Cert', @myCert OUT DECLARE @success int EXEC sp_OAMethod @myCert, 'LoadPfxFile', @success OUT, 'qa_data/pfx/cert_test123.pfx', 'test123' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @myCert, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @crypt EXEC @hr = sp_OADestroy @myCert RETURN END EXEC sp_OAMethod @crypt, 'SetSigningCert', @success OUT, @myCert IF @success <> 1 BEGIN EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @crypt EXEC @hr = sp_OADestroy @myCert RETURN END -- Get the PKCS7 output in base64 format. EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64' -- Perhaps we don't want the entire cert chain to be included in the signature? EXEC sp_OASetProperty @crypt, 'IncludeCertChain', 0 DECLARE @signedPkcs7 nvarchar(4000) EXEC sp_OAMethod @crypt, 'OpaqueSignStringENC', @signedPkcs7 OUT, @toBeSignedAndEncrypted -- We're going to encrypt the binary PKCS7 signature (which contains the content) DECLARE @bdPkcs7 int -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdPkcs7 OUT EXEC sp_OAMethod @bdPkcs7, 'AppendEncoded', @success OUT, @signedPkcs7, 'base64' -- --------------------------------------------------- -- Now for the encryption... -- Specify the encryption to be used. -- "pki" indicates "Public Key Infrastructure" and will create a PKCS7 encrypted (enveloped) message. EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'pki' EXEC sp_OASetProperty @crypt, 'Pkcs7CryptAlg', 'aes' EXEC sp_OASetProperty @crypt, 'KeyLength', 128 EXEC sp_OASetProperty @crypt, 'OaepHash', 'sha256' EXEC sp_OASetProperty @crypt, 'OaepPadding', 1 -- A certificate is needed as the encryption key.. DECLARE @cert int -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'qa_data/certs/testCert.pem' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @crypt EXEC @hr = sp_OADestroy @myCert EXEC @hr = sp_OADestroy @bdPkcs7 EXEC @hr = sp_OADestroy @cert RETURN END -- Tell the crypt object to use the certificate. EXEC sp_OAMethod @crypt, 'SetEncryptCert', @success OUT, @cert EXEC sp_OAMethod @crypt, 'EncryptBd', @success OUT, @bdPkcs7 IF @success <> 1 BEGIN EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @crypt EXEC @hr = sp_OADestroy @myCert EXEC @hr = sp_OADestroy @bdPkcs7 EXEC @hr = sp_OADestroy @cert RETURN END -- Make a "-----BEGIN PKCS7-----" ... "-----END PKCS7-----" sandwich... DECLARE @sb int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, '-----BEGIN PKCS7-----', 1 EXEC sp_OAMethod @bdPkcs7, 'GetEncoded', @sTmp0 OUT, 'base64_mime' EXEC sp_OAMethod @sb, 'Append', @success OUT, @sTmp0 EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, '-----END PKCS7-----', 1 DECLARE @outStr nvarchar(4000) EXEC sp_OAMethod @sb, 'GetAsString', @outStr OUT PRINT @outStr -- Sample output: -- -----BEGIN PKCS7----- -- MIIMFQYJKoZIhvcNAQcDoIIMBjCCDAICAQAxggH5MIIB9QIBADCBsTCBmzELMAkGA1UEBhMCR0Ix -- GzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMR -- Q09NT0RPIENBIExpbWl0ZWQxQTA/BgNVBAMTOENPTU9ETyBTSEEtMjU2IENsaWVudCBBdXRoZW50 -- aWNhdGlvbiBhbmQgU2VjdXJlIEVtYWlsIENBAhEAuBl4qE2MODB05C5h53M5UDA4BgkqhkiG9w0B -- AQcwK6APMA0GCWCGSAFlAwQCAQUAoRgwFgYJKoZIhvcNAQEIMAkGBSsOAwIaBQAEggEAA34hkvxY -- TXMUAsWBmnRIvfFK920WeUVenM4ob7qibDiaXI2qWBgz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4fpck14aaEF5/+h3FvgVw23FieWYc6AImn/2zvtv6+gCJl7YnrG -- 9zSjHx2Kf7WRiFbpSqYXGGA0iD0b7So9oyrTItepIRQh0OFcDhCGXvhl2Sb7IQM7sL3WvbAHcseS -- 0xJiEIZogWbVKL5OqAn/VrJsL+0atvkh6qy5FVmk9CBWh92lkmwWyk8+CoTVC6DPMRXLv/MQar2W -- j0cOL12vohVJe9MSHTMR6kHaaxj1J8b5AswZTBsbgFo16Q6ihxLC1fd70OM1c6Hak341UgCxHsOr -- j9JfT+r5jpK3booIzBorGNJOkAuaBAEKdcnNpyCP2sDCzG+7Ws1H+lJzXVDXARftZSbMvfLctU49 -- 5YZl+bBifBF8fSSSRNKXMbPeyww1idjezWKiXQC3hA6VPKtW7rn1py7hTlkdm6os1jW9vmSmz30j -- KHvey6Kcy+fdc1gyYmXhSDD55uYVrj1JmbaZvIeGTh8cQ8E4Dsq2Hy2PQ8S2e2S6YcJ9X6yNkwXG -- KrqG2vK4AeBuro1IfoUG1EJ17Q== -- -----END PKCS7----- EXEC @hr = sp_OADestroy @crypt EXEC @hr = sp_OADestroy @myCert EXEC @hr = sp_OADestroy @bdPkcs7 EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sb END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.