이메일 보안 및 암호화
메시지 암호화/복호화
Aspose.Email는 X509Certificates를 사용하여 이메일 메시지를 암호화하고 복호화하는 기능을 제공합니다. 이 문서에서는 기존 메시지 또는 새 메시지를 로드하고 다음을 사용하여 암호화하는 방법을 보여줍니다. MailMessage. The 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(도메인키 식별 메일)으로 이메일에 서명할 수 있게 합니다. 이를 통해 조직은 전송 중인 메시지에 대한 책임을 질 수 있습니다 (추가 정보). 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);