コード例 #1
0
        public void GenerateKeys()
        {
            var results = KeyHelpers.GenerateNewPublicPrivateKeyPair();

            TestContext.WriteLine("Public:\r\n{0}", results.Item1);
            TestContext.WriteLine("Private:\r\n{0}", results.Item2);
        }
コード例 #2
0
        public void ValidateProductKey()
        {
            string privateXmlKey             = KeyHelpers.GetPrivateKey();
            ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey);

            string productKey = keyPublisher.GenerateProductKey(123, 456, 789, "C", "toto", "Company", "*****@*****.**");

            Console.WriteLine(productKey);

            keyPublisher = new ProductKeyPublisher(privateXmlKey);
            ProductKeyInfo result = keyPublisher.ValidateProductKey(productKey);

            var shaHasher     = new SHA1CryptoServiceProvider();
            var usernameHash  = Encoding.ASCII.GetString(shaHasher.ComputeHash(Encoding.ASCII.GetBytes("toto")));
            var companyHash   = Encoding.ASCII.GetString(shaHasher.ComputeHash(Encoding.ASCII.GetBytes("Company")));
            var userEmailHash = Encoding.ASCII.GetString(shaHasher.ComputeHash(Encoding.ASCII.GetBytes("*****@*****.**")));

            Assert.AreEqual(123, result.ProductID);
            Assert.AreEqual(456, result.ProductFeatures);
            Assert.AreEqual(789, result.TrialDays);
            Assert.AreEqual(usernameHash, result.UsernameHash);
            Assert.AreEqual(companyHash, result.CompanyHash);
            Assert.AreEqual(userEmailHash, result.UserEmailHash);
            Assert.AreEqual(DateTime.Now.Date, result.GeneratedDate.Date);
        }
コード例 #3
0
        public void TryRandomKeyGeneration()
        {
            string privateXmlKey             = KeyHelpers.GetPrivateKey();
            ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey);

            string productKey = keyPublisher.GenerateProductKey(0, 0, 0, "", "", "", "");

            Console.WriteLine(productKey);

            keyPublisher.ValidateProductKey(productKey);

            for (int n = 1; n < 0x100; n++)
            {
                byte[] bytes       = BitConverter.GetBytes(n);
                string fakeSegment = Convert.ToBase64String(new byte[] { bytes[2], bytes[1], bytes[0] });
                if (productKey.Length > 6 && fakeSegment != productKey.Substring(6))
                {
                    productKey = String.Format("{0}{1}", fakeSegment, productKey.Substring(5));
                    try
                    {
                        keyPublisher.ValidateProductKey(productKey);
                        Assert.Fail("This should raise an InvalidProductKeyException");
                    }
                    catch (InvalidProductKeyException)
                    {
                        // OK. We are expecting this exception.
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }
コード例 #4
0
        private string GenerateProductKey(short productID, short productFeatures, short trialDays, string clientID, string username, string company, string email)
        {
            string privateXmlKey             = KeyHelpers.GetPrivateKey();
            ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey);

            return(keyPublisher.GenerateProductKey(productID, productFeatures, trialDays, clientID, username, company, email));
        }
コード例 #5
0
        public void LoadSaveLicense()
        {
            string publicXmlKey = KeyHelpers.GetPrivateKey();
            ProductLicenseManager licenseManager = new ProductLicenseManager(null, publicXmlKey,
                                                                             new MockLicenseStore(),
                                                                             new MachineIdentifierProviderMock(),
                                                                             new UserInformationProviderMock()
            {
                Username = "******",
                Company  = "Company",
                Email    = "*****@*****.**"
            });
            ProductLicense license = licenseManager.LoadLicense("MyProductName");

            string productKey       = GenerateProductKey(123, 456, 0, "C", "toto", "Company", "*****@*****.**");
            var    licenseActivated = GetProductActivated(productKey);

            Assert.AreEqual(LicenseStatus.NotFound, license.Status, license.StatusReason);

            license = licenseManager.ActivateProduct(licenseActivated);
            licenseManager.SaveLicense("MyProductName", license);
            license = licenseManager.LoadLicense("MyProductName");

            Assert.AreEqual(LicenseStatus.Licensed, license.Status, license.StatusReason);
            Assert.AreEqual(123, license.ProductID);
            Assert.AreEqual(456, license.ProductFeatures);
            Assert.AreEqual(0, license.TrialDays);
        }
コード例 #6
0
        public void TryValidateWithFakeCryptProvider()
        {
            string privateXmlKey             = KeyHelpers.GetPrivateKey();
            ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey);

            string productKey = keyPublisher.GenerateProductKey(123, 456, 789, "C", "toto", "Company", "*****@*****.**");

            Console.WriteLine(productKey);

            privateXmlKey = (new RSACryptoServiceProvider()).ToXmlString(true);
            keyPublisher  = new ProductKeyPublisher(privateXmlKey);
            ProductKeyInfo result = keyPublisher.ValidateProductKey(productKey);
        }
コード例 #7
0
        public void TryValidateWithPublicKey()
        {
            string privateXmlKey             = KeyHelpers.GetPrivateKey();
            ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey);

            string productKey = keyPublisher.GenerateProductKey(123, 456, 789, "C", "toto", "Company", "*****@*****.**");

            Console.WriteLine(productKey);

            privateXmlKey = KeyHelpers.GetPublicKey();
            keyPublisher  = new ProductKeyPublisher(privateXmlKey);
            ProductKeyInfo result = keyPublisher.ValidateProductKey(productKey);
        }
コード例 #8
0
        public void ActivateProductTest()
        {
            string privateXmlKey             = KeyHelpers.GetPrivateKey();
            ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey);

            string productKey = keyPublisher.GenerateProductKey(123, 456, 789, "C", "toto", "Company", "*****@*****.**");

            var activator   = new ProductActivation(privateXmlKey);
            var licenseInfo = activator.ActivateProduct(productKey, Convert.ToBase64String(new MachineIdentifierProviderMock().MachineHash));

            Assert.IsNotNull(licenseInfo);
            Assert.IsNotNull(licenseInfo.ActivationInfo);
            Assert.IsNotNull(licenseInfo.Signature);
        }
コード例 #9
0
        public void ValidateFakeProductKey()
        {
            string privateXmlKey             = KeyHelpers.GetPrivateKey();
            ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey);

            string productKey = keyPublisher.GenerateProductKey(123, 456, 789, "C", "toto", "Company", "*****@*****.**");

            Console.WriteLine(productKey);
            productKey = productKey.Replace('1', '8');
            productKey = productKey.Replace('2', '7');
            productKey = productKey.Replace('3', '6');
            productKey = productKey.Replace('4', '5');
            Console.WriteLine(productKey);

            keyPublisher = new ProductKeyPublisher(privateXmlKey);
            ProductKeyInfo result = keyPublisher.ValidateProductKey(productKey);
        }
コード例 #10
0
        public void ActivateProductMachineMismatch()
        {
            MachineIdentifierProviderMock machineMock = new MachineIdentifierProviderMock(false);
            string productKey       = GenerateProductKey(123, 456, 0, "C", "toto", "Company", "*****@*****.**");
            var    licenseActivated = GetProductActivated(productKey);

            string publicXmlKey = KeyHelpers.GetPrivateKey();
            ProductLicenseManager licenseManager = new ProductLicenseManager(null, publicXmlKey, null, machineMock,
                                                                             new UserInformationProviderMock()
            {
                Username = "******",
                Company  = "Company",
                Email    = "*****@*****.**"
            });
            ProductLicense license = licenseManager.ActivateProduct(licenseActivated);

            Assert.AreEqual(LicenseStatus.MachineHashMismatch, license.Status, license.StatusReason);
        }
コード例 #11
0
        public void ActivateProductWrongUserInfo()
        {
            string productKey       = GenerateProductKey(123, 456, 0, "C", "toto", "Company", "*****@*****.**");
            var    licenseActivated = GetProductActivated(productKey);

            string publicXmlKey = KeyHelpers.GetPrivateKey();
            ProductLicenseManager licenseManager = new ProductLicenseManager(null, publicXmlKey,
                                                                             new MockLicenseStore(),
                                                                             new MachineIdentifierProviderMock(),
                                                                             new UserInformationProviderMock()
            {
                Username = "******",
                Company  = "Company",
                Email    = "*****@*****.**"
            });
            ProductLicense license = licenseManager.ActivateProduct(licenseActivated);

            Assert.AreEqual(LicenseStatus.Invalid, license.Status, license.StatusReason);
        }
コード例 #12
0
        public void GenerateProductKeyTest()
        {
            string privateXmlKey             = KeyHelpers.GetPrivateKey();
            ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey);

            string productKey1 = keyPublisher.GenerateProductKey(0, 0, 0, "C", "toto", "Company", "*****@*****.**");

            System.Threading.Thread.Sleep(1);
            string productKey2 = keyPublisher.GenerateProductKey(0, 0, 0, "C", "toto", "Company", "*****@*****.**");

            System.Threading.Thread.Sleep(1);
            string productKey3 = keyPublisher.GenerateProductKey(0, 0, 0, "C", "toto", "Company", "*****@*****.**");

            System.Threading.Thread.Sleep(1);
            string productKey4 = keyPublisher.GenerateProductKey(0, 0, 0, "C", "toto", "Company", "*****@*****.**");

            Assert.AreNotEqual(productKey1, productKey2);
            Assert.AreNotEqual(productKey1, productKey3);
            Assert.AreNotEqual(productKey1, productKey4);
            Assert.AreNotEqual(productKey2, productKey3);
            Assert.AreNotEqual(productKey2, productKey4);
            Assert.AreNotEqual(productKey3, productKey4);
        }
コード例 #13
0
        private ProductLicenseInfo GetProductActivatedRealHash(string productKey, string hash)
        {
            string privateXmlKey = KeyHelpers.GetPrivateKey();

            return(new ProductActivation(privateXmlKey).ActivateProduct(productKey, hash));
        }
コード例 #14
0
        public void LoadSaveLicenseRealHash()
        {
            System.Threading.Thread.CurrentThread.CurrentCulture   = System.Globalization.CultureInfo.GetCultureInfo("fr-FR");
            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("fr-FR");

            string publicXmlKey = KeyHelpers.GetPrivateKey();
            ProductLicenseManager licenseManager = new ProductLicenseManager(null, publicXmlKey,
                                                                             new MockLicenseStore(),
                                                                             new MachineIdentifierProvider(null, new Security.Activation.MachineIdentifiers.VolumeInfoIdentifier(null)),
                                                                             new UserInformationProviderMock()
            {
                Username = "******",
                Company  = "Company",
                Email    = "*****@*****.**"
            });

            var hash = licenseManager.GetMachineHash();

            ProductLicense license = licenseManager.LoadLicense("MyProductName");

            string productKey       = GenerateProductKey(123, 456, 0, "C", "toto", "Company", "*****@*****.**");
            var    licenseActivated = GetProductActivatedRealHash(productKey, hash);

            try
            {
                using (StreamWriter writer = new StreamWriter("out.ksk", false))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(ProductLicenseInfo));
                    serializer.Serialize(writer, licenseActivated);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
                return;
            }

            Assert.AreEqual(LicenseStatus.NotFound, license.Status, license.StatusReason);

            KProcess.Ksmed.Security.Activation.ProductLicenseInfo licenseInfo = null;
            try
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader("out.ksk"))
                {
                    var serializer = new System.Xml.Serialization.XmlSerializer(typeof(KProcess.Ksmed.Security.Activation.ProductLicenseInfo));
                    licenseInfo = (KProcess.Ksmed.Security.Activation.ProductLicenseInfo)serializer.Deserialize(reader);
                }
                license = licenseManager.ActivateProduct(licenseInfo);
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }

            license = licenseManager.ActivateProduct(licenseInfo);
            licenseManager.SaveLicense("MyProductName", license);
            license = licenseManager.LoadLicense("MyProductName");

            Assert.AreEqual(LicenseStatus.Licensed, license.Status, license.StatusReason);
            Assert.AreEqual(123, license.ProductID);
            Assert.AreEqual(456, license.ProductFeatures);
            Assert.AreEqual(0, license.TrialDays);
        }
コード例 #15
0
        private ProductLicenseInfo GetProductActivated(string productKey)
        {
            string privateXmlKey = KeyHelpers.GetPrivateKey();

            return(new ProductActivation(privateXmlKey).ActivateProduct(productKey, Convert.ToBase64String(new MachineIdentifierProviderMock().MachineHash)));
        }