Standard.Licensing icon indicating copy to clipboard operation
Standard.Licensing copied to clipboard

Pad Block Corrupted Exception

Open markovejnovic opened this issue 6 years ago • 5 comments

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 avatar Sep 02 '19 21:09 markovejnovic

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

markovejnovic avatar Sep 02 '19 21:09 markovejnovic

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

markovejnovic avatar Sep 02 '19 21:09 markovejnovic

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.

brlauuu avatar Jan 10 '20 12:01 brlauuu

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);
        }
    }
}

davidpetric avatar Jul 27 '20 07:07 davidpetric