dnauck / Portable.Licensing

Portable.Licensing is a cross platform software licensing framework which allows you to implement licensing into your application or library. It provides you all tools to create and validate licenses for your software.
http://dev.nauck-it.de/projects/portable-licensing
MIT License
592 stars 173 forks source link

License Signature Validation Error #35

Open blackchineykh opened 8 years ago

blackchineykh commented 8 years ago

I wrote the below console application trying to test the implementaton of this. I have not been successful as i have been getting the "InvalidSignatureValidationErrorFailure" exception thrown.

I have pasted my code below. I appreciate any help you can give console

     static void Main(string[] args)
    {
        string path = @"C:\Development\Licenses\";
        string fileName = "Licence.lic";

        Console.WriteLine("Enter the application name:");
        string appName = Console.ReadLine();

        Console.WriteLine("Do you wish to create a new License? (Y/N):");
        var createLicense = Console.ReadLine();

        if (createLicense.Equals("Y"))
        {
            Console.WriteLine("Enter the customer company:");
            var company = Console.ReadLine();

            Console.WriteLine("Enter the customer full name:");
            var fullname = Console.ReadLine();

            Console.WriteLine("Enter the customer email:");
            var email = Console.ReadLine();

            Console.WriteLine("Enter the validity days before expiry (number of days):");
            var days = Console.ReadLine();

            Console.WriteLine("Enter the number of allowed uses of this license (numbers only):");
            var count = Console.ReadLine();

            var license = License.New();
            license.LicensedTo(fullname, email);

            if (days != null)
            {
                license = license.ExpiresAt(DateTime.Now.AddDays(Int32.Parse(days)));
            }

            var productFeatures = new Dictionary<string, string>
            {
                {"Version", "1.0"}
            };

            string passphrase = GeneratePassPhraseForProduct();
            var keyPair = GenerateKeysForProduct(passphrase);

            string privateKey = keyPair.ToEncryptedPrivateKeyString(passphrase);
            string publicKey = keyPair.ToPublicKeyString();

            var finalLicense = license.WithUniqueIdentifier(Guid.NewGuid())
           .As(LicenseType.Standard)
           .WithMaximumUtilization(1)
           .WithProductFeatures(productFeatures)
           .CreateAndSignWithPrivateKey(privateKey, passphrase);

            Console.WriteLine("Private key: {0}", privateKey);
            Console.WriteLine("Public key: {0}", publicKey);

            System.IO.File.WriteAllText(path + string.Format("{0}-private-key.txt", appName), privateKey);
            System.IO.File.WriteAllText(path + string.Format("{0}-public-key.txt", appName), publicKey);

            File.WriteAllText(path + fileName, finalLicense.ToString(), Encoding.UTF8);

            Console.WriteLine("License file successfully created");

            Console.WriteLine("Do you wish to validate the licence? (Y/N)");
            var validate = Console.ReadLine();

            if (validate.Equals("Y"))
            {
                using (XmlReader reader = XmlReader.Create(path + fileName))
                {
                    License readLicence = License.Load(reader);

                    var validationFailures = readLicence.Validate()
                      .ExpirationDate()
                           .When(lic => lic.Type == LicenseType.Trial)
                      .And()
                      .Signature(publicKey)
                      .AssertValidLicense().ToList();

                    foreach (var failure in validationFailures)
                        Console.WriteLine(failure.GetType().Name + ": " + failure.Message + " - " + failure.HowToResolve);
                }
            }
        }

        Console.WriteLine("Press any key to exit");

        Console.ReadLine();
    }

    public static KeyPair GenerateKeysForProduct(string PassPhrase)
    {
        if (string.IsNullOrWhiteSpace(PassPhrase))
        {
            throw new Exception("No passphrase was given. Cannot generate keys.");
        }

        var keyGenerator = Portable.Licensing.Security.Cryptography.KeyGenerator.Create();
        var keyPair = keyGenerator.GenerateKeyPair();

        return keyPair;
    }

    public static string GeneratePassPhraseForProduct()
    {
        return Guid.NewGuid().ToString();
    }`
pankajmcait commented 7 years ago

Have anyone find out the solution? i am also facing same error!!!

newky2k commented 4 years ago

I think this is to do with using the XmlReader to load the file after generation. If you use File.ReadAllText(using encoding) it works fine