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) PKCS7 Encrypt using Multiple CertificatesEncrypts MIME using multiple digital certificates. Calls AddEncryptCert once per certificate, followed by a call to EncryptN. The MIME may be decrypted by any owner of one of the certificates + its associated private key.
-- 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 @mime int -- Use "Chilkat_9_5_0.Mime" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END -- Build a simple MIME message to be encrypted: DECLARE @success int EXEC sp_OAMethod @mime, 'AddHeaderField', @success OUT, 'Content-Type', 'text/plain' EXEC sp_OAMethod @mime, 'AddHeaderField', @success OUT, 'abc', '123' EXEC sp_OAMethod @mime, 'SetBody', NULL, 'This is a test' -- A digital certificate is required to create PKCS7 encrypted MIME. -- It can come from a variety of sources: .cer file, .pfx file, PEM files, -- an in-memory representation, or directly from a Windows -- registry-based certificate store. -- Load one cert from a .cer file: DECLARE @cert1 int -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert1 OUT EXEC sp_OAMethod @cert1, 'LoadFromFile', @success OUT, 'myCert.cer' IF @success = 0 BEGIN EXEC sp_OAGetProperty @cert1, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @mime EXEC @hr = sp_OADestroy @cert1 RETURN END -- Load another cert ... DECLARE @cert2 int -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert2 OUT EXEC sp_OAMethod @cert2, 'LoadFromFile', @success OUT, 'anotherCert.cer' IF @success = 0 BEGIN EXEC sp_OAGetProperty @cert2, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @mime EXEC @hr = sp_OADestroy @cert1 EXEC @hr = sp_OADestroy @cert2 RETURN END -- Tell the MIME object to use these certs for encrypting: -- (AddEncryptCert shouldn't fail if we already know it contains -- a valid certificate..) EXEC sp_OAMethod @mime, 'AddEncryptCert', @success OUT, @cert1 EXEC sp_OAMethod @mime, 'AddEncryptCert', @success OUT, @cert2 -- Encrypt the MIME: EXEC sp_OAMethod @mime, 'EncryptN', @success OUT IF @success = 0 BEGIN EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @mime EXEC @hr = sp_OADestroy @cert1 EXEC @hr = sp_OADestroy @cert2 RETURN END -- Display the MIME: EXEC sp_OAMethod @mime, 'GetMime', @sTmp0 OUT PRINT @sTmp0 -- The resulting S/MIME looks something like this: -- abc: 123 -- Content-Disposition: attachment; filename="smime.p7m" -- Content-Transfer-Encoding: base64 -- Content-Type: application/x-pkcs7-mime; -- name="smime.p7m" -- -- MIID4QYJKoZIhvcNAQcDoIID0jCCA84CAQAxggNlMIIBgQIBADBpMFUxCzAJBgNVBAYTAlpBMSUw -- IwYDVQQKExxUaGF3dGUgQ29uc3VsdGluZyAoUHR5KSBMdGQuMR8wHQYDVQQDExZUaGF3dGUgQ29k -- ZSBTaWduaW5nIENBAhB4ouTcAmLszrGi170k1deSMA0GCSqGSIb3DQEBAQUABIIBAKnOPgmISrNH -- Hd+zESvH5dl5TvBLCxLuIuH6cuZdowHzCqAUzaYFdn1rp88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YR7bs+2Cf4JbyuMOA== EXEC @hr = sp_OADestroy @mime EXEC @hr = sp_OADestroy @cert1 EXEC @hr = sp_OADestroy @cert2 END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.