为了账号安全,请及时绑定邮箱和手机立即绑定

如何从签名文件 p7m/Enveloped 和 p7s/Enveloping 提取用

如何从签名文件 p7m/Enveloped 和 p7s/Enveloping 提取用

慕容708150 2021-06-28 17:58:21
我需要从带有包络模式 (p7m) 或包络模式 (p7s) 的签名文件中提取其已签名的原始文件。我很难弄清楚如何使用 bouncycastle 库来做到这一点。我同时使用了 BouncyCastle 1.5 和 BouncyCastle 1.4private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(GetOriginalContentFromFileSigned.class);protected static CMSSignedData extractContent(InputStream buffer) throws IOException, CMSException {            CMSSignedData cms = null;    CMSSignedData signature = new CMSSignedData(buffer);            Store cs = signature.getCertificates();    SignerInformationStore signers = signature.getSignerInfos();    Collection c = signers.getSigners();    Iterator it = c.iterator();    //the following array will contain the content of xml document    byte[] data = null;    while (it.hasNext()) {        SignerInformation signer = (SignerInformation) it.next();        Collection certCollection = cs.getMatches(signer.getSID());        Iterator certIt = certCollection.iterator();        X509CertificateHolder cert = (X509CertificateHolder) certIt.next();        CMSProcessable sc = signature.getSignedContent();        data = (byte[]) sc.getContent();        cms = signature;    }    return cms;}   protected static byte[] getOriginalDocumentBinaries(InputStream signedDocument) {    try (InputStream is = getOriginalDocumentStream(signedDocument)) {        return IOUtils.toByteArray(is);    } catch (Exception e) {        logger.warn("Unable to retrieve original document binaries", e);        return null;    }}分代码摘自https://github.com/esig/dss项目我缺少什么使方法“getOriginalDocumentBinaries”起作用?对我来说似乎没问题,但我不是充气城堡的专家。问候。
查看完整描述

2 回答

?
月关宝盒

TA贡献1772条经验 获得超5个赞

这个解决方案似乎有效:


/**

* Extract content from p7m file

*/

    private byte[] getOriginalDocumentBinaries(final byte[] signedDoc) throws SignerException {        

        ASN1InputStream asn1InputStream = null;

        try {

            asn1InputStream = new ASN1InputStream(new ByteArrayInputStream(signedDoc));

            DERObject signedContent;

            try {

                signedContent = asn1InputStream.readObject();

            }

            catch (IOException cause) {

                logger.error(cause.getMessage(), (Throwable)cause);

                throw new SignerException(cause.getMessage(), cause);

            }

            CMSSignedData cmsSignedData;

            try {

                cmsSignedData = new CMSSignedData(ContentInfo.getInstance(signedContent));

            }

            catch (IllegalArgumentException cause2) {

                logger.error(cause2.getMessage(), (Throwable)cause2);

                throw new SignerException(cause2.getMessage(), cause2);

            }catch (Throwable cause2) {

                throw new SignerException(cause2.getMessage(), cause2);

            }

            return (byte[])((CMSProcessableByteArray)cmsSignedData.getSignedContent()).getContent();

        }catch(Exception ex){

            logger.error(ex.getMessage(),ex);

            throw new SignerException(ex);

        }

        finally {

            try {

                asn1InputStream.close();

            }

            catch (IOException ex) {}

        }

    }


查看完整回答
反对 回复 2021-07-07
?
慕田峪7331174

TA贡献1828条经验 获得超13个赞

我刚刚遇到了同样的问题,我实施了这个。我相信这可能对其他人有帮助。


/**

*Extract from .p7m file and write into new file  

*/


public void extractTxtFileFromP7M() throws Exception {

    File file = new File(".p7m FilePath");

    String fileName = FilenameUtils.removeExtension(file.getName());

    byte[] p7mFileByteArray = fromFileToByteArray(file);

    byte[] extractedFileByteArray = getData(p7mFileByteArray);

    File extractedFile = new File("..../fileName");

    FileUtils.writeByteArrayToFile(extractedFile, extractedFileByteArray);

}

private byte[] fromFileToByteArray(File file) {

    try {

        return FileUtils.readFileToByteArray(file);

    } catch (IOException e) {

        log.error("Error while reading .p7m file!", e);

    }

    return new byte[0];

}

private byte[] getData(final byte[] p7bytes) {

    CMSSignedData cms = null;

    try {

        cms = new CMSSignedData(p7bytes);

    } catch (CMSException e) {

        log.error("Error while converting bytes to CMSSignedData : " + e.getMessage(), e);

    }

    if( cms == null || cms.getSignedContent() == null) {

        return new byte[0];

    }

    return (byte[]) cms.getSignedContent().getContent();


查看完整回答
反对 回复 2021-07-07
  • 2 回答
  • 0 关注
  • 277 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信