uruk-project / Jwt

JSON Web Token implementation for .Net & .Net Core
MIT License
85 stars 11 forks source link

System.NotSupportedException: 'Key wrap is not supported for algorithm: 'A256GCM' with a key of type 'EC' and size of 521 bits.' #587

Open zahradardan opened 4 months ago

zahradardan commented 4 months ago

i have error in writer3.WriteTokenString(asyDescriptorPlainText); with exception

System.NotSupportedException: 'Key wrap is not supported for algorithm: 'A256GCM' with a key of type 'EC' and size of 521 bits.'

        string publicKeyStringShahkar = "-******************-";

        TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);

        int secondsSinceEpoch = (int)t.TotalSeconds;

        var asymmetricJwkKey = AsymmetricJwk.FromPem(publicKeyStringShahkar);

        var payload = new
        {
            data = input,
            iat = secondsSinceEpoch
        };

        string jsonPayload = JsonConvert.SerializeObject(payload);

        var asyDescriptorPlainText = new PlaintextJweDescriptor(asymmetricJwkKey,
            KeyManagementAlgorithm.EcdhEsA256KW, EncryptionAlgorithm.A256Gcm)
        {
            Payload = jsonPayload
        };

        // The descriptor sets the 'alg' with value 'A256GCMKW' and 'enc' with value 'A128CBC-HS256'
        var writer3 = new JwtWriter();
        var token3 = writer3.WriteTokenString(asyDescriptorPlainText);
ycrumeyrolle commented 4 months ago

Can you please precise:

Also if you have to get the current time in Epoch time format:

long secondsSinceEpoch = EpochTime.UtcNow;

It is important to use int64 instead of int32 for avoiding the Year 2038 problem.