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) {}
}
}
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();
}
添加回答
举报