示例#1
0
        public void EncryptCompressed()
        {
            string         msg            = "Ths is some content";
            EncryptMessage encryptMessage = new EncryptMessage();

            encryptMessage.SetContent(msg);
            JWK encryptionKey = JWK.GenerateKey("A128GCM");

            // encryptMessage.AddAttribute(HeaderKeys.EncryptionAlgorithm, CBORObject.FromObject(EncryptionAlgorithm), Attributes.PROTECTED);

            Recipient recipient = new Recipient(encryptionKey);

            encryptMessage.AddRecipient(recipient);
            // recipient.ClearUnprotected();
            if (recipient.RecipientType == RecipientType.Direct && encryptionKey.ContainsName("alg"))
            {
                encryptMessage.AddAttribute("enc", encryptionKey.AsString("alg"), Attributes.PROTECTED);
            }
            else
            {
                encryptMessage.AddAttribute("enc", "A128GCM", Attributes.PROTECTED);
            }

            msg = encryptMessage.EncodeCompressed();
        }
示例#2
0
        static void BuildCompact(CBORObject control, JwkSet keys)
        {
            //  Encrypted or Signed?
            if (control.ContainsKey("signing"))
            {
                SignMessage sign   = new SignMessage();
                Signer      signer = new Signer(keys[0]);

                sign.SetContent(control["input"]["payload"].AsString());
                sign.AddSigner(signer);

                CBORObject xx = control["signing"]["protected"];
                foreach (CBORObject key in xx.Keys)
                {
                    signer.AddAttribute(key, xx[key], Attributes.PROTECTED);
                }

                string output = sign.EncodeCompressed();

                Message msg = Message.DecodeFromString(output);

                CheckMessage(msg, keys[0], control["input"]);
            }
            else if (control.ContainsKey("encrypting_key"))
            {
                EncryptMessage enc = new EncryptMessage();
                CBORObject     xx  = control["encrypting_content"]["protected"];
                foreach (CBORObject key in xx.Keys)
                {
                    enc.AddAttribute(key, xx[key], Attributes.PROTECTED);
                }

                Recipient recip = new Recipient(keys[0], control["input"]["alg"].AsString(), enc);

                enc.AddRecipient(recip);
                enc.SetContent(control["input"]["plaintext"].AsString());

                string output = enc.EncodeCompressed();

                Message msg = Message.DecodeFromString(output);

                CheckMessage(msg, keys[0], control["input"]);
            }
        }