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) Encrypt MIME using RSAES-OAEP with SHA256 and AES-128 content encryptionDemonstrates how to encrypt MIME using RSAES-OAEP with SHA256 and AES-128 content encryption. Note: This example requires Chilkat v9.5.0.67 or greater.
-- 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 requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @success int 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 -- Create a MIME message to encrypt. EXEC sp_OAMethod @mime, 'NewMultipartMixed', @success OUT EXEC sp_OAMethod @mime, 'AddHeaderField', @success OUT, 'someHeader1', 'Some value 1' EXEC sp_OAMethod @mime, 'AddHeaderField', @success OUT, 'someHeader2', 'Some value 2' DECLARE @part0 int -- Use "Chilkat_9_5_0.Mime" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Mime', @part0 OUT EXEC sp_OAMethod @part0, 'AddHeaderField', @success OUT, 'hdrA', 'Some value A' EXEC sp_OAMethod @part0, 'AddHeaderField', @success OUT, 'hdrB', 'Some value B' EXEC sp_OASetProperty @part0, 'ContentType', 'text/plain' EXEC sp_OAMethod @part0, 'SetBody', NULL, 'This is the plain-text body.' DECLARE @part1 int -- Use "Chilkat_9_5_0.Mime" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Mime', @part1 OUT EXEC sp_OAMethod @part1, 'AddHeaderField', @success OUT, 'hdrX', 'Some value X' EXEC sp_OAMethod @part1, 'AddHeaderField', @success OUT, 'hdrY', 'Some value Y' EXEC sp_OASetProperty @part1, 'ContentType', 'text/xml' EXEC sp_OAMethod @part1, 'SetBody', NULL, '<a>This is the XML body</a>' EXEC sp_OAMethod @mime, 'AppendPart', @success OUT, @part0 EXEC sp_OAMethod @mime, 'AppendPart', @success OUT, @part1 -- The MIME to be encrypted: DECLARE @sbMime int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbMime OUT EXEC sp_OAMethod @mime, 'GetMimeSb', @success OUT, @sbMime EXEC sp_OAMethod @sbMime, 'GetAsString', @sTmp0 OUT PRINT @sTmp0 -- Content-Type: multipart/mixed; boundary="------------080303020600020604010008" -- someHeader1: Some value 1 -- someHeader2: Some value 2 -- -- --------------080303020600020604010008 -- hdrA: Some value A -- hdrB: Some value B -- Content-Type: text/plain -- -- This is the plain-text body. -- --------------080303020600020604010008 -- hdrX: Some value X -- hdrY: Some value Y -- Content-Type: text/xml -- -- <a>This is the XML body</a> -- --------------080303020600020604010008-- -- -- Load an RSA-based certificate. -- (Encrypting MIME only requires the public key. Decrypting MIME requires the private 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/rsaes-oaep/cert.pem' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @mime EXEC @hr = sp_OADestroy @part0 EXEC @hr = sp_OADestroy @part1 EXEC @hr = sp_OADestroy @sbMime EXEC @hr = sp_OADestroy @cert RETURN END -- Set the email object properties to indicate the desired encryption. EXEC sp_OASetProperty @mime, 'Pkcs7CryptAlg', 'aes' -- If AES-256 is desired, set the following property to 256. EXEC sp_OASetProperty @mime, 'Pkcs7KeyLength', 128 EXEC sp_OASetProperty @mime, 'OaepPadding', 1 -- Other choices for the OAEP hash algorithm are "sha1", "sha384", and "sha512" EXEC sp_OASetProperty @mime, 'OaepHash', 'sha256' -- Encrypt the MIME (to create S/MIME, which stands for "Secure MIME") EXEC sp_OAMethod @mime, 'Encrypt', @success OUT, @cert IF @success <> 1 BEGIN EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @mime EXEC @hr = sp_OADestroy @part0 EXEC @hr = sp_OADestroy @part1 EXEC @hr = sp_OADestroy @sbMime EXEC @hr = sp_OADestroy @cert RETURN END PRINT '---------------' PRINT 'Encrypted MIME:' PRINT '---------------' EXEC sp_OAMethod @mime, 'GetMime', @sTmp0 OUT PRINT @sTmp0 PRINT 'Success.' -- --------------------------------------------------- -- This is sample output for RSAES-OAEP encrypted MIME: -- --------------------------------------------------- -- Content-Type: application/x-pkcs7-mime; name="smime.p7m"; smime-type="enveloped-data" -- someHeader1: Some value 1 -- someHeader2: Some value 2 -- Content-Disposition: attachment; filename="smime.p7m" -- Content-Transfer-Encoding: base64 -- -- MIIDvAYJKoZIhvcNAQcDoIIDrTCCA6kCAQAxggGgMIIBnAIBADB1MGgxCzAJBgNVBAYTAlVTMQsw -- CQYDVQQIDAJJTDEQMA4GA1UEBwwHV2hlYXRvbjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQ -- dHkgTHRkMRcwFQYDVQQDDA5DaGlsa2F0V2lkZ2V0cwIJAMRwugDmvniwMBwGCSqGSIb3DQEBBzAP -- oA0wCwYJYIZIAWUDBAIBBIIBAFaUL1ga1bOrdqYKcMm+FHUacBvzfBxk0fnPA0AMBdN8BvTWT3CN -- YuqBhjOGyq0FpYD9pVZybUuFMCVsVyIW2O62HnsOK58YaPEUUcdH2sI+yjqX9UAn0P0nVDSsVdeK -- W8x9kMfZg+3UP+y1q+lu7VRJO3f2C9oLQpTkc4VW4n7UOcUI0waykLCjCTL8lFDb7/J3GeuMKyhH -- 5riNz50kpbzqn7m2Ks9yA+QmcTdXclclGFr2vwEUFzSdT2pxh/vaUrogCxkYWbQ2eV7vZg6O4kV0 -- aqHBqySkPTAzHKiHH8K8GVdlTuJ8350CpICa4T8w7/Ht5I7dyOPPKy4C+rVtrvcwggH+BgkqhkiG -- 9w0BBwEwHQYJYIZIAWUDBAECBBDTC2WNBN+z2I47/4Feu9YqgIIB0MZvl3nC/q/Wzil6HtfQTr12 -- Q5moHY+ORzAa1P9XvX2ZUFhW530mV395mQw/A4o4ekmX9eRPEZquYzZPLT8hNeZIuNdhpcSQUmad -- rKnKkR0wKJ3jJ3LhOIohVBQSYs8kVDZKq6lJBIznlsurFelZoNEhyRschhteDZx5rb7fCe8c2+/O -- DHxaqaHCAzm/Bd7kcg6FFfuTZy3tu0PgP5IsXN4OFA3kkvwjAs4XsVS8jdIcmDNBkYieE8WmJOIm -- Mz7mh/CHWLgWfGKa0Dkb9RcbFgLwYNT3GzuXFw9XPbKkEZjEAtJajWbN6P0WQl96YYd9qZxUpGxZ -- zjTHEYzViUdUXolfpLufttrRXyxN1RFWhNFMFbv66xYqklMSgpdM/Mbk+EuvX6eXayDPvDBpfYMw -- NoRAzv1Ony2c0ez9rBemJICicxAzpuvHbRxdjYs63Dnv+TYgpBK12AxWWpPIjXvw0WQKgTC3Tg8s -- EnuGhpENqso/clJBEBSn4+2WhYtYbdI5sVme67lvqQl1Xxy3r18SWaQbyDOwgYi1E+54lMDOxMy0 -- y0FPHk5pP45DnXWj+XORPp5LhuZr5mf62YOXSSUwR5P0cXy4Rc+pN5lhRQPCf5z2 EXEC @hr = sp_OADestroy @mime EXEC @hr = sp_OADestroy @part0 EXEC @hr = sp_OADestroy @part1 EXEC @hr = sp_OADestroy @sbMime EXEC @hr = sp_OADestroy @cert END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.