Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.8.0+

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
mime: HCkMime;
crypt: HCkCrypt2;
pem: HCkPem;
inPath: PWideChar;
outPath: PWideChar;

begin
success := False;

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

mime := CkMime_Create();

success := CkMime_LoadMimeFile(mime,'c:/aaworkarea/EmailInBytes.txt');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMime__lastErrorText(mime));
    Exit;
  end;

success := CkMime_SaveBody(mime,'c:/aaworkarea/smime.p7m');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMime__lastErrorText(mime));
    Exit;
  end;

crypt := CkCrypt2_Create();

//  The certificate and its private key are in a PEM file.  The 2nd argument of LoadPemFile is the
//  password of the private key.  (Pass an empty string if the private key is not encrypted.)
pem := CkPem_Create();
success := CkPem_LoadPemFile(pem,'c:/aaworkarea/my.pem','pemPassword');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPem__lastErrorText(pem));
    Exit;
  end;
success := CkCrypt2_AddPemSource(crypt,pem);
if (success = False) then
  begin
    Memo1.Lines.Add(CkCrypt2__lastErrorText(crypt));
    Exit;
  end;

//  Indicate the public-key (PKCS7) encryption/decryption should be used:
CkCrypt2_putCryptAlgorithm(crypt,'pki');

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

success := CkCrypt2_CkDecryptFile(crypt,inPath,outPath);
if (success = False) then
  begin
    Memo1.Lines.Add(CkCrypt2__lastErrorText(crypt));
    Exit;
  end;

Memo1.Lines.Add('Success.');

CkMime_Dispose(mime);
CkCrypt2_Dispose(crypt);
CkPem_Dispose(pem);