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

Decrypt an S/MIME email and verify its signature

See more Email Object Examples

Register the recipient’s certificate and private key (from a PEM file) before opening signed-encrypted.eml. This workflow expects both encryption and a signature, so it checks both layers before reading the message. Cryptographic signature validity is one input to a signer-trust decision.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // Assumes Chilkat is unlocked. See Global Unlock Sample.

  final email = CkEmail();
  // The recipient's certificate and private key are in a PEM file.  The 2nd argument of LoadPemFile
  // is the password of the private key.
  final pem = CkPem();
  try {
    pem.loadPemFile('recipient.pem', 'RECIPIENT_PASSWORD');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    email.addPemSource(pem);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    email.loadEml('signed-encrypted.eml');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  if ((!email.receivedEncrypted) || (!email.decrypted)) {
    print('Expected a successfully decrypted S/MIME envelope.');
    return;
  }

  if ((!email.receivedSigned) || (!email.signaturesValid)) {
    print('Expected a valid signature inside the envelope.');
    return;
  }

  // This message has one signer. Examine its certificate after verification.
  final signer = CkCert();
  try {
    email.lastSignerCert(0, signer);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Signer certificate: ${signer.subjectDN}');
  print('Recovered subject: ${email.subject}');
  print('Recovered attachments: ${email.numAttachments}');
}