public void roundTrip()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), false);
            msg.SetContent(strContent);
            msg.Encrypt(rgbKey128);
            byte[] rgbMsg = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgbMsg);
            msg.Decrypt(rgbKey128);

            Assert.AreEqual <string>(msg.GetContentAsString(), strContent);
        }
        public void roundTripDetached()
        {
            EncryptMessage msg = new EncryptMessage(true, false);

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), false);
            msg.SetContent(strContent);
            msg.Encrypt(rgbKey128);

            byte[] content = msg.GetEncryptedContent();

            byte[] rgb = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgb);
            msg.SetEncryptedContent(content);
            msg.Decrypt(rgbKey128);
        }
        public void nullKeyForDecrypt()
        {
            EncryptMessage msg = new EncryptMessage(true, true);

            //        thrown.expect(CoseException.class);
            //        thrown.expectMessage("No Encrypted Content Specified");

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), false);
            msg.SetContent(strContent);
            msg.Encrypt(rgbKey128);

            byte[] rgb = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgb);
            msg.Decrypt(null);
        }
示例#4
0
        public void roundTrip()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.UNPROTECTED);
            msg.SetContent(strContent);
            Recipient r = new Recipient(key128, AlgorithmValues.Direct);

            msg.AddRecipient(r);
            msg.Encrypt();
            byte[] rgbMsg = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgbMsg);
            r   = msg.RecipientList[0];
            r.SetKey(key128);
            msg.Decrypt(r);

            Assert.AreEqual <string>(msg.GetContentAsString(), strContent);
        }
示例#5
0
        static void CheckMessage(Message msg, JWK key, CBORObject input)
        {
            if (msg.GetType() == typeof(EncryptMessage))
            {
                EncryptMessage enc = (EncryptMessage)msg;

                Recipient recipient = enc.RecipientList[0];
                recipient.SetKey(key);

                try {
                    enc.Decrypt(recipient);
                }
                catch (Exception e) { Console.WriteLine("Failed to decrypt " + e.ToString()); return; }

                if (enc.GetContentAsString() != input["plaintext"].AsString())
                {
                    Console.WriteLine("Plain text does not match");
                }
            }
            else if (msg.GetType() == typeof(SignMessage))
            {
                SignMessage sig = (SignMessage)msg;

                try {
                    try {
                        sig.GetContentAsString();
                    }
                    catch (System.Exception) {
                        sig.SetContent(input["payload"].AsString());
                    }
                    sig.Validate(key);

                    if (sig.GetContentAsString() != input["payload"].AsString())
                    {
                        Console.WriteLine("Plain text does not match");
                    }
                }
                catch (Exception e) { Console.WriteLine("Failed to verify " + e.ToString()); return; }
            }
        }
示例#6
0
        public void roundTripDetached()
        {
            EncryptMessage msg = new EncryptMessage(true, false);

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.UNPROTECTED);
            msg.SetContent(strContent);
            Recipient r = new Recipient(key128, AlgorithmValues.Direct);

            msg.AddRecipient(r);
            msg.Encrypt();

            byte[] content = msg.GetEncryptedContent();

            byte[] rgb = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgb);
            msg.SetEncryptedContent(content);
            r = msg.RecipientList[0];
            r.SetKey(key128);
            msg.Decrypt(r);
        }
示例#7
0
        public void noContentForDecrypt()
        {
            EncryptMessage msg = new EncryptMessage(true, false);

            //        thrown.expect(CoseException.class);
            //        thrown.expectMessage("No Enveloped Content Specified");

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.UNPROTECTED);
            msg.SetContent(strContent);
            Recipient r = new Recipient(key128, AlgorithmValues.Direct);

            msg.AddRecipient(r);
            msg.Encrypt();

            byte[] rgb = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgb);
            r   = msg.RecipientList[0];
            r.SetKey(key128);
            msg.Decrypt(r);
        }
示例#8
0
        static void CheckMessage(Message msg, Key key, JSON input)
        {
            if (msg.GetType() == typeof(EncryptMessage))
            {
                EncryptMessage enc = (EncryptMessage)msg;

                try {
                    enc.Decrypt(key);
                }
                catch (Exception e) { Console.WriteLine("Failed to decrypt " + e.ToString()); return; }

                if (enc.GetContentAsString() != input["plaintext"].AsString())
                {
                    Console.WriteLine("Plain text does not match");
                }
            }
            else if (msg.GetType() == typeof(SignMessage))
            {
                SignMessage sig = (SignMessage)msg;

                try {
                    try {
                        sig.GetContentAsString();
                    }
                    catch (System.Exception) {
                        sig.SetContent(input["payload"].AsString());
                    }
                    sig.Verify(key);

                    if (sig.GetContentAsString() != input["payload"].AsString())
                    {
                        Console.WriteLine("Plain text does not match");
                    }
                }
                catch (Exception e) { Console.WriteLine("Failed to verify " + e.ToString()); return; }
            }
        }
示例#9
0
        public void TestRoundTrip3()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.PROTECTED);
            msg.SetContent(rgbContent);
            msg.AddRecipient(recipient128);
            msg.Encrypt();

            List <Recipient> rList = msg.RecipientList;

            Assert.AreEqual(rList.Count(), (1));

            CBORObject rgbMsg = msg.EncodeToCBORObject();

            msg = (EncryptMessage)Message.DecodeFromCBOR(rgbMsg, Tags.Encrypt);
            Recipient r = msg.RecipientList[0];

            r.SetKey(cnKey128);
            byte[] contentNew = msg.Decrypt(r);

            CollectionAssert.AreEqual(contentNew, (rgbContent));
        }