General class for generating a CMS enveloped-data message. A simple example of usage.
 CmsEnvelopedDataGenerator  fact = new CmsEnvelopedDataGenerator(); fact.AddKeyTransRecipient(cert); CmsEnvelopedData         data = fact.Generate(content, algorithm); 
Inheritance: CmsEnvelopedGenerator
示例#1
0
 /// <summary>
 /// Verschlüsselt die Daten mit dem angegebenen Empfänger-Zertifikat
 /// </summary>
 /// <param name="data">Die zu verschlüsselnden Daten</param>
 /// <param name="cert">Das Empfänger-Zertifikat</param>
 /// <returns>Die verschlüsselten Daten</returns>
 public static byte[] EncryptData(byte[] data, X509Certificate cert)
 {
     var gen = new CmsEnvelopedDataGenerator();
     gen.AddKeyTransRecipient(cert);
     var message = new CmsProcessableByteArray(data);
     var envelopedData = gen.Generate(message, PkcsObjectIdentifiers.DesEde3Cbc.Id);
     var encryptedData = envelopedData.GetEncoded();
     return encryptedData;
 }
        Stream Envelope(CmsRecipientCollection recipients, Stream content)
        {
            var cms = new CmsEnvelopedDataGenerator ();
            int count = 0;

            foreach (var recipient in recipients) {
                cms.AddKeyTransRecipient (recipient.Certificate);
                count++;
            }

            if (count == 0)
                throw new ArgumentException ("No recipients specified.", "recipients");

            // FIXME: how to decide which algorithm to use?
            var input = new CmsProcessableInputStream (content);
            var envelopedData = cms.Generate (input, CmsEnvelopedGenerator.DesEde3Cbc);

            return new MemoryStream (envelopedData.GetEncoded (), false);
        }
示例#3
0
		private void PasswordUtf8Test(
			string algorithm)
		{
			byte[] data = Hex.Decode("504b492d4320434d5320456e76656c6f706564446174612053616d706c65");

			CmsEnvelopedDataGenerator edGen = new CmsEnvelopedDataGenerator();

			edGen.AddPasswordRecipient(
				new Pkcs5Scheme2Utf8PbeKey("abc\u5639\u563b".ToCharArray(), new byte[20], 5),
				algorithm);

			CmsEnvelopedData ed = edGen.Generate(
				new CmsProcessableByteArray(data),
				CmsEnvelopedDataGenerator.Aes128Cbc);

			RecipientInformationStore recipients = ed.GetRecipientInfos();

			Assert.AreEqual(ed.EncryptionAlgOid, CmsEnvelopedDataGenerator.Aes128Cbc);

			ICollection c = recipients.GetRecipients();

			Assert.AreEqual(1, c.Count);

			foreach (PasswordRecipientInformation recipient in c)
			{
				CmsPbeKey key = new Pkcs5Scheme2Utf8PbeKey(
					"abc\u5639\u563b".ToCharArray(), recipient.KeyDerivationAlgorithm);

				byte[] recData = recipient.GetContent(key);

				Assert.IsTrue(Arrays.AreEqual(data, recData));
			}
		}
示例#4
0
		public void TestECMqvKeyAgreeMultiple()
		{
			byte[] data = Hex.Decode("504b492d4320434d5320456e76656c6f706564446174612053616d706c65");

			CmsEnvelopedDataGenerator edGen = new CmsEnvelopedDataGenerator();

			ArrayList recipientCerts = new ArrayList();
			recipientCerts.Add(ReciECCert);
			recipientCerts.Add(ReciECCert2);

			edGen.AddKeyAgreementRecipients(
				CmsEnvelopedDataGenerator.ECMqvSha1Kdf,
				OrigECKP.Private,
				OrigECKP.Public,
				recipientCerts,
				CmsEnvelopedDataGenerator.Aes128Wrap);

			CmsEnvelopedData ed = edGen.Generate(
				new CmsProcessableByteArray(data),
				CmsEnvelopedDataGenerator.Aes128Cbc);

			Assert.AreEqual(ed.EncryptionAlgOid, CmsEnvelopedDataGenerator.Aes128Cbc);

			RecipientInformationStore recipients = ed.GetRecipientInfos();

			ConfirmDataReceived(recipients, data, ReciECCert, ReciECKP.Private);
			ConfirmDataReceived(recipients, data, ReciECCert2, ReciECKP2.Private);
			ConfirmNumberRecipients(recipients, 2);
		}
示例#5
0
		private void TryKekAlgorithm(
			KeyParameter		kek,
			DerObjectIdentifier	algOid)
		{
			byte[] data = Encoding.ASCII.GetBytes("WallaWallaWashington");
			CmsEnvelopedDataGenerator edGen = new CmsEnvelopedDataGenerator();

			byte[] kekId = new byte[] { 1, 2, 3, 4, 5 };

			string keyAlgorithm = ParameterUtilities.GetCanonicalAlgorithmName(algOid.Id);

			edGen.AddKekRecipient(keyAlgorithm, kek, kekId);

			CmsEnvelopedData ed = edGen.Generate(
				new CmsProcessableByteArray(data),
				CmsEnvelopedDataGenerator.DesEde3Cbc);

			RecipientInformationStore recipients = ed.GetRecipientInfos();

			Assert.AreEqual(ed.EncryptionAlgOid, CmsEnvelopedDataGenerator.DesEde3Cbc);

			ArrayList c = new ArrayList(recipients.GetRecipients());

			Assert.IsTrue(c.Count > 0);

			foreach (RecipientInformation recipient in c)
			{
				Assert.AreEqual(algOid.Id, recipient.KeyEncryptionAlgOid);

				byte[] recData = recipient.GetContent(kek);

				Assert.IsTrue(Arrays.AreEqual(data, recData));
			}
		}
示例#6
0
		private void TryKeyTrans(
			string				generatorOID,
			DerObjectIdentifier	checkOID,
			Type				asn1Params)
		{
			byte[] data = Encoding.ASCII.GetBytes("WallaWallaWashington");

			CmsEnvelopedDataGenerator edGen = new CmsEnvelopedDataGenerator();

			edGen.AddKeyTransRecipient(ReciCert);

			CmsEnvelopedData ed = edGen.Generate(new CmsProcessableByteArray(data), generatorOID);

			RecipientInformationStore recipients = ed.GetRecipientInfos();

			Assert.AreEqual(checkOID.Id, ed.EncryptionAlgOid);

			if (asn1Params != null)
			{
				Assert.IsTrue(asn1Params.IsInstanceOfType(ed.EncryptionAlgorithmID.Parameters));
			}

			ArrayList c = new ArrayList(recipients.GetRecipients());

			Assert.AreEqual(1, c.Count);

			foreach (RecipientInformation recipient in c)
			{
				Assert.AreEqual(recipient.KeyEncryptionAlgOid, PkcsObjectIdentifiers.RsaEncryption.Id);

				byte[] recData = recipient.GetContent(ReciKP.Private);

				Assert.IsTrue(Arrays.AreEqual(data, recData));
			}
		}
示例#7
0
		public void TestKeyTransSmallAes()
		{
			byte[] data = new byte[] { 0, 1, 2, 3 };

			CmsEnvelopedDataGenerator edGen = new CmsEnvelopedDataGenerator();

			edGen.AddKeyTransRecipient(ReciCert);

			CmsEnvelopedData ed = edGen.Generate(
				new CmsProcessableByteArray(data),
				CmsEnvelopedDataGenerator.Aes128Cbc);

			RecipientInformationStore recipients = ed.GetRecipientInfos();

			Assert.AreEqual(ed.EncryptionAlgOid,
				CmsEnvelopedDataGenerator.Aes128Cbc);

			ICollection c = recipients.GetRecipients();

			Assert.AreEqual(1, c.Count);

			foreach (RecipientInformation recipient in c)
			{
				byte[] recData = recipient.GetContent(ReciKP.Private);
				Assert.IsTrue(Arrays.AreEqual(data, recData));
			}
		}
示例#8
0
		public void TestKeyTransOdes()
		{
			byte[] data = Encoding.ASCII.GetBytes("WallaWallaBouncyCastle");

			CmsEnvelopedDataGenerator edGen = new CmsEnvelopedDataGenerator();

			edGen.AddKeyTransRecipient(ReciCert);

			CmsEnvelopedData ed = edGen.Generate(
				new CmsProcessableByteArray(data),
				OiwObjectIdentifiers.DesCbc.Id);

			RecipientInformationStore recipients = ed.GetRecipientInfos();

			Assert.AreEqual(ed.EncryptionAlgOid, OiwObjectIdentifiers.DesCbc.Id);

			ICollection c = recipients.GetRecipients();

			Assert.AreEqual(1, c.Count);

			foreach (RecipientInformation recipient in c)
			{
				byte[] recData = recipient.GetContent(ReciKP.Private);

				Assert.IsTrue(Arrays.AreEqual(data, recData));
			}
		}
示例#9
0
		public void TestKeyTrans128RC4()
		{
			byte[] data = Encoding.ASCII.GetBytes("WallaWallaBouncyCastle");

			CmsEnvelopedDataGenerator edGen = new CmsEnvelopedDataGenerator();

			edGen.AddKeyTransRecipient(ReciCert);

			CmsEnvelopedData ed = edGen.Generate(
				new CmsProcessableByteArray(data),
				"1.2.840.113549.3.4", 128);  // RC4 OID

			RecipientInformationStore recipients = ed.GetRecipientInfos();

			Assert.AreEqual(ed.EncryptionAlgOid, "1.2.840.113549.3.4");

			ICollection c = recipients.GetRecipients();

			Assert.AreEqual(1, c.Count);

			foreach (RecipientInformation recipient in c)
			{
				byte[] recData = recipient.GetContent(ReciKP.Private);

				Assert.IsTrue(Arrays.AreEqual(data, recData));
			}
		}
 private static byte[] EncryptData(byte[] data, X509Certificate encryptionCertificate)
 {
     var dataGenerator = new CmsEnvelopedDataGenerator();
     dataGenerator.AddKeyTransRecipient(encryptionCertificate);
     var encryptedData = dataGenerator.Generate(new CmsProcessableByteArray(data), CmsEnvelopedGenerator.Aes256Cbc);
     return encryptedData.GetEncoded();
 }