SQL Server
SQL Server
Create PKCS7 (CMS) EnvelopedData
Encrypt some data to a recipient by creating a PKCS7 (CMS) EnvelopedData structure. The data will be encrypted using a symmetric content-encryption algorithm (e.g., AES), and the randomly generated symmetric key will be encrypted using the recipient’s RSA public key extracted from their X.509 certificate.Chilkat SQL Server Downloads
-- 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)
DECLARE @success int
SELECT @success = 0
DECLARE @crypt int
EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Specify the encryption to be used.
-- "pki" indicates "Public Key Infrastructure" and will create a PKCS7 encrypted (enveloped-data) message.
EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'pki'
EXEC sp_OASetProperty @crypt, 'Pkcs7CryptAlg', 'aes'
EXEC sp_OASetProperty @crypt, 'KeyLength', 256
EXEC sp_OASetProperty @crypt, 'OaepHash', 'sha256'
EXEC sp_OASetProperty @crypt, 'OaepPadding', 1
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
-- Use a certificate found in the Windows certificate store.
EXEC sp_OAMethod @cert, 'LoadByCommonName', @success OUT, 'My Certificate'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- Tell the crypt object to use the certificate.
EXEC sp_OAMethod @crypt, 'SetEncryptCert', @success OUT, @cert
DECLARE @toBeEncrypted nvarchar(4000)
SELECT @toBeEncrypted = 'This string is to be encrypted.'
-- Get the result in multi-line BASE64 MIME format.
EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64_mime'
EXEC sp_OASetProperty @crypt, 'Charset', 'utf-8'
DECLARE @result nvarchar(4000)
EXEC sp_OAMethod @crypt, 'EncryptStringENC', @result OUT, @toBeEncrypted
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- -------------------------------------------------------------------------
-- See the following example to decrypt what was created in this example
-- Decrypt PKCS7 (CMS) EnvelopedData
-- -------------------------------------------------------------------------
PRINT @result
-- Sample output:
-- MIICSgYJKoZIhvcNAQcDoIICOzCCAjcCAQAxggHiMIIB3gIBADCBljCBgTELMAkGA1UEBhMCSVQx
-- EDAOBgNVBAgMB0JlcmdhbW8xGTAXBgNVBAcMEFBvbnRlIFNhbiBQaWV0cm8xFzAVBgNVBAoMDkFj
-- dGFsaXMgUy5wLkEuMSwwKgYDVQQDDCNBY3RhbGlzIENsaWVudCBBdXRoZW50aWNhdGlvbiBDQSBH
-- MwIQPCWvkSv8oQ7xRmEHJ6TzEDA8BgkqhkiG9w0BAQcwL6APMA0GCWCGSAFlAwQCAQUAoRwwGgYJ
-- KoZIhvcNAQEIMA0GCWCGSAFlAwQCAQUABIIBAKqHAPQNSsQoX7B2NH7QyEOWQRsSVs8oCHXmy8f4
-- MVZD2er3bvYUCIomxpwbLEAl14qjUIMynahooYGgqip7+4FqL301G+BVjZVfEhHWj+VI1dAWnWuL
-- VHlvc/pbQNBWqV8rKVJsNIsuAZkdj4WSwLVKxYkYX43B8fh/g71XN2DTJu7Z/824v48KBmgpQBOT
-- 2q7IcDGxNPAFN2p6eavIVGn2LvhEbf/Fszyj+GR5tMcnQP1BOLJ3s3JzUBbvj8hcZrF1Vhl9HnTU
-- YQx8G/KdW1mR+Wlhl3BWoK0LYKRTbnTx2BXOs0CY1SXOAdhKr01ZYjA+xW4nGzY0lfXS9QZjh9gw
-- TAYJKoZIhvcNAQcBMB0GCWCGSAFlAwQBKgQQw0xTbfmnt0zjWHo5SaQIp4AgxTVY9E/Ncqy6t+RM
-- 8y4c3Av62/wB8IpPUEmtM2OeuZo=
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @cert
END
GO