Delphi ActiveX Requires Chilkat v11.8.0+
Delphi ActiveX
Add PEM Text as a Decryption Source
See more MIME Examples
Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.
PEM text held in memory is loaded into a Pem object with LoadPem, and AddPemSource registers its certificates and private keys as a source for later decryption.
The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background. This is the in-memory counterpart of loading a PEM file with LoadPemFile, for cases where the PEM text is already in memory rather than residing in a file.
AddPemSource was added in Chilkat v11.8.0 and replaces UseCertVault and the AddPfxSource methods.Chilkat Delphi ActiveX Downloads
var
success: Integer;
mime: TChilkatMime;
sbPem: TChilkatStringBuilder;
pemPassword: WideString;
pem: TChilkatPem;
begin
success := 0;
mime := TChilkatMime.Create(Self);
success := mime.LoadMimeFile('qa_data/encrypted.eml');
if (success = 0) then
begin
Memo1.Lines.Add(mime.LastErrorText);
Exit;
end;
// PEM text (a certificate and its private key) that is held in memory. Here it is loaded into a
// StringBuilder from a file.
sbPem := TChilkatStringBuilder.Create(Self);
success := sbPem.LoadFile('qa_data/recipient.pem','utf-8');
if (success = 0) then
begin
Memo1.Lines.Add(sbPem.LastErrorText);
Exit;
end;
// Load the PEM text into a Pem object. The 2nd argument of LoadPem is the password of the private
// key (from a secure source).
pemPassword := 'myPemPassword';
pem := TChilkatPem.Create(Self);
success := pem.LoadPem(sbPem.GetAsString(),pemPassword);
if (success = 0) then
begin
Memo1.Lines.Add(pem.LastErrorText);
Exit;
end;
// Add the Pem object as a source of certificates and private keys for later decryption.
success := mime.AddPemSource(pem.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(mime.LastErrorText);
Exit;
end;
success := mime.Decrypt();
if (success = 0) then
begin
Memo1.Lines.Add(mime.LastErrorText);
Exit;
end;
Memo1.Lines.Add('Decrypted.');