Ejemplo n.º 1
0
        public static Message DecodeFromString(string messageData)
        {
            JSON message = new JSON();

            //  We need to figure out if this is the compact or one of the JSON encoded versions.
            //  We guess this is going to be based on the first character - either it is a '{' or something else

            if (messageData[0] == '{')
            {
                message = JSON.Parse(messageData);
            }
            else
            {
                //  Split the string based on periods
                string[] rgData = messageData.Split('.');

                if (rgData.Length == 3)
                {
                    message = new JSON();

                    if (rgData[1].Length > 0)
                    {
                        message.Add("payload", rgData[1]);
                    }

                    JSON signature = new JSON();
                    signature.Add("protected", rgData[0]);
                    signature.Add("signature", rgData[2]);

                    JSON sigs = new JSON();
                    sigs.Add(signature);
                    message.Add("signatures", sigs);
                }
                else if (rgData.Length == 5)
                {
                    message = new JSON();
                    message.Add("protected", rgData[0]);
                    message.Add("iv", rgData[2]);
                    message.Add("ciphertext", rgData[3]);
                    message.Add("tag", rgData[4]);

                    JSON recip = new JSON();
                    recip.Add("encrypted_key", rgData[1]);

                    JSON recips = new JSON();
                    recips.Add(recip);

                    message.Add("recipients", recips);
                }
            }

            if (message.ContainsKey("iv"))
            {
                EncryptMessage msgx = new EncryptMessage();
                msgx.DecodeFromJSON(message);
                return(msgx);
            }

            return(new SignMessage(message));
        }
Ejemplo n.º 2
0
        public static Message DecodeFromJSON(JSON message)
        {
            if (message.ContainsKey("iv"))
            {
                EncryptMessage msgx = new EncryptMessage();
                msgx.DecodeFromJSON(message);
                return(msgx);
            }

            return(new SignMessage(message));
        }