亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從簽名文件 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 關注
  • 323 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號