Пример #1
0
        Test_PKV_licence_key_generation_and_verification_with_random_key_bytes_key_byte_qty_and_verification_key_byte_selection()
        {
            var pkvLicenceKey = new KeyGenerator();

            var pkvKeyCheck = new KeyCheck();

            for (var i = 0; i < 10000; i++)
            {
                var randomKeyByteSetsLength = new Random().Next(2, 400);

                var keyByteSets = new KeyByteSet[randomKeyByteSetsLength];

                for (var j = 0; j < randomKeyByteSetsLength; j++)
                {
                    var random = new Random();

                    var kbs = new KeyByteSet
                              (
                        j + 1,
                        (byte)random.Next(0, 256),
                        (byte)random.Next(0, 256),
                        (byte)random.Next(0, 256)
                              );

                    keyByteSets[j] = kbs;
                }

                // Select a random key byte set to test key verification with

                var kbs1 = keyByteSets[new Random().Next(0, randomKeyByteSetsLength)];
                var kbs2 = keyByteSets[new Random().Next(0, randomKeyByteSetsLength)];

                // The check project also uses a class called KeyByteSet, but with
                // separate name spacing to achieve single self contained dll

                var keyByteSet1 =
                    new KeyByteSet(kbs1.KeyByteNo, kbs1.KeyByteA, kbs1.KeyByteB,
                                   kbs1.KeyByteC); // Change no to test others
                var keyByteSet2 = new KeyByteSet(kbs2.KeyByteNo, kbs2.KeyByteA, kbs2.KeyByteB, kbs2.KeyByteC);

                var seed = new Random().Next(0, int.MaxValue);

                var key = pkvLicenceKey.MakeKey(seed, keyByteSets);

                // Check that check sum validation passes

                Assert.IsTrue(pkvKeyCheck.CheckKeyChecksum(key, keyByteSets.Length));

                // Check using full check method

                Assert.IsTrue(pkvKeyCheck.CheckKey(
                                  key,
                                  new[] { keyByteSet1, keyByteSet2 },
                                  keyByteSets.Length,
                                  null
                                  ) == LicenseKeyResult.KeyGood, "Failed on iteration " + i
                              );
            }
        }
        public void TestPkvLicenceKeyGenerationAndVerificationWithRandomKeyBytesKeyByteQtyAndVerificationKeyByteSelection()
        {
            var pkvLicenceKeyGenerator = new PkvKeyGenerator();

            var pkvKeyVerifier = new PkvKeyVerifier();

            var random = new Random();

            for (int i = 0; i < 10000; i++)
            {
                int randomKeyByteSetsLength = random.Next(2, 400);

                KeyByteSet[] keyByteSets = new KeyByteSet[randomKeyByteSetsLength];

                for (int j = 0; j < randomKeyByteSetsLength; j++)
                {
                    var kbs = new KeyByteSet
                              (
                        j + 1,
                        (byte)random.Next(0, 256),
                        (byte)random.Next(0, 256),
                        (byte)random.Next(0, 256)
                              );

                    keyByteSets[j] = kbs;
                }

                // Select a random key byte set to test key verification with

                KeyByteSet kbs1 = keyByteSets[random.Next(0, randomKeyByteSetsLength)];
                KeyByteSet kbs2 = keyByteSets[random.Next(0, randomKeyByteSetsLength)];

                // The check project also uses a class called KeyByteSet, but with
                // separate name spacing to achieve single self contained dll

                KeyByteSet keyByteSet1 = new KeyByteSet(kbs1.KeyByteNumber, kbs1.KeyByteA, kbs1.KeyByteB, kbs1.KeyByteC); // Change no to test others
                KeyByteSet keyByteSet2 = new KeyByteSet(kbs2.KeyByteNumber, kbs2.KeyByteA, kbs2.KeyByteB, kbs2.KeyByteC);

                int seed = random.Next(0, int.MaxValue);

                string key = pkvLicenceKeyGenerator.MakeKey(seed, keyByteSets);

                // Check that check sum validation passes

                Assert.True(pkvKeyVerifier.CheckKeyChecksum(key, keyByteSets.Length));

                // Check using full check method

                Assert.True(pkvKeyVerifier.VerifyKey(
                                key,
                                new[] { keyByteSet1, keyByteSet2 },
                                keyByteSets.Length,
                                null
                                ) == PkvKeyVerificationResult.KeyIsValid, "Failed on iteration " + i
                            );
            }
        }
        /// <summary>
        ///     Check activation key file for valid licence key or trial period value
        /// </summary>
        /// <param name="activationKeyFileFullPath">The absolute local path where the activation key file exists</param>
        /// <param name="keyByteSet2"> </param>
        /// <param name="licenceKeyFileEncryptionString">An encryption key which the licence key XML will be decrypted with</param>
        /// <param name="friendlyApplicationName">A friendly application name for error reporting e.g. 'My Licenced Application'</param>
        /// <param name="keyByteSet1"> </param>
        public void CheckActivationKeyFile(string activationKeyFileFullPath, KeyByteSet keyByteSet1,
                                           KeyByteSet keyByteSet2, string licenceKeyFileEncryptionString, string friendlyApplicationName)
        {
            if (!File.Exists(activationKeyFileFullPath))
            {
                throw new ActivationKeyFileNotPresentException(
                          "The product activation key file '" + activationKeyFileFullPath + "' was expected at '" +
                          activationKeyFileFullPath +
                          "', but was not found. Please visit the vendor website to obtain a product activation key file.");
            }

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(activationKeyFileFullPath);

            var activationCodeEncrypted = xmlDoc.GetElementsByTagName("Key");

            var activationKeyEncrypted = activationCodeEncrypted[0].InnerText;

            if (!string.IsNullOrEmpty(activationKeyEncrypted))
            {
                string clearTextActivationKey;

                try
                {
                    clearTextActivationKey =
                        ActivationKeyDecryption.Decrypt(activationKeyEncrypted, licenceKeyFileEncryptionString);
                }
                catch
                {
                    throw new ActivationKeyInvalidException(string.Format(
                                                                "The licence key that allows {0} to run is invalid. Please check that the key file exists in the executing directory, and has not been modified.",
                                                                friendlyApplicationName));
                }

                // If a check is made, that means that the free trial build period has expired

                if (clearTextActivationKey.ToLower() == "trial")
                {
                    throw new TrialPeriodExpiredException(string.Format("The trial period for {0} has expired.",
                                                                        friendlyApplicationName));
                }

                var pkvLicenceKeyResult =
                    new KeyCheck().CheckKey(clearTextActivationKey, new[] { keyByteSet1, keyByteSet2 }, 8, null);

                if (pkvLicenceKeyResult != LicenseKeyResult.KeyGood)
                {
                    throw new ActivationKeyInvalidException(string.Format(
                                                                "The licence key that allows {0} to run is invalid. Please check that the key file exists in the executing directory, and has not been modified.",
                                                                friendlyApplicationName));
                }
            }
        }
        public int Compare(object x, object y)
        {
            KeyByteSet kbs1 = (KeyByteSet)x;
            KeyByteSet kbs2 = (KeyByteSet)y;

            if (kbs1.KeyByteNo > kbs2.KeyByteNo)
            {
                return(1);
            }

            if (kbs1.KeyByteNo < kbs2.KeyByteNo)
            {
                return(-1);
            }

            return(0);
        }
        public void TestPkvLicenceKeyGenerationAndVerification()
        {
            var pkvLicenceKeyGenerator = new PkvKeyGenerator();

            var pkvKeyVerifier = new PkvKeyVerifier();

            string key;

            KeyByteSet[] keyByteSets =
            {
                new KeyByteSet(keyByteNumber: 1, keyByteA: 58,  keyByteB: 6,   keyByteC: 97),
                new KeyByteSet(keyByteNumber: 2, keyByteA: 96,  keyByteB: 254, keyByteC: 23),
                new KeyByteSet(keyByteNumber: 3, keyByteA: 11,  keyByteB: 185, keyByteC: 69),
                new KeyByteSet(keyByteNumber: 4, keyByteA: 2,   keyByteB: 93,  keyByteC: 41),
                new KeyByteSet(keyByteNumber: 5, keyByteA: 62,  keyByteB: 4,   keyByteC: 234),
                new KeyByteSet(keyByteNumber: 6, keyByteA: 200, keyByteB: 56,  keyByteC: 49),
                new KeyByteSet(keyByteNumber: 7, keyByteA: 89,  keyByteB: 45,  keyByteC: 142),
                new KeyByteSet(keyByteNumber: 8, keyByteA: 6,   keyByteB: 88,  keyByteC: 32)
            };

            // Change these to a random key byte set from the above array to test key verification with

            KeyByteSet kbs1 = keyByteSets[3];
            KeyByteSet kbs2 = keyByteSets[7];
            KeyByteSet kbs3 = keyByteSets[4];

            // The check project also uses a class called KeyByteSet, but with
            // separate name spacing to achieve single self contained dll

            KeyByteSet keyByteSet1 = new KeyByteSet(kbs1.KeyByteNumber, kbs1.KeyByteA, kbs1.KeyByteB, kbs1.KeyByteC); // Change no to test others
            KeyByteSet keyByteSet2 = new KeyByteSet(kbs2.KeyByteNumber, kbs2.KeyByteA, kbs2.KeyByteB, kbs2.KeyByteC);
            KeyByteSet keyByteSet3 = new KeyByteSet(kbs3.KeyByteNumber, kbs3.KeyByteA, kbs3.KeyByteB, kbs3.KeyByteC);

            var random = new Random();

            for (int i = 0; i < 10000; i++)
            {
                int seed = random.Next(0, int.MaxValue);

                key = pkvLicenceKeyGenerator.MakeKey(seed, keyByteSets);

                // Check that check sum validation passes

                Assert.True(pkvKeyVerifier.CheckKeyChecksum(key, keyByteSets.Length));

                // Check using full check method

                Assert.True(pkvKeyVerifier.VerifyKey(
                                key,
                                new[] { keyByteSet1, keyByteSet2, keyByteSet3 },
                                keyByteSets.Length,
                                null
                                ) == PkvKeyVerificationResult.KeyIsValid, "Failed on iteration " + i
                            );

                // Check that erroneous check sum validation fails

                Assert.False(pkvKeyVerifier.CheckKeyChecksum(key.Remove(23, 1) + "A", keyByteSets.Length)); // Change key by replacing 17th char
            }

            // Check a few random inputs

            Assert.False(pkvKeyVerifier.VerifyKey("adcsadrewf",
                                                  new[] { keyByteSet1, keyByteSet2 },
                                                  keyByteSets.Length,
                                                  null) == PkvKeyVerificationResult.KeyIsValid
                         );
            Assert.False(pkvKeyVerifier.VerifyKey("",
                                                  new[] { keyByteSet1, keyByteSet2 },
                                                  keyByteSets.Length,
                                                  null) == PkvKeyVerificationResult.KeyIsValid
                         );
            Assert.False(pkvKeyVerifier.VerifyKey("123",
                                                  new[] { keyByteSet1, keyByteSet2 },
                                                  keyByteSets.Length,
                                                  null) == PkvKeyVerificationResult.KeyIsValid
                         );
            Assert.False(pkvKeyVerifier.VerifyKey("*()",
                                                  new[] { keyByteSet1, keyByteSet2 },
                                                  keyByteSets.Length,
                                                  null) == PkvKeyVerificationResult.KeyIsValid
                         );
            Assert.False(pkvKeyVerifier.VerifyKey("dasdasdasgdjwqidqiwd21887127eqwdaishxckjsabcxjkabskdcbq2e81y12e8712",
                                                  new[] { keyByteSet1, keyByteSet2 },
                                                  keyByteSets.Length,
                                                  null) == PkvKeyVerificationResult.KeyIsValid
                         );
        }
        public void Test_pkv_licence_key_generation_and_verification()
        {
            var pkvLicenceKey = new PkvLicenceKeyGenerator();

            var pkvKeyCheck = new PkvKeyCheck();

            string key;

            KeyByteSet[] keyByteSets =
            {
                new KeyByteSet(1,  58,   6,  97),
                new KeyByteSet(2,  96, 254,  23),
                new KeyByteSet(3,  11, 185,  69),
                new KeyByteSet(4,   2,  93,  41),
                new KeyByteSet(5,  62,   4, 234),
                new KeyByteSet(6, 200,  56,  49),
                new KeyByteSet(7,  89,  45, 142),
                new KeyByteSet(8,   6,  88, 32)
            };

            // Change these to a random key byte set from the above array to test key verification with

            KeyByteSet kbs1 = keyByteSets[3];
            KeyByteSet kbs2 = keyByteSets[7];
            KeyByteSet kbs3 = keyByteSets[4];

            // The check project also uses a class called KeyByteSet, but with
            // separate name spacing to achieve single self contained dll

            KeyByteSet keyByteSet1 = new KeyByteSet(kbs1.KeyByteNo, kbs1.KeyByteA, kbs1.KeyByteB, kbs1.KeyByteC); // Change no to test others
            KeyByteSet keyByteSet2 = new KeyByteSet(kbs2.KeyByteNo, kbs2.KeyByteA, kbs2.KeyByteB, kbs2.KeyByteC);
            KeyByteSet keyByteSet3 = new KeyByteSet(kbs3.KeyByteNo, kbs3.KeyByteA, kbs3.KeyByteB, kbs3.KeyByteC);

            var random = new Random();

            for (int i = 0; i < 10000; i++)
            {
                int seed = random.Next(0, int.MaxValue);

                key = pkvLicenceKey.MakeKey(seed, keyByteSets);

                // Check that check sum validation passes

                Assert.True(pkvKeyCheck.CheckKeyChecksum(key, keyByteSets.Length));

                // Check using full check method

                Assert.True(pkvKeyCheck.CheckKey(
                                key,
                                new[] { keyByteSet1, keyByteSet2, keyByteSet3 },
                                keyByteSets.Length,
                                null
                                ) == PkvLicenceKeyResult.KeyGood, "Failed on iteration " + i
                            );

                // Check that erroneous check sum validation fails

                Assert.False(pkvKeyCheck.CheckKeyChecksum(key.Remove(23, 1) + "A", keyByteSets.Length)); // Change key by replacing 17th char
            }

            // Check a few random inputs

            Assert.False(pkvKeyCheck.CheckKey("adcsadrewf",
                                              new[] { keyByteSet1, keyByteSet2 },
                                              keyByteSets.Length,
                                              null) == PkvLicenceKeyResult.KeyGood
                         );
            Assert.False(pkvKeyCheck.CheckKey("",
                                              new[] { keyByteSet1, keyByteSet2 },
                                              keyByteSets.Length,
                                              null) == PkvLicenceKeyResult.KeyGood
                         );
            Assert.False(pkvKeyCheck.CheckKey("123",
                                              new[] { keyByteSet1, keyByteSet2 },
                                              keyByteSets.Length,
                                              null) == PkvLicenceKeyResult.KeyGood
                         );
            Assert.False(pkvKeyCheck.CheckKey("*()",
                                              new[] { keyByteSet1, keyByteSet2 },
                                              keyByteSets.Length,
                                              null) == PkvLicenceKeyResult.KeyGood
                         );
            Assert.False(pkvKeyCheck.CheckKey("dasdasdasgdjwqidqiwd21887127eqwdaishxckjsabcxjkabskdcbq2e81y12e8712",
                                              new[] { keyByteSet1, keyByteSet2 },
                                              keyByteSets.Length,
                                              null) == PkvLicenceKeyResult.KeyGood
                         );
        }
Пример #7
0
        private void BtnGen_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtApp.Text) || string.IsNullOrEmpty(txtUser.Text))
            {
                return;
            }

            try
            {
                btnGen.IsEnabled = false;

                var random = new Random();

                if (!Apps.ContainsKey(txtApp.Text))
                {
                    if (txtApp.Text == "CopyTool")
                    {
                        Apps.Add(txtApp.Text, new KeyByteSet[] {
                            new KeyByteSet(1, 177, 99, 0),
                            new KeyByteSet(2, 186, 153, 17),
                            new KeyByteSet(3, 7, 113, 200),
                            //new KeyByteSet(4, 73, 102, 49),
                            //new KeyByteSet(5, 61, 173, 33),
                        });
                    }
                    else
                    {
                        var newKeySet = new KeyByteSet[KeyLength];
                        for (int j = 0; j < KeyLength; j++)
                        {
                            newKeySet[j] = new KeyByteSet(j + 1, (byte)random.Next(0, 256), (byte)random.Next(0, 256), (byte)random.Next(0, 256));
                        }

                        Apps.Add(txtApp.Text, newKeySet);
                    }
                    AppUserMap.Add(txtApp.Text, new Dictionary <string, int>());
                }

                if (!AppUserMap[txtApp.Text].ContainsKey(txtUser.Text))
                {
                    AppUserMap[txtApp.Text].Add(txtUser.Text, AppUserMap[txtApp.Text].Count);
                }

                var pkvLicenceKey = new PkvLicenceKeyGenerator();

                int seed = AppUserMap[txtApp.Text][txtUser.Text];
                // Generate the key
                string key = pkvLicenceKey.MakeKey(seed, Apps[txtApp.Text]);

                var kbs1 = Apps[txtApp.Text][new Random().Next(0, KeyLength - 1)];
                var kbs2 = Apps[txtApp.Text][new Random().Next(0, KeyLength - 1)];

                // The check project also uses a class called KeyByteSet, but with
                // separate name spacing to achieve single self contained dll

                var keyByteSet1 = new KeyByteSet(kbs1.KeyByteNo, kbs1.KeyByteA, kbs1.KeyByteB, kbs1.KeyByteC); // Change no to test others
                var keyByteSet2 = new KeyByteSet(kbs2.KeyByteNo, kbs2.KeyByteA, kbs2.KeyByteB, kbs2.KeyByteC);

                txtLicense.Text = key;

                var partialKeys = new[] { keyByteSet1, keyByteSet2 };

                CheckKey(key, KeyLength, partialKeys);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Sorry! cannot generate license key!");
            }
            finally
            {
                btnGen.IsEnabled = true;
            }
        }