Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) Send Encrypted Email using RSAES-OAEP with AES-128 CBC and SHA256

Demonstrates how to send encrypted email using RSAES-OAEP with SHA256 and AES-128 content encryption.

Note: This example requires Chilkat v9.5.0.67 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

// Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
//
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- Load an RSA-based certificate.
    -- (Encrypting an email only requires the public key.  Decrypting an email requires the private key.)
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Cert', @cert OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int
    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 @cert
        RETURN
      END

    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Email', @email OUT

    -- Create a simple email.
    EXEC sp_OASetProperty @email, 'Subject', 'Sample RSAES-OAEP Encrypted Email'
    EXEC sp_OASetProperty @email, 'Body', 'Sample RSAES-OAEP Encrypted Email'
    EXEC sp_OASetProperty @email, 'From', 'support@chilkatsoft.com'

    -- Add a recipient.
    -- (The email is encrypted using the recipient's certificate.
    -- If sending to multiple recipients, then the AddEncryptCert method would
    -- need to be called once for each recipient's certificate.)
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Chilkat GMail', 'chilkat.support@gmail.com'

    -- Set the email object properties to indicate the desired encryption.
    EXEC sp_OASetProperty @email, 'Pkcs7CryptAlg', 'aes'
    -- If AES-256 is desired, set the following property to 256.
    EXEC sp_OASetProperty @email, 'Pkcs7KeyLength', 128
    EXEC sp_OASetProperty @email, 'OaepPadding', 1
    -- Other choices for the OAEP hash algorithm are "sha1", "sha384", and "sha512"
    EXEC sp_OASetProperty @email, 'OaepHash', 'sha256'

    -- Provide the certificate to be used for encryption
    EXEC sp_OAMethod @email, 'AddEncryptCert', @success OUT, @cert

    -- Don't forget to indicate that the email should be encrypted when sent..
    EXEC sp_OASetProperty @email, 'SendEncrypted', 1

    -- The mailman object will do the RSAES-OAEP encryption when sending.
    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.MailMan', @mailman OUT

    -- Set the SMTP settings for your email account on your mail server.
    EXEC sp_OASetProperty @mailman, 'SmtpUsername', 'SMTP_LOGIN'
    EXEC sp_OASetProperty @mailman, 'SmtpPassword', 'SMTP_PASSWORD'
    EXEC sp_OASetProperty @mailman, 'SmtpHost', 'MY_SMTP_DOMAIN_OR_IP'
    EXEC sp_OASetProperty @mailman, 'SmtpPort', 587
    EXEC sp_OASetProperty @mailman, 'StartTLS', 1

    -- Send the email.  The mailman will encrypt the email as directed by the 
    -- property settings of the email object.
    EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END

    EXEC sp_OAMethod @mailman, 'CloseSmtpConnection', @success OUT
    IF @success <> 1
      BEGIN

        PRINT 'Connection to SMTP server not closed cleanly.'
      END


    PRINT 'Mail Sent!'

    -- -----------------------------------------------------------
    -- This is an example of an RSAES-OAEP encrypted email
    -- -----------------------------------------------------------

    -- MIME-Version: 1.0
    -- Date: Thu, 27 Apr 2017 08:43:32 -0500
    -- Message-ID: <772DC039F0259C474BAC60240EA2BA2272402308@CHILKAT13>
    -- Content-Type: application/pkcs7-mime; name="smime.p7m"; smime-type="enveloped-data"
    -- Content-Transfer-Encoding: base64
    -- X-Priority: 3 (Normal)
    -- Subject: Sample RSAES-OAEP Encrypted Email
    -- From: support@chilkatsoft.com
    -- To: "Chilkat GMail" <chilkat.support@gmail.com>
    -- Content-Disposition: attachment; filename="smime.p7m"
    -- 
    -- MIICWQYJKoZIhvcNAQcDoIICSjCCAkYCAQAxggGgMIIBnAIBADB1MGgxCzAJBgNVBAYTAlVTMQsw
    -- CQYDVQQIDAJJTDEQMA4GA1UEBwwHV2hlYXRvbjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQ
    -- dHkgTHRkMRcwFQYDVQQDDA5DaGlsa2F0V2lkZ2V0cwIJAMRwugDmvniwMBwGCSqGSIb3DQEBBzAP
    -- oA0wCwYJYIZIAWUDBAIBBIIBABr0E1dKJjK0FAYRz/NCtJmBeB0nUxgcuBnhGPI/UeyGub3cCo+K
    -- G5F4/iVTBJqVvYIF5+fvBnyTginwv7OiUiFWLyihiFC3NIyZJO22+XMHpNatCffTPZk10WswkTgk
    -- G3ApnRvGQAaldnFD0Hs8drPU4vBvY9QsjT7YDGa6u2NMX+sr1ewEZArqU0mNfJ6RsEYd5FQbFEVF
    -- qLmnz8Dt+yhoJlUtfUd8TXIeqHRJ7RxKOTSzlBZaAdTv2QX4oL9IcAgZeTg5iw+yRPkSAwWyg+I/
    -- 7fybLsUpRGDHTGUU+AvHvP0kYKa1mkvccBVEC/+4hEyhpS1tWIR5ByY6vM76Z+8wgZwGCSqGSIb3
    -- DQEHATAdBglghkgBZQMEAQIEEKqD0YDHX1NsVDaV32UczpeAcLteQyRPTV4hATjwcPiVelPfeWNs
    -- xZKRGaEBqLM8+Y+V4ciCFoOlgJuOcP4m1PTHyilfzd+SCsKz5l1C7+sfPf36n2aacX6IWga59Bz5
    -- QbWrOHDUT7O5PnGwKVgQFw3Cj4GrdPGWKcoqxB0HuKnj3WA=

    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @mailman


END
GO

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.