junian / Standard.Licensing

Easy-to-use licensing library for .NET Framework, Mono, .NET Core, and MAUI / Xamarin products
https://junian.dev/Standard.Licensing/
MIT License
530 stars 120 forks source link

Pad Block Corrupted Exception #7

Open markovejnovic opened 5 years ago

markovejnovic commented 5 years ago

Hello. After reading a private key created with

KeyGenerator keyGen = KeyGenerator.Create();
KeyPair keyPair = keyGen.GenerateKeyPair();
string privateKey = keyPair.ToEncryptedPrivateKeyString(args[Array.IndexOf(args, "--private-key") + 1]);
string publicKey = keyPair.ToPublicKeyString();
File.WriteAllText("privKey.txt", privateKey, Encoding.ASCII);
File.WriteAllText("pubKey.txt", publicKey, Encoding.ASCII);

I am unable to create a license by reading the file:

var utilization = Int32.Parse(args[Array.IndexOf(args, "--license-std") + 3]);
var name = args[Array.IndexOf(args, "--license-std") + 1];
var email = args[Array.IndexOf(args, "--license-std") + 2];
var privKeyLocation = args[Array.IndexOf(args, "--license-std") + 4];
var passPhrase = args[Array.IndexOf(args, "--license-std") + 5];
var privKey = File.ReadAllBytes(privKeyLocation);
var privKeyStr = Encoding.ASCII.GetString(privKey);
ILicenseBuilder license = License.New()
    .WithUniqueIdentifier(Guid.NewGuid())
    .As(LicenseType.Standard)
    .WithMaximumUtilization(utilization)
    .LicensedTo(name, email);
License l = license.CreateAndSignWithPrivateKey(privKeyStr, passPhrase);

due to an exception thrown by BouncyCastle:

Org.BouncyCastle.Crypto.InvalidCipherTextException: 'pad block corrupted'
markovejnovic commented 5 years ago

This seems to be an issue with using a string in CreateAndSignPrivateKey, due to the padding block being truncated as described here.

markovejnovic commented 5 years ago

Worth noting that my key generation and license generation code snippets are two different code blocks.

brlauuu commented 4 years ago

I am storing the license to a file as well and reading it from it after. However, I'm using File.ReadAllText(path) instead of FIle.ReadAllBytes(path) like you are. Maybe it's worth trying like that.

davidpetric commented 4 years ago

Hello,

For anyone who is trying this NuGet I have downloaded the project source code and run the unit test for generating the license and it worked...then I checked my code and I think that Visual Studio used a wrong namespace for the KeyGenerator class.

Here is my code that generates the private and public keys that works:

namespace StandardLicensingLicenseGenerator
{
    using System;

    public class KeysGenerator
    {
        public Tuple<string, string> Generate(string secretPassPhrase)
        {
            var keyGenerator = Standard.Licensing.Security.Cryptography.KeyGenerator.Create();
            var keyPair = keyGenerator.GenerateKeyPair();

            var privateKey = keyPair.ToEncryptedPrivateKeyString(secretPassPhrase);
            var publicKey = keyPair.ToPublicKeyString();

            return new Tuple<string, string>(privateKey, publicKey);
        }
    }
}
mudasar commented 1 month ago

I am facing the same issue every thing was working fine until last year and now i have to generate a new license and all of sudden the keys pad lock broken issue is there. even unable to parse the old licenses (with old keys) base64 error