Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

Encrypt with RSA-OAEP and AES-128-CBC

See more Email Object Examples

Use this profile when the receiver requires AES-128-CBC content encryption and RSA-OAEP with SHA-256. The SHA-256 settings apply to OAEP and its mask generation function. Use the matching decryption example to inspect the result.

Chilkat SQL Server Downloads

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

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.

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

    EXEC sp_OAMethod @email, 'LoadEml', @success OUT, 'message.eml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END
    --  Use the recipient's public encryption certificate. Its private key stays with the recipient.
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'recipient.cer'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END
    EXEC sp_OAMethod @email, 'SetEncryptCert', @success OUT, @cert
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END
    EXEC sp_OASetProperty @email, 'Pkcs7CryptAlg', 'aes'
    EXEC sp_OASetProperty @email, 'Pkcs7KeyLength', 128
    EXEC sp_OASetProperty @email, 'OaepPadding', 1
    EXEC sp_OASetProperty @email, 'OaepHash', 'sha256'
    EXEC sp_OASetProperty @email, 'OaepMgfHash', 'sha256'
    EXEC sp_OASetProperty @email, 'SendEncrypted', 1
    DECLARE @mail int
    EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mail OUT

    DECLARE @rendered int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @rendered OUT

    EXEC sp_OAMethod @mail, 'RenderToMimeBd', @success OUT, @email, @rendered
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @mail, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @mail
        EXEC @hr = sp_OADestroy @rendered
        RETURN
      END
    EXEC sp_OAMethod @rendered, 'WriteFile', @success OUT, 'oaep-encrypted.eml'
    IF @success <> 1
      BEGIN

        PRINT 'Failed: rendered.WriteFile.'
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @mail
        EXEC @hr = sp_OADestroy @rendered
        RETURN
      END

    PRINT 'Saved oaep-encrypted.eml.'

    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @mail
    EXEC @hr = sp_OADestroy @rendered


END
GO