Esempio n. 1
0
        public static void ItDecryptsLicense()
        {
            using (var key = RSA.Create())
            {
                string expected;
                string encryptedLicenseWithGrants = GenerateLicenseXmlWithEncryptedGrants(key, 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(key);

                // Context is the input for this transform, argument is always ignored
                transform.LoadInput(null);

                XmlDocument decryptedDoc = transform.GetOutput() as XmlDocument;
                Assert.NotNull(decryptedDoc);
                string decrypted = decryptedDoc.OuterXml;
                Assert.Equal(expected, decrypted);
            }
        }
Esempio n. 2
0
        private static void EncryptGrant(XmlNode grant, RSA 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));
                }
        }
Esempio n. 3
0
        public static void ItRoundTrips()
        {
            byte[]           input = new byte[] { 1, 2, 7, 4 };
            MemoryStream     ms    = new MemoryStream(input);
            KeyInfo          keyInfo;
            EncryptionMethod encMethod;
            CipherData       cipherData;

            using (RSA rsa = RSA.Create())
            {
                Encrypt(ms, rsa, out keyInfo, out encMethod, out cipherData);

                XmlLicenseEncryptedRef decr = new XmlLicenseEncryptedRef();
                decr.AddAsymmetricKey(rsa);
                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);
                    }
            }
        }