use of com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo in project LinLong-Java by zhenwei1108.
the class CMSEncryptedDataGenerator method doGenerate.
private CMSEncryptedData doGenerate(CMSTypedData content, OutputEncryptor contentEncryptor) throws CMSException {
AlgorithmIdentifier encAlgId;
ASN1OctetString encContent;
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
try {
OutputStream cOut = contentEncryptor.getOutputStream(bOut);
content.write(cOut);
cOut.close();
} catch (IOException e) {
throw new CMSException("");
}
byte[] encryptedContent = bOut.toByteArray();
encAlgId = contentEncryptor.getAlgorithmIdentifier();
encContent = new BEROctetString(encryptedContent);
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.encryptedData, new EncryptedData(eci, unprotectedAttrSet));
return new CMSEncryptedData(contentInfo);
}
use of com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo 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);
}
use of com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo in project itext2 by albfernandez.
the class PdfPublicKeySecurityHandler method createDERForRecipient.
private ASN1Primitive createDERForRecipient(byte[] in, X509Certificate cert) throws IOException, GeneralSecurityException {
String s = "1.2.840.113549.3.2";
AlgorithmParameterGenerator algorithmparametergenerator = AlgorithmParameterGenerator.getInstance(s);
AlgorithmParameters algorithmparameters = algorithmparametergenerator.generateParameters();
ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(algorithmparameters.getEncoded("ASN.1"));
ASN1InputStream asn1inputstream = new ASN1InputStream(bytearrayinputstream);
ASN1Primitive derobject = asn1inputstream.readObject();
KeyGenerator keygenerator = KeyGenerator.getInstance(s);
keygenerator.init(128);
SecretKey secretkey = keygenerator.generateKey();
Cipher cipher = Cipher.getInstance(s);
cipher.init(1, secretkey, algorithmparameters);
byte[] abyte1 = cipher.doFinal(in);
DEROctetString deroctetstring = new DEROctetString(abyte1);
KeyTransRecipientInfo keytransrecipientinfo = computeRecipientInfo(cert, secretkey.getEncoded());
DERSet derset = new DERSet(new RecipientInfo(keytransrecipientinfo));
AlgorithmIdentifier algorithmidentifier = new AlgorithmIdentifier(new ASN1ObjectIdentifier(s), derobject);
EncryptedContentInfo encryptedcontentinfo = new EncryptedContentInfo(PKCSObjectIdentifiers.data, algorithmidentifier, deroctetstring);
EnvelopedData env = new EnvelopedData(null, derset, encryptedcontentinfo, (org.bouncycastle.asn1.ASN1Set) null);
ContentInfo contentinfo = new ContentInfo(PKCSObjectIdentifiers.envelopedData, env);
return contentinfo.toASN1Primitive();
}
Aggregations