public static void ItRoundTrips()
        {
            byte[]           input = new byte[] { 1, 2, 7, 4 };
            MemoryStream     ms    = new MemoryStream(input);
            KeyInfo          keyInfo;
            EncryptionMethod encMethod;
            CipherData       cipherData;
            var keyGen = GeneratorUtilities.GetKeyPairGenerator("RSA");

            keyGen.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
            var pair = keyGen.GenerateKeyPair();

            Encrypt(ms, (RsaKeyParameters)pair.Public, out keyInfo, out encMethod, out cipherData);

            XmlLicenseEncryptedRef decr = new XmlLicenseEncryptedRef();

            decr.AddAsymmetricKey(pair);
            using (var encrypted = new MemoryStream(cipherData.CipherValue))
                using (Stream decrypted = decr.Decrypt(encMethod, keyInfo, encrypted))
                {
                    byte[] decryptedBytes = new byte[decrypted.Length];
                    decrypted.Read(decryptedBytes, 0, (int)decrypted.Length);
                    Assert.Equal(input, decryptedBytes);
                }
        }
        public static void ItDecryptsLicense()
        {
            var keyGen = GeneratorUtilities.GetKeyPairGenerator("RSA");

            keyGen.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
            var pair = keyGen.GenerateKeyPair();

            string expected;
            string encryptedLicenseWithGrants = GenerateLicenseXmlWithEncryptedGrants((RsaKeyParameters)pair.Public, out expected);

            Assert.Contains("hello", expected);
            Assert.DoesNotContain("hello", encryptedLicenseWithGrants);

            XmlNamespaceManager nsManager;
            XmlDocument         toDecrypt = LoadXmlWithLicenseNs(encryptedLicenseWithGrants, out nsManager);

            var decryptor = new XmlLicenseEncryptedRef();
            var transform = new XmlLicenseTransform()
            {
                Decryptor = decryptor,
                Context   = FindLicenseTransformContext(toDecrypt, nsManager)
            };

            decryptor.AddAsymmetricKey(pair);


            transform.LoadInput(null);

            XmlDocument decryptedDoc = transform.GetOutput() as XmlDocument;

            Assert.NotNull(decryptedDoc);
            string decrypted = decryptedDoc.OuterXml;

            Assert.Equal(expected, decrypted);
        }
        private static void EncryptGrant(XmlNode grant, RsaKeyParameters key, XmlNamespaceManager nsMgr)
        {
            using (var ms = new MemoryStream())
                using (var sw = new StreamWriter(ms))
                {
                    sw.Write(grant.InnerXml);
                    sw.Flush();
                    ms.Position = 0;

                    KeyInfo          keyInfo;
                    EncryptionMethod encryptionMethod;
                    CipherData       cipherData;
                    XmlLicenseEncryptedRef.Encrypt(ms, key, out keyInfo, out encryptionMethod, out cipherData);
                    grant.RemoveAll();
                    XmlDocument doc            = grant.OwnerDocument;
                    XmlElement  encryptedGrant = doc.CreateElement("encryptedGrant", LicenseTransformNsUrl);
                    grant.AppendChild(encryptedGrant);

                    encryptedGrant.AppendChild(doc.ImportNode(keyInfo.GetXml(), true));
                    encryptedGrant.AppendChild(doc.ImportNode(encryptionMethod.GetXml(), true));
                    encryptedGrant.AppendChild(doc.ImportNode(cipherData.GetXml(), true));
                }
        }