Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
S/MIME Encrypt using Certificate in Apple Keychain
See more Apple Keychain Examples
Encrypts MIME to creates S/MIME using a certificate found in the Apple Keychain.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Cert,
Chilkat.Mime;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
cert: TCert;
mime: TMime;
begin
success := False;
cert := TCert.Create;
// Load the certificate by the Subject Common Name
// On MacOS and iOS, Chilkat will search the Keychain(s) for the matching certificate.
success := cert.LoadByCommonName('My Cert');
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
// Note: The private key is not needed for encryption.
// Only the certificate is needed.
// Create a simple MIME message to encrypt.
mime := TMime.Create;
mime.AddHeaderField('Subject','test');
mime.AddHeaderField('SomeHeader','123');
mime.AddHeaderField('Content-Type','text/plain');
mime.SetBody('This is the body.');
WriteLn(mime.GetMime());
WriteLn('----');
// Here's the MIME to be encrypted:
// Subject: test
// SomeHeader: 123
// Content-Type: text/plain
//
// This is the body.
// -------------------------------------
// Encrypt the MIME.
success := mime.Encrypt(cert);
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
// Show the encrypted MIME (S/MIME)
WriteLn(mime.GetMime());
// Save the encrypted MIME to a file.
mime.SaveMime('./encrypted.mime');
// Here's the S/MIME
// Subject: test
// SomeHeader: 123
// Content-Type: application/x-pkcs7-mime; name="smime.p7m"; smime-type="enveloped-data"
// Content-Disposition: attachment; filename="smime.p7m"
// Content-Transfer-Encoding: base64
//
// MIICSwYJKoZIhvcNAQcDoIICPDCCAjgCAQAxggGzMIIBrwIBADCBljCBgTELMAkGA1UEBhMCSVQx
// EDAOBgNVBAgMB0JlcmdhbW8xGTAXBgNVBAcMEFBvbnRlIFNhbiBQaWV0cm8xFzAVBgNVBAoMDkFj
// dGFsaXMgUy5wLkEuMSwwKgYDVQQDDCNBY3RhbGlzIENsaWVudCBBdXRoZW50aWNhdGlvbiBDQSBH
// MwIQPCWvkSv8oQ7xRmEHJ6TzEDANBgkqhkiG9w0BAQEFAASCAQB3VZvHRE5EWxug7Sckpcz1ucDZ
// YiTKiqmyPt75MhzNRQLtKFx/jWwlemUwnPMzeu6yutCkZ74Bdn7MBsfDqV3bUz43wAu+fRBteGvF
// mTc00MfY8L7o8dkpj4AqAOCj4hKQzbSE99GvSzyXcPE2Gm5NrOPtKxqfFqbBRTCb4fBZP84LaL+x
// rnYfrM4qXTppixyN8iFYCd4maEbMu/GA5o+j0BkDDnx42pILDoAGV/ERyx55Y3Nc2Mhm/cITBMNn
// g7uS9KPrlYizNaqVu09Hi9jg4gdZaRiTjUqg05tSOk/YqIQxTgfscwSPY92/ewpI6e1EHtLt8Q33
// gWCbERptSntUMHwGCSqGSIb3DQEHATAdBglghkgBZQMEAQIEENm1AxeXlEMx7p6McjHIj5CAUEQj
// 0GuJ5LnTqiqIjOiwmwNidl1N1TRluxX5vAQvwBuYE6bQK4+i04yn2Av3cucW4kvxgP2Nmni+XgQt
// aPPKlasaVceEeZ15IYjw77/m3YYn
cert.Free;
mime.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.