我需要使用信封模式(p7m)或信封模式(p7s)从签名文件中提取对其进行签名的原始文件。
我很难弄清楚如何使用bouncycastle库执行此操作。
我同时使用BouncyCastle 1.5和BouncyCastle 1.4
var result = query.Select(employee => new EMPLOYEE_DTO()
{
PHONE_NO = employee.PHONE_NO,
EMAIL = employee.EMAIL,
EMP_NO = employee.EMP_NO,
})
.ToList();
部分代码来自https://github.com/esig/dss项目
我缺少使方法“ getOriginalDocumentBinaries”工作的原因吗? 对我来说似乎没事,但我不是Bouncycastle的专家。
问候。
答案 0 :(得分:1)
我只是遇到了同样的问题,所以我实现了它。我相信这对其他人可能会有帮助。
/**
*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();
}
答案 1 :(得分:0)
此解决方案似乎有效:
/**
* 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) {}
}
}