Compact Encryption

#264 · closed · 1 comments

View on GitHub ↗

samsopheak

Hi, I'm trying to translate the below JOSE JavaScript code to this JOSE .net package. Original Code ``` import * as jose from 'https://deno.land/x/[email protected]/index.ts' import * as pako from 'https://deno.land/x/[email protected]/pako.js' const exampleShcFromWeb = await fetch("https://spec.smarthealth.cards/examples/example-00-e-file.smart-health-card"); const exampleShcBody = new Uint8Array(await exampleShcFromWeb.arrayBuffer()); const exampleContentType = 'application/smart-health-card' const shlinkPayload = { "key": "rxTgYlOaKJPFtcEd0qcceN8wEU4p94SqAwIWQe6uX7Q", // other properties omitted; not relevant for this example }; const encrypted = await new jose .CompactEncrypt(new Uint8Array(exampleShcBody)) .setProtectedHeader({ alg: 'dir', enc: 'A256GCM', cty: exampleContentType, }) .encrypt(jose.base64url.decode(shlinkPayload.key)); console.log(encrypted) //eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIiwiY3R5IjoiYXBwbGljYXRpb24vc21hcnQtaGVhbHRoLWNhcmQifQ..B9Bd5AW751az-gEx.iah6mxLb5TQe2ZfCwEUs4R1t8WoP0mnFc-TUzN1NIyzUeDwJNOcxv4CY8wV6ys4Dicnr3IhqTvVU1RbR-4eq1GCd4g96faV8_0MbHwXzP246Tz9BDLhQ2zlAjYqvvCi_JuWdyWqGhKeWGX1XibNHFzzVT0FmYensfKF4o0uSeZWQDKVEEhzMSKuALMpUkfwHcmCRfLT-ctANSxq-Zj0IIeT66XbztOomStjlfi-F-FaqBGZfHOARCVvT143CTYELLJCUdD4qUVkrNuLmRZrNuqVpY0g5BjABswkIoDmyoRJAEohuZCamZNA--p-uRqJjRefED1eMrKSppabV2ugaqoFlieujTOE-a3VKib9aC-lFsmLalkwh9ctr_FZqS9H46rqGjGcOxtAXalo1jkMPGupVsE1W-xIH14wbPCYcgfldH9SH7X60462kxD8OFdHpvnnfAvjQnaE4QDqasT5ySpBRtck4GVxs2IRBt62-kOlzoI8lHapLdwIms-Gdt7z38E47ZE3afE4IIbobPGz7wGvjbi3z234ARvGQ4jREgPQb1NRYAEtZlrZNzR6N7ofXD8jF502tw-QWI_Ox0jFP5tynIiMp-hG25ecQ0s4MzPHFC0ZABPamgg3MS-UILl76gMDCHS5Te_JAXZoC1HnkETw5M217SaG5ISAU0F5qETMREfTjZR9E45MDhnw7uY1vo2lffRB3ei1QqGuLh0gUnVU7TUfFYwcOqV15sb0t1lMj0mmyG5v-_dE9H6dYtRKJARltmdfSmc1HisBewx75Xh5ChJQ1hiCEDaZ1wqFjsFJ6SrKgJ7C1N7vx6QKx8YXwFH7ePG2qG39leT5JKZnqAvi9fqc6x-YwfhSjbRKGZoj2o55Fd2fbwtK6CXpiW6AekT7PUcl_7ynTq-DaQ_Yc29WwtmgapcCRNpfcMsoqCD4giu1V3Sj5DQLglwuk1gAMcuV5fo8JpABu2_is83WZ_GJ1WWMUxyZGq6u-EGuZrP96Yewb7-zfnt2lao_LJg1ef5cqDTW7-0MS27wkmLiIi0e-PYvS-UfWVHg1oNbR-MHXMVEQ6gqNg08IgEyPDSFCUbf75HuMILN80bQNtSlFj6FR7uNKHr8sigvKI80k.5flOKKmeqYm0TamwROr8Nw ``` However, I'm not sure I have done correctly. Can you please help review the below code? ``` public void TestSmartLink() { var uri = new Uri("https://spec.smarthealth.cards/examples/example-00-e-file.smart-health-card"); var payload = GetFile(uri); var cty = "application/smart-health-card"; var key = "rxTgYlOaKJPFtcEd0qcceN8wEU4p94SqAwIWQe6uX7Q"; var headers = new Dictionary<string, object>() { { "cty", cty } }; var recipent = new JweRecipient(JweAlgorithm.DIR, Jose.Base64Url.Decode(key)); string token = JWE.EncryptBytes(payload, new[] { recipent }, JweEncryption.A256GCM, mode: SerializationMode.Compact, extraProtectedHeaders: headers); Console.WriteLine(token); Console.ReadLine(); } byte[] GetFile(Uri uri) { using (var client = new HttpClient()) { var response = client.GetAsync(uri).Result; if (response.IsSuccessStatusCode) { return response.Content.ReadAsByteArrayAsync().Result; } else { throw new Exception($"Failed to download file: {response.ReasonPhrase}"); } } } ``` Many Thanks, Sam

Comments

dvsekhvalnov

HI @samsopheak , i don't think you need to use `JWE`, it is designed for multiple recipients. Though it will work too. i would probably just: ```c# // your stuff omited here // ..... // JWT.Encode() will give you compact serialized token string token = Jose.JWT.Encode(payload, Jose.Base64Url.Decode(key), JweAlgorithm.DIR, JweEncryption. A256GCM, extraHeaders: headers); ``` You can also read docs here on DIR alg: https://github.com/dvsekhvalnov/jose-jwt?tab=readme-ov-file#dir-direct-pre-shared-symmetric-key-family-of-algorithms