我正在尝试使用Bouncycastle解密Android上的s / mime加密附件(来自Outlook / Exchange,smime.p7m)。
我可以在Windows上使用p7mViewer解密/查看内容。
代码,如bc-java的ReadEncryptedMail.java:
ByteArrayInputStream inputStream = new ByteArrayInputStream(encryptedContent);
Session session = Session.getInstance(new Properties());
MimeMessage mimeMessage = new MimeMessage(session, inputStream);
SMIMEEnveloped smimeEnveloped = new SMIMEEnveloped(mimeMessage);
结果我得到java.io.IOException:DER长度超过4个字节:75 org.bouncycastle.asn1.ASN1InputStream.readLength(未知来源:53)
我缺少什么?
答案 0 :(得分:0)
经过大量挖掘后,我在bouncycastle邮件列表http://bouncy-castle.1462172.n4.nabble.com/how-to-generically-parse-a-PKCS7-object-td1467990.html上找到了“如何一般地解析PKCS7对象”
使用代码我获得了ENVELOPEDDATA的内容类型,这导致我尝试成功解析内容的CMSEnvelopedDataParser。
CMSEnvelopedDataParser cmsEnvelopedDataParser = new CMSEnvelopedDataParser(encryptedContent);
Collection recInfos = cmsEnvelopedDataParser.getRecipientInfos().getRecipients();
Iterator recipientIterator = recInfos.iterator();
if (recipientIterator.hasNext()) {
RecipientInformation recipientInformation = (RecipientInformation) recipientIterator.next();
byte[] contentBytes = recipientInformation.getContent(new JceKeyTransEnvelopedRecipient(privateKey));
String content = new String(contentBytes);
}