Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Decrypt MIME using RSAES-OAEP with SHA256 and AES-128 content encryptionDemonstrates 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.
IncludeFile "CkCert.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkMime.pb" IncludeFile "CkPrivateKey.pb" Procedure ChilkatExample() ; 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 success.i sb.i = CkStringBuilder::ckCreate() If sb.i = 0 Debug "Failed to create object." ProcedureReturn EndIf bCrlf.i = 1 CkStringBuilder::ckAppendLine(sb,"Content-Type: application/x-pkcs7-mime; name=" + Chr(34) + "smime.p7m" + Chr(34) + "; smime-type=" + Chr(34) + "enveloped-data" + Chr(34),bCrlf) CkStringBuilder::ckAppendLine(sb,"someHeader1: Some value 1",bCrlf) CkStringBuilder::ckAppendLine(sb,"someHeader2: Some value 2",bCrlf) CkStringBuilder::ckAppendLine(sb,"Content-Disposition: attachment; filename=" + Chr(34) + "smime.p7m" + Chr(34),bCrlf) CkStringBuilder::ckAppendLine(sb,"Content-Transfer-Encoding: base64",bCrlf) CkStringBuilder::ckAppendLine(sb,"",bCrlf) CkStringBuilder::ckAppendLine(sb,"MIIDvAYJKoZIhvcNAQcDoIIDrTCCA6kCAQAxggGgMIIBnAIBADB1MGgxCzAJBgNVBAYTAlVTMQsw",bCrlf) CkStringBuilder::ckAppendLine(sb,"CQYDVQQIDAJJTDEQMA4GA1UEBwwHV2hlYXRvbjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQ",bCrlf) CkStringBuilder::ckAppendLine(sb,"dHkgTHRkMRcwFQYDVQQDDA5DaGlsa2F0V2lkZ2V0cwIJAMRwugDmvniwMBwGCSqGSIb3DQEBBzAP",bCrlf) CkStringBuilder::ckAppendLine(sb,"oA0wCwYJYIZIAWUDBAIBBIIBAFaUL1ga1bOrdqYKcMm+FHUacBvzfBxk0fnPA0AMBdN8BvTWT3CN",bCrlf) CkStringBuilder::ckAppendLine(sb,"YuqBhjOGyq0FpYD9pVZybUuFMCVsVyIW2O62HnsOK58YaPEUUcdH2sI+yjqX9UAn0P0nVDSsVdeK",bCrlf) CkStringBuilder::ckAppendLine(sb,"W8x9kMfZg+3UP+y1q+lu7VRJO3f2C9oLQpTkc4VW4n7UOcUI0waykLCjCTL8lFDb7/J3GeuMKyhH",bCrlf) CkStringBuilder::ckAppendLine(sb,"5riNz50kpbzqn7m2Ks9yA+QmcTdXclclGFr2vwEUFzSdT2pxh/vaUrogCxkYWbQ2eV7vZg6O4kV0",bCrlf) CkStringBuilder::ckAppendLine(sb,"aqHBqySkPTAzHKiHH8K8GVdlTuJ8350CpICa4T8w7/Ht5I7dyOPPKy4C+rVtrvcwggH+BgkqhkiG",bCrlf) CkStringBuilder::ckAppendLine(sb,"9w0BBwEwHQYJYIZIAWUDBAECBBDTC2WNBN+z2I47/4Feu9YqgIIB0MZvl3nC/q/Wzil6HtfQTr12",bCrlf) CkStringBuilder::ckAppendLine(sb,"Q5moHY+ORzAa1P9XvX2ZUFhW530mV395mQw/A4o4ekmX9eRPEZquYzZPLT8hNeZIuNdhpcSQUmad",bCrlf) CkStringBuilder::ckAppendLine(sb,"rKnKkR0wKJ3jJ3LhOIohVBQSYs8kVDZKq6lJBIznlsurFelZoNEhyRschhteDZx5rb7fCe8c2+/O",bCrlf) CkStringBuilder::ckAppendLine(sb,"DHxaqaHCAzm/Bd7kcg6FFfuTZy3tu0PgP5IsXN4OFA3kkvwjAs4XsVS8jdIcmDNBkYieE8WmJOIm",bCrlf) CkStringBuilder::ckAppendLine(sb,"Mz7mh/CHWLgWfGKa0Dkb9RcbFgLwYNT3GzuXFw9XPbKkEZjEAtJajWbN6P0WQl96YYd9qZxUpGxZ",bCrlf) CkStringBuilder::ckAppendLine(sb,"zjTHEYzViUdUXolfpLufttrRXyxN1RFWhNFMFbv66xYqklMSgpdM/Mbk+EuvX6eXayDPvDBpfYMw",bCrlf) CkStringBuilder::ckAppendLine(sb,"NoRAzv1Ony2c0ez9rBemJICicxAzpuvHbRxdjYs63Dnv+TYgpBK12AxWWpPIjXvw0WQKgTC3Tg8s",bCrlf) CkStringBuilder::ckAppendLine(sb,"EnuGhpENqso/clJBEBSn4+2WhYtYbdI5sVme67lvqQl1Xxy3r18SWaQbyDOwgYi1E+54lMDOxMy0",bCrlf) CkStringBuilder::ckAppendLine(sb,"y0FPHk5pP45DnXWj+XORPp5LhuZr5mf62YOXSSUwR5P0cXy4Rc+pN5lhRQPCf5z2",bCrlf) ; Load the encrypted MIME into the MIME object. mime.i = CkMime::ckCreate() If mime.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkMime::ckLoadMimeSb(mime,sb) ; Provide the required cert + private key from a PFX (.pfx/.p12) file. success = CkMime::ckAddPfxSourceFile(mime,"qa_data/rsaes-oaep/cert_plus_privatekey.pfx","PFX_PASSWORD") If success <> 1 Debug CkMime::ckLastErrorText(mime) CkStringBuilder::ckDispose(sb) CkMime::ckDispose(mime) ProcedureReturn EndIf ; Decrypt. success = CkMime::ckDecrypt(mime) If success <> 1 Debug CkMime::ckLastErrorText(mime) CkStringBuilder::ckDispose(sb) CkMime::ckDispose(mime) ProcedureReturn EndIf ; Examine the decrypted MIME. Debug CkMime::ckGetMime(mime) Debug "Decrypt test 1 succeeded." ; ----------------------------------------------------------- ; Now let's decrypt another way.. ; We have the same cert and private key in PEM files.. cert.i = CkCert::ckCreate() If cert.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkCert::ckLoadFromFile(cert,"qa_data/rsaes-oaep/cert.pem") privKey.i = CkPrivateKey::ckCreate() If privKey.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkPrivateKey::ckLoadPemFile(privKey,"qa_data/rsaes-oaep/privatekey.pem") ; Decrypt using the cert + private key mime2.i = CkMime::ckCreate() If mime2.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkMime::ckLoadMimeSb(mime2,sb) success = CkMime::ckDecrypt2(mime2,cert,privKey) If success <> 1 Debug CkMime::ckLastErrorText(mime2) CkStringBuilder::ckDispose(sb) CkMime::ckDispose(mime) CkCert::ckDispose(cert) CkPrivateKey::ckDispose(privKey) CkMime::ckDispose(mime2) ProcedureReturn EndIf ; Examine the decrypted MIME. Debug CkMime::ckGetMime(mime2) Debug "Decrypt test 2 succeeded." CkStringBuilder::ckDispose(sb) CkMime::ckDispose(mime) CkCert::ckDispose(cert) CkPrivateKey::ckDispose(privKey) CkMime::ckDispose(mime2) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.