gpt4 book ai didi

java - 阅读加密邮件

转载 作者:行者123 更新时间:2023-11-29 09:13:22 25 4
gpt4 key购买 nike

我有一个 PFX 证书,用于在 Outlook 中读取加密的电子邮件。我想通过 Java 阅读这封电子邮件。我设法获得了 javax.mail.Message 对象的列表。使用我的 PFX key 解密这些内容的最简单方法是什么?

最佳答案

使用 BouncycaSTLe 和 JavaMail:

Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "user@gmail.com", "gmailPassword");
System.out.println(store);

Folder inbox = store.getFolder("MyFolder");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.getMessages();

for (Message message : messages) {
KeyStore ks = KeyStore.getInstance("pkcs12");

FileInputStream fis = new FileInputStream("c:\\key.pfx");
String password = "pfxPassword";

ks.load(fis, password.toCharArray());
String alias = ks.aliases().nextElement();

PrivateKey pKey = (PrivateKey) ks.getKey(alias, password.toCharArray());
X509Certificate cert = (X509Certificate) ks.getCertificate(alias);

RecipientId recId = new RecipientId();

recId.setSerialNumber(cert.getSerialNumber());
recId.setIssuer(cert.getIssuerX500Principal().getEncoded());

SMIMEEnveloped m = new SMIMEEnveloped((MimeMessage) message);
RecipientInformationStore recipients = m.getRecipientInfos();
RecipientInformation recipient = recipients.get(recId);

MimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient.getContent(pKey, ks.getProvider().getName()));

MimeMultipart parts=(MimeMultipart) res.getContent();

for (int i=0;i<parts.getCount();i++){
BodyPart part=parts.getBodyPart(i);
if (part.getContentType().contains("application/octet-stream")){
//System.out.println(IOUtils.toString((InputStream) part.getContent()));
ZipInputStream zin = new ZipInputStream((InputStream)part.getContent());
ZipEntry entry;
while((entry = zin.getNextEntry()) != null) {
System.out.println(IOUtils.toString(zin));
}
}
}
}

关于java - 阅读加密邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11079848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com