Sample code for 30+ languages & platforms
CkPerl Requires Chilkat v11.0.0+

Encrypt with RSA-OAEP and AES-128-CBC

See more Email Object Examples

Use this profile when the receiver requires AES-128-CBC content encryption and RSA-OAEP with SHA-256. The SHA-256 settings apply to OAEP and its mask generation function. Use the matching decryption example to inspect the result.

Chilkat CkPerl Downloads

CkPerl
use chilkat();

$success = 0;

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

$email = chilkat::CkEmail->new();
$success = $email->LoadEml("message.eml");
if ($success != 1) {
    print $email->lastErrorText() . "\r\n";
    exit;
}

#  Use the recipient's public encryption certificate. Its private key stays with the recipient.
$cert = chilkat::CkCert->new();
$success = $cert->LoadFromFile("recipient.cer");
if ($success != 1) {
    print $cert->lastErrorText() . "\r\n";
    exit;
}

$success = $email->SetEncryptCert($cert);
if ($success != 1) {
    print $email->lastErrorText() . "\r\n";
    exit;
}

$email->put_Pkcs7CryptAlg("aes");
$email->put_Pkcs7KeyLength(128);
$email->put_OaepPadding(1);
$email->put_OaepHash("sha256");
$email->put_OaepMgfHash("sha256");
$email->put_SendEncrypted(1);
$mail = chilkat::CkMailMan->new();
$rendered = chilkat::CkBinData->new();
$success = $mail->RenderToMimeBd($email,$rendered);
if ($success != 1) {
    print $mail->lastErrorText() . "\r\n";
    exit;
}

$success = $rendered->WriteFile("oaep-encrypted.eml");
if ($success != 1) {
    print "Failed: rendered.WriteFile." . "\r\n";
    exit;
}

print "Saved oaep-encrypted.eml." . "\r\n";