Dart
Dart
Encrypt MIME for Multiple Recipients
See more MIME Examples
Demonstrates encrypting the current MIME entity for several recipients at once. A certificate is added for each recipient with AddEncryptCert, and EncryptN then produces a single CMS/PKCS #7 message that each recipient can independently decrypt.
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. When a message targets more than one recipient, each recipient's certificate is added to the internal list and EncryptN wraps the message key separately for each one. Any single recipient can then decrypt with their own private key.
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
final mime = CkMime();
try {
mime.setBodyFromPlainText('This message will be encrypted.');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Add a certificate for each intended recipient. Every recipient receives an independently
// wrapped copy of the message key.
final cert1 = CkCert();
try {
cert1.loadFromFile('qa_data/recipient1.cer');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
mime.addEncryptCert(cert1);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final cert2 = CkCert();
try {
cert2.loadFromFile('qa_data/recipient2.cer');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
mime.addEncryptCert(cert2);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Encrypt for every added recipient at once.
try {
mime.encryptN();
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
mime.saveMime('qa_output/encrypted_multi.eml');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('Message encrypted for multiple recipients.');
}