use of com.github.zhenwei.pkix.operator.OutputAEADEncryptor in project LinLong-Java by zhenwei1108.
the class CMSEnvelopedDataGenerator method doGenerate.
private CMSEnvelopedData doGenerate(CMSTypedData content, OutputEncryptor contentEncryptor) throws CMSException {
ASN1EncodableVector recipientInfos = new ASN1EncodableVector();
AlgorithmIdentifier encAlgId;
ASN1OctetString encContent;
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
try {
OutputStream cOut = contentEncryptor.getOutputStream(bOut);
content.write(cOut);
cOut.close();
if (contentEncryptor instanceof OutputAEADEncryptor) {
byte[] mac = ((OutputAEADEncryptor) contentEncryptor).getMAC();
bOut.write(mac, 0, mac.length);
}
} catch (IOException e) {
throw new CMSException("");
}
byte[] encryptedContent = bOut.toByteArray();
encAlgId = contentEncryptor.getAlgorithmIdentifier();
encContent = new BEROctetString(encryptedContent);
GenericKey encKey = contentEncryptor.getKey();
for (Iterator it = recipientInfoGenerators.iterator(); it.hasNext(); ) {
RecipientInfoGenerator recipient = (RecipientInfoGenerator) it.next();
recipientInfos.add(recipient.generate(encKey));
}
EncryptedContentInfo eci = new EncryptedContentInfo(content.getContentType(), encAlgId, encContent);
ASN1Set unprotectedAttrSet = null;
if (unprotectedAttributeGenerator != null) {
AttributeTable attrTable = unprotectedAttributeGenerator.getAttributes(Collections.EMPTY_MAP);
unprotectedAttrSet = new BERSet(attrTable.toASN1EncodableVector());
}
ContentInfo contentInfo = new ContentInfo(CMSObjectIdentifiers.envelopedData, new EnvelopedData(originatorInfo, new DERSet(recipientInfos), eci, unprotectedAttrSet));
return new CMSEnvelopedData(contentInfo);
}
Aggregations