![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Android™) Decrypt PKCS7 (CMS) EnvelopedDataThis example receives a PKCS7 (CMS) EnvelopedData message that was encrypted to me. To decrypt it:
Summary:
Only the intended recipient, who possesses the correct private key, can successfully decrypt the EnvelopedData. Note: This example requires Chilkat v11.0.0 or greater.
// Important: Don't forget to include the call to System.loadLibrary // as shown at the bottom of this code sample. package com.test; import android.app.Activity; import com.chilkatsoft.*; import android.widget.TextView; import android.os.Bundle; public class SimpleActivity extends Activity { private static final String TAG = "Chilkat"; // Called when the activity is first created. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Here's the PKCS7 (CMS) EnvelopedData we received. // It was created using this example: Create PKCS7 CMS EnvelopedData // MIICSgYJKoZIhvcNAQcDoIICOzCCAjcCAQAxggHiMIIB3gIBADCBljCBgTELMAkGA1UEBhMCSVQx // EDAOBgNVBAgMB0JlcmdhbW8xGTAXBgNVBAcMEFBvbnRlIFNhbiBQaWV0cm8xFzAVBgNVBAoMDkFj // dGFsaXMgUy5wLkEuMSwwKgYDVQQDDCNBY3RhbGlzIENsaWVudCBBdXRoZW50aWNhdGlvbiBDQSBH // MwIQPCWvkSv8oQ7xRmEHJ6TzEDA8BgkqhkiG9w0BAQcwL6APMA0GCWCGSAFlAwQCAQUAoRwwGgYJ // KoZIhvcNAQEIMA0GCWCGSAFlAwQCAQUABIIBAKqHAPQNSsQoX7B2NH7QyEOWQRsSVs8oCHXmy8f4 // MVZD2er3bvYUCIomxpwbLEAl14qjUIMynahooYGgqip7+4FqL301G+BVjZVfEhHWj+VI1dAWnWuL // VHlvc/pbQNBWqV8rKVJsNIsuAZkdj4WSwLVKxYkYX43B8fh/g71XN2DTJu7Z/824v48KBmgpQBOT // 2q7IcDGxNPAFN2p6eavIVGn2LvhEbf/Fszyj+GR5tMcnQP1BOLJ3s3JzUBbvj8hcZrF1Vhl9HnTU // YQx8G/KdW1mR+Wlhl3BWoK0LYKRTbnTx2BXOs0CY1SXOAdhKr01ZYjA+xW4nGzY0lfXS9QZjh9gw // TAYJKoZIhvcNAQcBMB0GCWCGSAFlAwQBKgQQw0xTbfmnt0zjWHo5SaQIp4AgxTVY9E/Ncqy6t+RM // 8y4c3Av62/wB8IpPUEmtM2OeuZo= CkStringBuilder sb = new CkStringBuilder(); boolean bCrlf = true; sb.AppendLine("MIICSgYJKoZIhvcNAQcDoIICOzCCAjcCAQAxggHiMIIB3gIBADCBljCBgTELMAkGA1UEBhMCSVQx",bCrlf); sb.AppendLine("EDAOBgNVBAgMB0JlcmdhbW8xGTAXBgNVBAcMEFBvbnRlIFNhbiBQaWV0cm8xFzAVBgNVBAoMDkFj",bCrlf); sb.AppendLine("dGFsaXMgUy5wLkEuMSwwKgYDVQQDDCNBY3RhbGlzIENsaWVudCBBdXRoZW50aWNhdGlvbiBDQSBH",bCrlf); sb.AppendLine("MwIQPCWvkSv8oQ7xRmEHJ6TzEDA8BgkqhkiG9w0BAQcwL6APMA0GCWCGSAFlAwQCAQUAoRwwGgYJ",bCrlf); sb.AppendLine("KoZIhvcNAQEIMA0GCWCGSAFlAwQCAQUABIIBAKqHAPQNSsQoX7B2NH7QyEOWQRsSVs8oCHXmy8f4",bCrlf); sb.AppendLine("MVZD2er3bvYUCIomxpwbLEAl14qjUIMynahooYGgqip7+4FqL301G+BVjZVfEhHWj+VI1dAWnWuL",bCrlf); sb.AppendLine("VHlvc/pbQNBWqV8rKVJsNIsuAZkdj4WSwLVKxYkYX43B8fh/g71XN2DTJu7Z/824v48KBmgpQBOT",bCrlf); sb.AppendLine("2q7IcDGxNPAFN2p6eavIVGn2LvhEbf/Fszyj+GR5tMcnQP1BOLJ3s3JzUBbvj8hcZrF1Vhl9HnTU",bCrlf); sb.AppendLine("YQx8G/KdW1mR+Wlhl3BWoK0LYKRTbnTx2BXOs0CY1SXOAdhKr01ZYjA+xW4nGzY0lfXS9QZjh9gw",bCrlf); sb.AppendLine("TAYJKoZIhvcNAQcBMB0GCWCGSAFlAwQBKgQQw0xTbfmnt0zjWHo5SaQIp4AgxTVY9E/Ncqy6t+RM",bCrlf); sb.AppendLine("8y4c3Av62/wB8IpPUEmtM2OeuZo=",bCrlf); CkCrypt2 crypt = new CkCrypt2(); crypt.put_CryptAlgorithm("pki"); // The CMS message contains information about the digital certificate that was used to encrypt. // Assuming the certificate is installed in a Windows certificate store, or macOS keychain, // Chilkat will automatically locate and use the certificate + private key for decryption. crypt.put_EncodingMode("base64"); crypt.put_Charset("utf-8"); String decryptedText = crypt.decryptStringENC(sb.getAsString()); if (crypt.get_LastMethodSuccess() == false) { Log.i(TAG, crypt.lastErrorText()); return; } Log.i(TAG, decryptedText); // We can get the digital certificate used for decryption by calling LastDecryptCert CkCert cert = new CkCert(); boolean success = crypt.LastDecryptCert(cert); if (crypt.get_LastMethodSuccess() == false) { Log.i(TAG, crypt.lastErrorText()); return; } Log.i(TAG, "Cert used to decrypt: " + cert.subjectCN()); } static { System.loadLibrary("chilkat"); // Note: If the incorrect library name is passed to System.loadLibrary, // then you will see the following error message at application startup: //"The application <your-application-name> has stopped unexpectedly. Please try again." } } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.