디지털 서명된 메시지 관리

Aspose.Email은 전체 S/MIME 이메일 객체 알고리즘을 구현합니다. 이를 통해 API는 이메일 메시지에 디지털 서명을 추가하고, 형식 변환 시 서명을 보존하거나 제거하는 등 완전한 기능을 제공합니다.

이메일에 서명 첨부

다음은 SecureEmailManager.attachSignature 메서드를 사용하면 이메일 메시지에 디지털 서명을 첨부할 수 있습니다. 서명을 첨부한 후에는 다음과 같은 속성을 통해 결과를 확인합니다 IsSigned, MessageClass그리고 첨부 파일 세부 정보를 포함합니다.

MailMessage 또는 MapiMessage, 로드된 개인 키를 SmimeKey, 및 서명 옵션을 사용하여 서명 첨부 파일 프로세스를 사용자 지정합니다 SignatureOptions 서명 첨부 시 분리형 또는 비분리형 서명을 포함한 다양한 옵션을 지정할 수 있게 해주는 클래스.

다음 코드 샘플은 파일에서 메시지를 로드하고, 개인 인증서를 사용해 분리형 및 비분리형 디지털 서명을 첨부한 뒤, 서명이 성공적으로 첨부되었는지 확인하는 방법을 보여줍니다.

String dataDir = "data/";

// Load the private key and its certificate; the password is a char array
SmimeKey privateCert = SmimeKey.loadPFX(dataDir + "certFile.pfx", "password".toCharArray());

MapiMessage msg = MapiMessage.load(dataDir + "message.msg");

SignatureOptions opt = new SignatureOptions();
opt.setDetached(true);
MapiMessage signedDetached = new SecureEmailManager().attachSignature(msg, privateCert, opt);

if (signedDetached.isSigned()) {
    System.out.println("Detached Signature Attached Successfully.");
}

opt.setDetached(false);
MapiMessage signedNonDetached = new SecureEmailManager().attachSignature(msg, privateCert, opt);

if (signedNonDetached.isSigned()) {
    System.out.println("Non-Detached Signature Attached Successfully.");
}

EML을 MSG로 변환할 때 서명 보존

Aspose.Email은 EML을 MSG로 변환할 때 디지털 서명을 보존합니다. 다음 코드 스니펫은 EML을 MSG로 변환하는 방법을 보여줍니다.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Load mail message
MailMessage message = MailMessage.load(dataDir + "Message.eml", new EmlLoadOptions());
// Save as MSG
message.save(dataDir + "ConvertEMLToMSG_out.msg", SaveOptions.getDefaultMsgUnicode());

Outlook 메시지 파일에서 서명 제거

호환성 등 이유로 MAPI 형식 메시지에서 서명을 제거해야 하는 경우, Aspose.Email은 다음을 제공합니다: MapiMessage.removeSignature 메서드와 MapiMessage.isSigned 속성.

다음 코드 스니펫은 파일에서 MAPI 메시지를 로드하고, 디지털 서명 여부를 확인한 뒤, 서명된 경우 디지털 서명 없이 새 메시지를 생성하는 방법을 보여줍니다:

MapiMessage msg = MapiMessage.load("data/signed.msg");

if (msg.isSigned()) {
    MapiMessage unsignedMsg = msg.removeSignature();
}

MSG에서 EML로 S/MIME 메시지 변환

Aspose.Email은 MSG를 EML로 변환할 때 디지털 서명을 보존합니다. 다음 코드 스니펫을 참고하십시오.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

MapiMessage mapi = MapiMessage.load(dataDir + "message.msg");

// The signature is kept when the message is converted
MailMessage eml = mapi.toMailMessage(new MailConversionOptions());
eml.save(dataDir + "ConvertMIMEMessagesFromMSGToEML_out.eml", SaveOptions.getDefaultEml());

인증서를 사용해 MSG 파일 복호화

암호화된 MAPI 메시지가 있고, 인증서에 저장된 개인키를 사용해 복호화해야 하는 경우, Aspose.Email의 다음 기능이 유용할 수 있습니다:

.NET API와 달리 Java API에는 매개변수 없는 decrypt() Windows 인증서 저장소를 검색하는: 키는 항상 전달되어야 합니다.

다음 코드 스니펫은 암호화된 MAPI 메시지를 처리하는 방법을 보여줍니다:

String dataDir = "data/";

SmimeKey privateCert = SmimeKey.loadPFX(dataDir + "privateCertFile.pfx", "password".toCharArray());
MapiMessage msg = MapiMessage.load(dataDir + "encrypted.msg");

if (msg.isEncrypted()) {
    MapiMessage decryptedMsg = msg.decrypt(privateCert);

    // or, with the raw content of the PFX file:
    // MapiMessage decryptedMsg = msg.decrypt(Files.readAllBytes(Paths.get(dataDir + "privateCertFile.pfx")), "password");
}

보안 이메일 서명 확인

다음 API 멤버를 사용하여 서명을 확인할 수 있습니다: MapiMessage 및 MailMessage 객체:

  • 그 SecureEmailManager 보안 이메일 서명을 검사하기 위한 클래스;
  • 그 SmimeResult 클래스는 검사 결과를 저장합니다;
  • 그 checkSignature(MapiMessage msg) 및 checkSignature(MapiMessage msg, SmimeKey key) 메서드들.

인증서를 제공하지 않고 메시지 서명을 확인합니다:

MapiMessage msg = MapiMessage.load("signed.msg");

SmimeResult result = new SecureEmailManager().checkSignature(msg);

System.out.println("Signature valid: " + result.isSuccess());
if (!result.isSuccess()) {
    System.out.println("Error: " + result.getError());
}

암호화된 메시지의 서명을 검증하려면 복호화에 사용된 인증서를 제공하십시오:

MapiMessage msg = MapiMessage.load("signed-and-encrypted.msg");

SmimeKey key = SmimeKey.loadPFX("cert.pfx", "pass".toCharArray());

SmimeResult result = new SecureEmailManager().checkSignature(msg, key);

System.out.println("Signature valid: " + result.isSuccess());
for (java.security.cert.X509Certificate certificate : result.getSigningCertificates()) {
    System.out.println("Signed by: " + certificate.getSubjectX500Principal());
}