Sample code for 30+ languages & platforms
SQL Server

MIME S/MIME Encryption Algorithm Properties

See more MIME Examples

Demonstrates the properties that control S/MIME encryption: Pkcs7CryptAlg (the symmetric content-encryption algorithm), Pkcs7KeyLength (the content-encryption key length in bits), OaepPadding (whether RSAES-OAEP key transport is used instead of RSAES-PKCS1-v1_5), and the OAEP hash settings OaepHash and OaepMgfHash. The properties are configured before calling Encrypt.

Background. CMS/PKCS #7 encryption uses a symmetric algorithm to protect the content and an RSA key-transport scheme to protect the symmetric key. The defaults (aes, 128-bit, PKCS1-v1_5 padding) work broadly; these properties tune the algorithms and padding.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    EXEC sp_OAMethod @mime, 'SetBodyFromPlainText', @success OUT, 'This message will be encrypted.'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    --  Pkcs7CryptAlg is the symmetric content-encryption algorithm.  Supported values are aes, aes-gcm,
    --  des, 3des, and rc2.  The default is aes.
    EXEC sp_OASetProperty @mime, 'Pkcs7CryptAlg', 'aes'

    --  Pkcs7KeyLength is the content-encryption key length in bits.  The default is 128.
    EXEC sp_OASetProperty @mime, 'Pkcs7KeyLength', 256

    --  OaepPadding selects the RSA key-transport padding.  The default is 0 (RSAES-PKCS1-v1_5).
    --  Set 1 to use RSAES-OAEP, in which case the OAEP hash settings apply.
    EXEC sp_OASetProperty @mime, 'OaepPadding', 1
    EXEC sp_OASetProperty @mime, 'OaepHash', 'sha256'
    EXEC sp_OASetProperty @mime, 'OaepMgfHash', 'sha256'

    --  Load the recipient certificate (public key is sufficient for encrypting).
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'qa_data/recipient.cer'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    --  Encrypt using the algorithm settings configured above.
    EXEC sp_OAMethod @mime, 'Encrypt', @success OUT, @cert
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END


    EXEC sp_OAGetProperty @mime, 'Pkcs7CryptAlg', @sTmp0 OUT

    EXEC sp_OAGetProperty @mime, 'Pkcs7KeyLength', @iTmp0 OUT

    PRINT 'Encrypted with ' + @sTmp0 + ' ' + @iTmp0 + '-bit key.'

    EXEC @hr = sp_OADestroy @mime
    EXEC @hr = sp_OADestroy @cert


END
GO