如何找到私钥解密密码.p7m消息与RecipientInformation?



我尝试使用以下Java代码来解密来自JavaMail API的S/MIME消息:

String mimeType = mail.getContentType();
if( mimeType == null ) {
return mail;
}
ContentType contentType = new ContentType( mimeType );
if( "application/pkcs7-mime".equals( contentType.getBaseType() ) //
&& "smime.p7m".equals( contentType.getParameter( "name" ) ) ) {
Object content = mail.getContent();
if( content instanceof InputStream ) {
CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser( (InputStream)content );
RecipientInformationStore recipients = ep.getRecipientInfos();
Iterator<RecipientInformation> it = recipients.getRecipients().iterator();
RecipientInformation recipient = (RecipientInformation) it.next();
recipient.getContent(new JceKeyTransEnvelopedRecipient(privateKey).setProvider( provider) );
}
}

我有一个私钥列表。我怎样才能找到正确的私钥传递在jcekeytransenveledreceiver构造函数?我认为这应该是可能的与RecipientInformation对象。

S/MIME消息通常使用来自证书的公钥加密,特别是X.509或PKIX证书(PKIX是X.509的互联网'版本'或技术配置文件)和KeyTransRI -如果这确实是你所拥有的,你的代码不检查-或KeyAgreeRI包含一个'收件人id',它实际上标识证书;您需要将该证书映射到相应的私钥,这就是Java加密的正常工作方式(KeyStoreAPI将私钥与其相应的证书或链存储在一起)。

RecipientInformation.getRID()返回RecipientId,通常是KeyTransRecipientIdKeyAgreeRecipientId,对应于RecipientInfo,其中任何一个都可以让您获得颁发者和证书的序列,或者'主题密钥标识符',这是大多数证书的扩展,通常包含其主题密钥位字符串值的哈希。

类似地,签名是用私钥完成的,SignerInfo包含相应证书的id,分发给救济者并由救济者使用以验证签名。

最新更新