0

I want to encrypt my JSON object into a JWE token. I am using the JOSE NuGet SDK with C# language. I am using A256KW JweAlgorithm for secret key and A256GCM for JweEncryption. Could anyone please help me find out what I am doing wrong?

string key = "Some Key";

// Create Security key  using private key above:
// not that latest version of JWT using Microsoft namespace instead of System

var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));

//Some PayLoad that contain information about the  customer
var payload = new JwtPayload
                   {
                     { "page ", "2 "},
                     { "sc", "http://dummy.com/"},
                   };

var payload1 = Newtonsoft.Json.JsonConvert.SerializeObject(anyPayJson);
           

string token_1 = JWE.Encrypt(payload1, new[] { new JweRecipient(JweAlgorithm.A256KW, key, null) }, JweEncryption.A256GCM);

I am new in JOSE JWT and have no idea how to use JWE algo to the wrap secret key. I am getting error while processing it:

AesKeyWrap management algorithm expects key to be byte[] array.

jps
  • 20,041
  • 15
  • 75
  • 79
  • The key used in the `Encrypt()` call or `JweRecipient()`-ctor must not be a string, but a `byte[]`, which must be 32 bytes large because of `A256KW`. – Topaco Dec 06 '21 at 11:42
  • @Topaco if i make change in JweRecipient()-ctor to string token_1 = JWE.Encrypt(payload1, new[] { new JweRecipient(JweAlgorithm.A256KW, Encoding.UTF8.GetBytes(key), null) }, JweEncryption.A256GCM); the it gives me "AesKeyWrap management algorithm expected key of size 256 bits, but was given 344 bits". – Kryp2 Gemini Dec 06 '21 at 11:52
  • As already said, you need a 32 bytes = 256 bits key. Maybe your key is just wrong. Or it is encoded and must be decoded. Without an example key only guessing is possible. – Topaco Dec 06 '21 at 12:01
  • @Topaco my key is "bXF9p18KmVjgyzv3lP6otbne1W8PLo6gEE287SMyjeI" – Kryp2 Gemini Dec 06 '21 at 12:08
  • This is a Base64 encoded key, you have to Base64 decode it (e.g. `Convert.FromBase64String()`). You may need to add a padding byte (`=`) to the end of the string. – Topaco Dec 06 '21 at 12:16
  • @Topaco Thanks for your help I am able to get a response from that method by using padding byte (=), but my problem is not resolved yet I am expecting a response in the below format it looks like jtw token "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIn0.Hwv8rJUIcWzZgfAe1g8QaXrcczAx2- lvQX0gighE8OPg6L8-L9YyikY4Xx2oWUr2mbsHnS7nbT1dJ59Nz-CpdUk5JwC--Qml.e1rlkqkDbe3yRusnTiDzMw.ddSHOBVCIpU4-jSCqpGbtAsOuDBJnjnP4xFU97TeBOghlk3quvTd0lvkunDNvKOSqlw0zi2Gtz9Y4lZNPVEyYTkqGprjqMFGulcI_0gKzyu7CaFzJjWBicspIo81ljPdwkodNnfjwnuGjEIj5UUgJHcebEaFNDVqgU4Gtsvn9g7LOHVhmGXLOzlNcRbbgp.SQqu8k5C7QoYP0uXSV6H42z ..." – Kryp2 Gemini Dec 06 '21 at 13:42
  • AnyPayJson anyPayJson = new AnyPayJson(); anyPayJson.page = 2; anyPayJson.filters = new Filters(); anyPayJson.filters.startDate = DateTime.Now.Date; anyPayJson.filters.endDate = DateTime.Now.Date; string key = "bXF9p18KmVjgyzv3lP6otbne1W8PLo6gEE287SMyjeI="; var payload1 = Newtonsoft.Json.JsonConvert.SerializeObject(anyPayJson); string token = JWE.Encrypt(payload1, new[] { new JweRecipient(JweAlgorithm.A256KW, Convert.FromBase64String(key), null) }, JweEncryption.A256GCM); – Kryp2 Gemini Dec 06 '21 at 13:44
  • Don't post code in comments, edit your answer instead. Just specify the format as described in the documentation: `SerializationMode.Compact`. If you have further problems, please post a new question. – Topaco Dec 06 '21 at 13:51
  • @Topaco Ok Sure – Kryp2 Gemini Dec 06 '21 at 14:00

0 Answers0