メールのセキュリティと暗号化
メッセージの暗号化/復号化
Aspose.Email は X509Certificates を使用してメールメッセージの暗号化と復号化を行う機能を提供します。本記事では、既存または新規メッセージをロードし、次を使用して暗号化する方法を示します。 MailMessage。 Encrypt() および Decrypt() メソッドは、 MailMessage 適用された効果のためのオブジェクトであり、メッセージの暗号化/復号化時に注意が必要です。メッセージの暗号化と復号化には以下の手順が含まれます。
- 新しいメッセージを作成するか、既存のものをロードする
- X509Certificate オブジェクトを使用して暗号化証明書をロードする
- 証明書を使用してメッセージを暗号化する
- メッセージを送信するか保存する
- 必要に応じてメッセージを復号化する
以下のコードスニペットは、メッセージを暗号化および復号化する方法を示しています。
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Email();
string publicCertFile = dataDir + "MartinCertificate.cer";
string privateCertFile = dataDir + "MartinCertificate.pfx";
X509Certificate2 publicCert = new X509Certificate2(publicCertFile);
X509Certificate2 privateCert = new X509Certificate2(privateCertFile, "anothertestaccount");
// Create a message
MailMessage msg = new MailMessage("atneostthaecrcount@gmail.com", "atneostthaecrcount@gmail.com", "Test subject", "Test Body");
// Encrypt the message
MailMessage eMsg = msg.Encrypt(publicCert);
if (eMsg.IsEncrypted == true)
Console.WriteLine("Its encrypted");
else
Console.WriteLine("Its NOT encrypted");
// Decrypt the message
MailMessage dMsg = eMsg.Decrypt(privateCert);
if (dMsg.IsEncrypted == true)
Console.WriteLine("Its encrypted");
else
Console.WriteLine("Its NOT encrypted");
メッセージ暗号化の検証
Aspose.Email MailMessage メッセージが暗号化されているかどうかをチェックできるクラスです。 IsEncrypted のプロパティ MailMessage 以下のコードサンプルに示すように、これを確認できます。
MailMessage mailMessageOrig = MailMessage.Load(Path.Combine(dataDir, "Message.msg"), new MsgLoadOptions());
X509Certificate2 publicCert = new X509Certificate2(publicCertFile);
X509Certificate2 privateCert = new X509Certificate2(privateCertFile, "anothertestaccount");
Console.WriteLine("Message is encrypted: {0}" , mailMessageOrig.IsEncrypted);
MailMessage mailMessage = mailMessageOrig.Encrypt(publicCert);
Console.WriteLine("Message is encrypted: {0}", mailMessage.IsEncrypted);
mailMessage = mailMessage.Decrypt(privateCert);
Console.WriteLine("Message is encrypted: {0}", mailMessage.IsEncrypted);
セキュアメールの署名チェック
この SecureEmailManager クラスは、セキュアな MailMessage オブジェクトの署名をチェックできるようにします。
この SmimeResult クラスはチェック結果を格納します。
以下のメソッドは SecureEmailManager クラスとコードスニペットにより、署名を処理できるようになります。
- SecureEmailManager.CheckSignature(MailMessage msg) メソッド。
- SecureEmailManager.CheckSignature(MailMessage msg, X509Certificate2 certificateForDecrypt) メソッド。
- SecureEmailManager.CheckSignature(MailMessage msg, X509Certificate2 certificateForDecrypt, X509Store store) メソッド。
証明書を提供せずにメッセージの署名をチェックします:
var eml = MailMessage.Load(fileName);
var result = new SecureEmailManager().CheckSignature(eml);
復号用証明書と検証に使用する証明書ストアを指定して、暗号化メッセージの署名をチェックします:
var certFileName = "cert.pfx";
var cert = new X509Certificate2(certFileName, "pass");
var eml = MailMessage.Load(fileName);
var store = new X509Store();
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
store.Close();
var result = new SecureEmailManager().CheckSignature(eml, cert, store);
DKIM でメールに署名
Aspose.Email は DKIM(DomainKeys Identified Mail)でメールに署名することを可能にします。これにより、組織は送信中のメッセージに対して責任を負うことができます(詳細情報)。DKIM はメールヘッダーにデジタル署名を付与し、受信者が検証できるようにします。送信者の公開鍵により、受信者は署名がメッセージ内容と一致しているかを確認できます。 DKIMSign メソッド( MailMessage メッセージに署名するための暗号化および署名情報を設定するクラスです。以下のコードスニペットは、DKIM を使用してメールに署名する方法を示します。
var eml = new MailMessage("sender@gmail.com", "receiver@gmail.com", "Some subject", "Some body text");
string privateKeyFile = "key2.pem";
RSACryptoServiceProvider rsa = PemReader.GetPrivateKey(privateKeyFile);
DKIMSignatureInfo signInfo = new DKIMSignatureInfo("test", "somedomain.com");
var signedEml = eml.DKIMSign(rsa, signInfo);