Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
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
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(DataFlex) 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

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vSb
    Handle hoSb
    Boolean iBCrlf
    Handle hoMime
    Variant vCert
    Handle hoCert
    Variant vPrivKey
    Handle hoPrivKey
    Handle hoMime2
    String sTemp1

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

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Move True To iBCrlf
    Get ComAppendLine Of hoSb 'Content-Type: application/x-pkcs7-mime; name="smime.p7m"; smime-type="enveloped-data"' iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "someHeader1: Some value 1" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "someHeader2: Some value 2" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb 'Content-Disposition: attachment; filename="smime.p7m"' iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "Content-Transfer-Encoding: base64" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "MIIDvAYJKoZIhvcNAQcDoIIDrTCCA6kCAQAxggGgMIIBnAIBADB1MGgxCzAJBgNVBAYTAlVTMQsw" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "CQYDVQQIDAJJTDEQMA4GA1UEBwwHV2hlYXRvbjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQ" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "dHkgTHRkMRcwFQYDVQQDDA5DaGlsa2F0V2lkZ2V0cwIJAMRwugDmvniwMBwGCSqGSIb3DQEBBzAP" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "oA0wCwYJYIZIAWUDBAIBBIIBAFaUL1ga1bOrdqYKcMm+FHUacBvzfBxk0fnPA0AMBdN8BvTWT3CN" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "YuqBhjOGyq0FpYD9pVZybUuFMCVsVyIW2O62HnsOK58YaPEUUcdH2sI+yjqX9UAn0P0nVDSsVdeK" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "W8x9kMfZg+3UP+y1q+lu7VRJO3f2C9oLQpTkc4VW4n7UOcUI0waykLCjCTL8lFDb7/J3GeuMKyhH" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "5riNz50kpbzqn7m2Ks9yA+QmcTdXclclGFr2vwEUFzSdT2pxh/vaUrogCxkYWbQ2eV7vZg6O4kV0" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "aqHBqySkPTAzHKiHH8K8GVdlTuJ8350CpICa4T8w7/Ht5I7dyOPPKy4C+rVtrvcwggH+BgkqhkiG" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "9w0BBwEwHQYJYIZIAWUDBAECBBDTC2WNBN+z2I47/4Feu9YqgIIB0MZvl3nC/q/Wzil6HtfQTr12" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "Q5moHY+ORzAa1P9XvX2ZUFhW530mV395mQw/A4o4ekmX9eRPEZquYzZPLT8hNeZIuNdhpcSQUmad" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "rKnKkR0wKJ3jJ3LhOIohVBQSYs8kVDZKq6lJBIznlsurFelZoNEhyRschhteDZx5rb7fCe8c2+/O" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "DHxaqaHCAzm/Bd7kcg6FFfuTZy3tu0PgP5IsXN4OFA3kkvwjAs4XsVS8jdIcmDNBkYieE8WmJOIm" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "Mz7mh/CHWLgWfGKa0Dkb9RcbFgLwYNT3GzuXFw9XPbKkEZjEAtJajWbN6P0WQl96YYd9qZxUpGxZ" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "zjTHEYzViUdUXolfpLufttrRXyxN1RFWhNFMFbv66xYqklMSgpdM/Mbk+EuvX6eXayDPvDBpfYMw" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "NoRAzv1Ony2c0ez9rBemJICicxAzpuvHbRxdjYs63Dnv+TYgpBK12AxWWpPIjXvw0WQKgTC3Tg8s" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "EnuGhpENqso/clJBEBSn4+2WhYtYbdI5sVme67lvqQl1Xxy3r18SWaQbyDOwgYi1E+54lMDOxMy0" iBCrlf To iSuccess
    Get ComAppendLine Of hoSb "y0FPHk5pP45DnXWj+XORPp5LhuZr5mf62YOXSSUwR5P0cXy4Rc+pN5lhRQPCf5z2" iBCrlf To iSuccess

    //  Load the encrypted MIME into the MIME object.
    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End
    Get pvComObject of hoSb to vSb
    Get ComLoadMimeSb Of hoMime vSb To iSuccess

    //  Provide the required cert + private key from a PFX (.pfx/.p12) file.
    Get ComAddPfxSourceFile Of hoMime "qa_data/rsaes-oaep/cert_plus_privatekey.pfx" "PFX_PASSWORD" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Decrypt.
    Get ComDecrypt Of hoMime To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Examine the decrypted MIME.
    Get ComGetMime Of hoMime To sTemp1
    Showln sTemp1
    Showln "Decrypt test 1 succeeded."

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

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromFile Of hoCert "qa_data/rsaes-oaep/cert.pem" To iSuccess

    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End
    Get ComLoadPemFile Of hoPrivKey "qa_data/rsaes-oaep/privatekey.pem" To iSuccess

    //  Decrypt using the cert + private key
    Get Create (RefClass(cComChilkatMime)) To hoMime2
    If (Not(IsComObjectCreated(hoMime2))) Begin
        Send CreateComObject of hoMime2
    End
    Get pvComObject of hoSb to vSb
    Get ComLoadMimeSb Of hoMime2 vSb To iSuccess

    Get pvComObject of hoCert to vCert
    Get pvComObject of hoPrivKey to vPrivKey
    Get ComDecrypt2 Of hoMime2 vCert vPrivKey To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMime2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Examine the decrypted MIME.
    Get ComGetMime Of hoMime2 To sTemp1
    Showln sTemp1
    Showln "Decrypt test 2 succeeded."


End_Procedure

 

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