Sample code for 30+ languages & platforms
Java

Extract PKCS7 from MIME and Decrypt

See more MIME Examples

Extracts the base64-encoded PKCS7 body of a MIME message to a file, and then decrypts using Chilkat Crypt2.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.Mime;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Mime mime = new Mime();

        try {
            mime.loadMimeFile("c:/aaworkarea/EmailInBytes.txt");

            mime.saveBody("c:/aaworkarea/smime.p7m");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Crypt2 crypt = new Crypt2();

        try {
            crypt.addPfxSourceFile("c:/aaworkarea/my.pfx", "pfxPassword");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Indicate the public-key (PKCS7) encryption/decryption should be used:
        crypt.setCryptAlgorithm("pki");

        String inPath = "c:/aaworkarea/smime.p7m";
        String outPath = "c:/aaworkarea/decrypted.dat";

        try {
            crypt.ckDecryptFile(inPath, outPath);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Success.");
    }
}