uruk-project / Jwt

JSON Web Token implementation for .Net & .Net Core
MIT License
82 stars 13 forks source link

JWE with arbitrary bytes/string as payload #458

Closed lochgeo closed 4 years ago

lochgeo commented 4 years ago

Hi Yann,

The RFC 7516, while describing the plaintext does not specify that the content needs to be a valid JWS. I am trying to create a JWE using a string as my content, but I get the error that type (string) is not a valid JwsDescriptor. Below is the code that I am using

       var keyEncryptionKey = Jwk.FromX509Certificate(new X509Certificate2(), false);

        // Creates a JWE descriptor with all its properties
        var descriptor = new JweDescriptor<string>()
        {
            EncryptionKey = keyEncryptionKey,
            EncryptionAlgorithm = EncryptionAlgorithm.Aes256Gcm,
            Algorithm = KeyManagementAlgorithm.RsaOaep,
            Payload = "data"
        };

        // Generates the UTF-8 string representation of the JWT
        var writer = new JwtWriter();
        var token = writer.WriteTokenString(descriptor);

Is it possible to produce a such a JWE using the library? Could you help me with a code sample, if its possible?

ycrumeyrolle commented 4 years ago

Hi,

You can find it in the samples https://github.com/ycrumeyrolle/Jwt/blob/master/samples/BinaryJwtCreationSample/Program.cs for binary data, and https://github.com/ycrumeyrolle/Jwt/blob/master/samples/PlaintextJwtCreationSample/Program.cs for UTF8 string data.

lochgeo commented 4 years ago

Oh.. didn't realise those samples were already present. Many thanks!