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
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
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) Decrypt MIME using RSAES-OAEP with SHA256 and AES-128 content encryption

Demonstrates how to decrypt MIME what was encrypted with RSAES-OAEP with SHA256 and AES-128 content encryption.

In actuality, we don't really need to know in advance the various algorithms used to encrypt the MIME. When decrypting, the information about the algorithms required are embedded in the PCKS7 message itself, and this tells a decryptor what it should use.

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
    -- 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.

    -- We begin with this 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

    DECLARE @success int

    DECLARE @sb int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @bCrlf int
    SELECT @bCrlf = 1
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'Content-Type: application/x-pkcs7-mime; name="smime.p7m"; smime-type="enveloped-data"', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'someHeader1: Some value 1', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'someHeader2: Some value 2', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'Content-Disposition: attachment; filename="smime.p7m"', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'Content-Transfer-Encoding: base64', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, '', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'MIIDvAYJKoZIhvcNAQcDoIIDrTCCA6kCAQAxggGgMIIBnAIBADB1MGgxCzAJBgNVBAYTAlVTMQsw', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'CQYDVQQIDAJJTDEQMA4GA1UEBwwHV2hlYXRvbjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQ', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'dHkgTHRkMRcwFQYDVQQDDA5DaGlsa2F0V2lkZ2V0cwIJAMRwugDmvniwMBwGCSqGSIb3DQEBBzAP', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'oA0wCwYJYIZIAWUDBAIBBIIBAFaUL1ga1bOrdqYKcMm+FHUacBvzfBxk0fnPA0AMBdN8BvTWT3CN', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'YuqBhjOGyq0FpYD9pVZybUuFMCVsVyIW2O62HnsOK58YaPEUUcdH2sI+yjqX9UAn0P0nVDSsVdeK', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'W8x9kMfZg+3UP+y1q+lu7VRJO3f2C9oLQpTkc4VW4n7UOcUI0waykLCjCTL8lFDb7/J3GeuMKyhH', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, '5riNz50kpbzqn7m2Ks9yA+QmcTdXclclGFr2vwEUFzSdT2pxh/vaUrogCxkYWbQ2eV7vZg6O4kV0', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'aqHBqySkPTAzHKiHH8K8GVdlTuJ8350CpICa4T8w7/Ht5I7dyOPPKy4C+rVtrvcwggH+BgkqhkiG', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, '9w0BBwEwHQYJYIZIAWUDBAECBBDTC2WNBN+z2I47/4Feu9YqgIIB0MZvl3nC/q/Wzil6HtfQTr12', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'Q5moHY+ORzAa1P9XvX2ZUFhW530mV395mQw/A4o4ekmX9eRPEZquYzZPLT8hNeZIuNdhpcSQUmad', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'rKnKkR0wKJ3jJ3LhOIohVBQSYs8kVDZKq6lJBIznlsurFelZoNEhyRschhteDZx5rb7fCe8c2+/O', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'DHxaqaHCAzm/Bd7kcg6FFfuTZy3tu0PgP5IsXN4OFA3kkvwjAs4XsVS8jdIcmDNBkYieE8WmJOIm', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'Mz7mh/CHWLgWfGKa0Dkb9RcbFgLwYNT3GzuXFw9XPbKkEZjEAtJajWbN6P0WQl96YYd9qZxUpGxZ', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'zjTHEYzViUdUXolfpLufttrRXyxN1RFWhNFMFbv66xYqklMSgpdM/Mbk+EuvX6eXayDPvDBpfYMw', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'NoRAzv1Ony2c0ez9rBemJICicxAzpuvHbRxdjYs63Dnv+TYgpBK12AxWWpPIjXvw0WQKgTC3Tg8s', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'EnuGhpENqso/clJBEBSn4+2WhYtYbdI5sVme67lvqQl1Xxy3r18SWaQbyDOwgYi1E+54lMDOxMy0', @bCrlf
    EXEC sp_OAMethod @sb, 'AppendLine', @success OUT, 'y0FPHk5pP45DnXWj+XORPp5LhuZr5mf62YOXSSUwR5P0cXy4Rc+pN5lhRQPCf5z2', @bCrlf

    -- Load the encrypted MIME into the MIME object.
    DECLARE @mime int
    -- Use "Chilkat_9_5_0.Mime" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT

    EXEC sp_OAMethod @mime, 'LoadMimeSb', @success OUT, @sb

    -- Provide the required cert + private key from a PFX (.pfx/.p12) file.
    EXEC sp_OAMethod @mime, 'AddPfxSourceFile', @success OUT, 'qa_data/rsaes-oaep/cert_plus_privatekey.pfx', 'PFX_PASSWORD'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sb
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    -- Decrypt.
    EXEC sp_OAMethod @mime, 'Decrypt', @success OUT
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sb
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    -- Examine the decrypted MIME.
    EXEC sp_OAMethod @mime, 'GetMime', @sTmp0 OUT
    PRINT @sTmp0

    PRINT 'Decrypt test 1 succeeded.'

    -- -----------------------------------------------------------
    -- Now let's decrypt another way..
    -- We have the same cert and private key in PEM files..

    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'

    DECLARE @privKey int
    -- Use "Chilkat_9_5_0.PrivateKey" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT

    EXEC sp_OAMethod @privKey, 'LoadPemFile', @success OUT, 'qa_data/rsaes-oaep/privatekey.pem'

    -- Decrypt using the cert + private key
    DECLARE @mime2 int
    -- Use "Chilkat_9_5_0.Mime" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime2 OUT

    EXEC sp_OAMethod @mime2, 'LoadMimeSb', @success OUT, @sb

    EXEC sp_OAMethod @mime2, 'Decrypt2', @success OUT, @cert, @privKey
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @mime2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sb
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @privKey
        EXEC @hr = sp_OADestroy @mime2
        RETURN
      END

    -- Examine the decrypted MIME.
    EXEC sp_OAMethod @mime2, 'GetMime', @sTmp0 OUT
    PRINT @sTmp0

    PRINT 'Decrypt test 2 succeeded.'

    EXEC @hr = sp_OADestroy @sb
    EXEC @hr = sp_OADestroy @mime
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @privKey
    EXEC @hr = sp_OADestroy @mime2


END
GO

 

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