Esempio n. 1
0
        public string Decrypt(string cipherText)
        {
            byte[] cipherBytes   = Convert.FromBase64String(cipherText);
            var    envelopedData = new CmsEnvelopedData(cipherBytes);
            RecipientInformationStore recipientsStore = envelopedData.GetRecipientInfos();
            ICollection recipientsCollection          = recipientsStore.GetRecipients();
            IList       recipients = recipientsCollection as IList;

            byte[] plainBytes = new byte[] { };
            int    index      = 0;

            foreach (KeyTransRecipientInformation recipientInfo in recipients)
            {
                // todo: better approach than catching n exceptions.
                RecipientInformation recipient = recipientsStore.GetFirstRecipient(recipientInfo.RecipientID);
                try
                {
                    plainBytes = recipient.GetContent(this.privateKey);
                    break;
                }
                catch (CmsException e) when(index != recipientsStore.Count - 1)
                {
                }
                index++;
            }
            return(Encoding.UTF8.GetString(plainBytes));
        }
        private void VerifyRecipient(RecipientInformation recipient, IAsymmetricKeyParameter privKey)
        {
            Assert.IsTrue(privKey.IsPrivate);

            Assert.AreEqual(recipient.KeyEncryptionAlgOid, PkcsObjectIdentifiers.RsaEncryption.Id);

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

            Assert.IsTrue(Arrays.AreEqual(exContent, recData));
        }
        public static byte[] DekrypterData2(byte[] kryptertData, AsymmetricKeyParameter privateKey)
        {
            CmsEnvelopedDataParser    cmsEnvelopedDataParser    = new CmsEnvelopedDataParser(kryptertData);
            RecipientInformationStore recipientInformationStore = cmsEnvelopedDataParser.GetRecipientInfos();

            IEnumerator enumerator = recipientInformationStore.GetRecipients().GetEnumerator();

            enumerator.MoveNext();
            RecipientInformation recipientInformation = enumerator.Current as RecipientInformation;

            return(recipientInformation.GetContent(privateKey));
        }
Esempio n. 4
0
        private static void ConfirmDataReceived(RecipientInformationStore recipients,
                                                byte[] expectedData, X509Certificate reciCert, AsymmetricKeyParameter reciPrivKey)
        {
            RecipientID rid = new RecipientID();

            rid.Issuer       = PrincipalUtilities.GetIssuerX509Principal(reciCert);
            rid.SerialNumber = reciCert.SerialNumber;

            RecipientInformation recipient = recipients[rid];

            Assert.IsNotNull(recipient);

            byte[] actualData = recipient.GetContent(reciPrivKey);
            Assert.IsTrue(Arrays.AreEqual(expectedData, actualData));
        }
Esempio n. 5
0
        public void TestRfc4134Ex5_2()
        {
            byte[] data = Hex.Decode("5468697320697320736f6d652073616d706c6520636f6e74656e742e");

//			KeyFactory kFact = KeyFactory.GetInstance("RSA");
//			Key key = kFact.generatePrivate(new PKCS8EncodedKeySpec(bobPrivRsaEncrypt));
            AsymmetricKeyParameter key = PrivateKeyFactory.CreateKey(bobPrivRsaEncrypt);

            CmsEnvelopedData ed = new CmsEnvelopedData(rfc4134ex5_2);

            RecipientInformationStore recipients = ed.GetRecipientInfos();

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

            ICollection c = recipients.GetRecipients();
            IEnumerator e = c.GetEnumerator();

            if (e.MoveNext())
            {
                do
                {
                    RecipientInformation recipient = (RecipientInformation)e.Current;

                    if (recipient is KeyTransRecipientInformation)
                    {
                        byte[] recData = recipient.GetContent(key);

                        Assert.IsTrue(Arrays.AreEqual(data, recData));
                    }
                }while (e.MoveNext());
            }
            else
            {
                Assert.Fail("no recipient found");
            }
        }
Esempio n. 6
0
		private void VerifyRecipient(RecipientInformation recipient, AsymmetricKeyParameter privKey)
		{
			Assert.IsTrue(privKey.IsPrivate);

			Assert.AreEqual(recipient.KeyEncryptionAlgOid, PkcsObjectIdentifiers.RsaEncryption.Id);

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

			Assert.IsTrue(Arrays.AreEqual(exContent, recData));
		}