Example #1
0
        public static void BasicEncryptionDecryptionTests()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");

            string enc = pc.EncryptOrDecrypt(true, "Pubnub Messaging API 1");
            Console.WriteLine ("Pubnub Messaging API 1 = " + enc);
            Console.WriteLine ("dec = " + pc.EncryptOrDecrypt(false, enc));

            enc = pc.EncryptOrDecrypt(true, "yay!");
            Console.WriteLine ("yay = " + enc);
            Console.WriteLine ("dec = " + pc.EncryptOrDecrypt(false, enc));

            Console.WriteLine ("Wi24KS4pcTzvyuGOHubiXg==: = " + pc.EncryptOrDecrypt(false, "Wi24KS4pcTzvyuGOHubiXg=="));
            Console.WriteLine ("f42pIQcWZ9zbTbH8cyLwB/tdvRxjFLOYcBNMVKeHS54=: = " + pc.EncryptOrDecrypt(false, "f42pIQcWZ9zbTbH8cyLwB/tdvRxjFLOYcBNMVKeHS54="));
            Console.WriteLine ("f42pIQcWZ9zbTbH8cyLwByD/GsviOE0vcREIEVPARR0=: = " + pc.EncryptOrDecrypt(false, "f42pIQcWZ9zbTbH8cyLwByD/GsviOE0vcREIEVPARR0="));
            Console.WriteLine ("zMqH/RTPlC8yrAZ2UhpEgLKUVzkMI2cikiaVg30AyUu7B6J0FLqCazRzDOmrsFsF = " + pc.EncryptOrDecrypt(false, "zMqH/RTPlC8yrAZ2UhpEgLKUVzkMI2cikiaVg30AyUu7B6J0FLqCazRzDOmrsFsF"));
            Console.WriteLine ("GsvkCYZoYylL5a7/DKhysDjNbwn+BtBtHj2CvzC4Y4g= = " + pc.EncryptOrDecrypt(false, "GsvkCYZoYylL5a7/DKhysDjNbwn+BtBtHj2CvzC4Y4g="));

            Console.WriteLine ("IDjZE9BHSjcX67RddfCYYg== = " + pc.EncryptOrDecrypt(false, "IDjZE9BHSjcX67RddfCYYg=="));
            Console.WriteLine ("Ns4TB41JjT2NCXaGLWSPAQ== = " + pc.EncryptOrDecrypt(false, "Ns4TB41JjT2NCXaGLWSPAQ=="));

            Console.WriteLine ("+BY5/miAA8aeuhVl4d13Kg== = " + pc.EncryptOrDecrypt(false, "+BY5/miAA8aeuhVl4d13Kg=="));

            Console.WriteLine ("Zbr7pEF/GFGKj1rOstp0tWzA4nwJXEfj+ezLtAr8qqE= = " + pc.EncryptOrDecrypt(false, "Zbr7pEF/GFGKj1rOstp0tWzA4nwJXEfj+ezLtAr8qqE="));
            Console.WriteLine ("q/xJqqN6qbiZMXYmiQC1Fw==: = " + pc.EncryptOrDecrypt(false, "q/xJqqN6qbiZMXYmiQC1Fw=="));
        }
Example #2
0
        public void TestGermanCharsDecryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            string strMessage= "stpgsG1DZZxb44J7mFNSzg==";
            JavaScriptSerializer js = new JavaScriptSerializer();
            //decrypt
            string dec = pc.EncryptOrDecrypt(false, strMessage);
            //deserialize
            strMessage= js.Deserialize<string>(dec);

            Assert.AreEqual("ÜÖ", strMessage);
        }
Example #3
0
        public void TestArrayEncryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //create an empty array object
            object [] objArr = {};
            //serialize
            JavaScriptSerializer js = new JavaScriptSerializer();
            string strArr = js.Serialize(objArr);
            //Encrypt
            string enc = pc.EncryptOrDecrypt(true, strArr);

            Assert.AreEqual("Ns4TB41JjT2NCXaGLWSPAQ==", enc);
        }
Example #4
0
 public void TestArrayDecryption()
 {
     PubnubCrypto pc = new PubnubCrypto("enigma");
     //Input the deserialized string
     string strMessage= "Ns4TB41JjT2NCXaGLWSPAQ==";
     //decrypt
     string dec = pc.EncryptOrDecrypt(false, strMessage);
     //create a serialized object
     object [] objArr = {};
     JavaScriptSerializer js = new JavaScriptSerializer();
     string res = js.Serialize(objArr);
     //compare the serialized object and the return of the Decrypt method
     Assert.AreEqual(res, dec);
 }
Example #5
0
        /**
         * Publish
         *
         * Send a message to a channel
         *
         * @param String channel name.
         * @param List<object> info.
         * @return bool false on fail
         */
        public bool publish(string channel, object message)
        {
            if (this.PUBLISH_KEY.Length == 0) return false;
            // Generate String to Sign
            string signature = "0";
            JavaScriptSerializer ser = new JavaScriptSerializer();
            if (this.SECRET_KEY.Length > 0)
            {
                StringBuilder string_to_sign = new StringBuilder();
                string_to_sign
                    .Append(this.PUBLISH_KEY)
                    .Append('/')
                    .Append(this.SUBSCRIBE_KEY)
                    .Append('/')
                    .Append(this.SECRET_KEY)
                    .Append('/')
                    .Append(channel)
                    .Append('/')
                    .Append(ser.Serialize(message))
                    //.Append(SerializeToJsonString(message)); // 1

                ;
                // Sign Message
                signature = md5(string_to_sign.ToString());
            }
            //string message_org = message.ToString();
            if (this.CIPHER_KEY.Length > 0)
            {
                PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY);
                //serialize the message
                message = ser.Serialize(message);
                //encrypt and encode
                message = aes.encrypt(message.ToString());
            }

            // Build URL
            List<string> url = new List<string>();
            url.Add("publish");
            url.Add(this.PUBLISH_KEY);
            url.Add(this.SUBSCRIBE_KEY);
            url.Add(signature);
            url.Add(channel);
            url.Add("0");
            //url.Add(SerializeToJsonString(message));
            url.Add(ser.Serialize(message));

            return _request(url, ResponseType.Publish);
        }
Example #6
0
 public void TestYayEncryptionBasic()
 {
     PubnubCrypto pc = new PubnubCrypto("enigma");
     //deserialized string
     string strMessage= "yay!";
     //Encrypt
     string enc = pc.EncryptOrDecrypt(true, strMessage);
     Assert.AreEqual("q/xJqqN6qbiZMXYmiQC1Fw==", enc);
 }
Example #7
0
 public void TestYayDecryptionBasic()
 {
     PubnubCrypto pc = new PubnubCrypto("enigma");
     string strMessage= "q/xJqqN6qbiZMXYmiQC1Fw==";
     //decrypt
     string dec = pc.EncryptOrDecrypt(false, strMessage);
     //deserialize again
     Assert.AreEqual("yay!", dec);
 }
Example #8
0
        public void TestUnicodeCharsEncryption()
        {
            /*string unicodeString = "漢語";

            Console.WriteLine( unicodeString );

            string encoded = EncodeNonAsciiCharacters(unicodeString);
            Console.WriteLine( encoded );

            string decoded = DecodeEncodedNonAsciiCharacters( encoded );
            Console.WriteLine( decoded );*/

            PubnubCrypto pc = new PubnubCrypto("enigma");
            string strMessage= "漢語";

            JavaScriptSerializer js = new JavaScriptSerializer();
            strMessage= js.Serialize(strMessage);
            Console.WriteLine(strMessage);
            string enc = pc.EncryptOrDecrypt(true, strMessage);
            Console.WriteLine(enc);
            Assert.AreEqual("+BY5/miAA8aeuhVl4d13Kg==", enc);
        }
Example #9
0
        public void TestStuffCanEncryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //input serialized string
            string strMessage= "{\"this stuff\":{\"can get\":\"complicated!\"}}";
            //encrypt
            string enc = pc.EncryptOrDecrypt(true, strMessage);

            Assert.AreEqual("zMqH/RTPlC8yrAZ2UhpEgLKUVzkMI2cikiaVg30AyUu7B6J0FLqCazRzDOmrsFsF", enc);
        }
Example #10
0
        public void TestPubNubEncryption1()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //non serialized string
            string strMessage= "Pubnub Messaging API 1";
            //serialize
            JavaScriptSerializer js = new JavaScriptSerializer();
            strMessage= js.Serialize(strMessage);
            //encrypt
            string enc = pc.EncryptOrDecrypt(true, strMessage);

            Assert.AreEqual("f42pIQcWZ9zbTbH8cyLwByD/GsviOE0vcREIEVPARR0=", enc);
        }
Example #11
0
 public void TestPubNubDecryption1()
 {
     PubnubCrypto pc = new PubnubCrypto("enigma");
     //deserialized string
     string strMessage= "f42pIQcWZ9zbTbH8cyLwByD/GsviOE0vcREIEVPARR0=";
     JavaScriptSerializer js = new JavaScriptSerializer();
     //decrypt
     string dec = pc.EncryptOrDecrypt(false, strMessage);
     //deserialize
     strMessage= js.Deserialize<string>(dec);
     Assert.AreEqual("Pubnub Messaging API 1", strMessage);
 }
Example #12
0
        /// <summary>
        /// Gets the result by wrapping the json response based on the request
        /// </summary>
        /// <param name="type"></param>
        /// <param name="jsonString"></param>
        /// <param name="url_components"></param>
        /// <returns></returns>
        private List<object> WrapResultBasedOnResponseType(ResponseType type, string jsonString, List<string> url_components, bool reconnect)
        {
            List<object> result = new List<object>();
            string channelName = getChannelName(url_components, type);

            JavaScriptSerializer jS = new JavaScriptSerializer();
            //mod for monomac
            /*result = jS.Deserialize<List<object>>(jsonString) as List<object>;

            if (result != null && result.Count > 0 && result[0] is object[])
            {
                result[0] = decodeMsg((object[])result[0], type);
            }*/
            //mod for monomac
            result = (List<object>)jS.Deserialize<List<object>>(jsonString);
            var resultOccupancy = jS.DeserializeObject(jsonString.ToString());

            if ((result.Count != 0) && (type != ResponseType.DetailedHistory))
            {
                if (result[0] is object[])
                {
                    foreach (object message in (object[])result[0])
                    {
                         //this.ReturnMessage = message;
                        result[0] = message;
                    }
                }
            }

            switch (type)
            {
                case ResponseType.Publish:
                    result.Add(channelName);
                    _publishMsg.AddOrUpdate(channelName, result, (key, oldValue) => result);
                    break;
                case ResponseType.History:
                    if (this.CIPHER_KEY.Length > 0)
                    {
                        List<object> historyDecrypted = new List<object>();
                        PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY);
                        foreach (object message in result)
                        {
                            //mod for monomac
                            //historyDecrypted.Add(aes.decrypt(message.ToString()));
                            string strDecrypted = aes.decrypt(message.ToString());
                            string strDecoded = jS.Deserialize<string>(strDecrypted);
                            historyDecrypted.Add(strDecoded);
                        }
                        History = historyDecrypted;
                    }
                    else
                    {
                        History = result;
                    }
                    break;
                case ResponseType.DetailedHistory:
                //mod for monomac
                    if (this.CIPHER_KEY.Length > 0)
                    {
                        List<object> historyDecrypted = new List<object>();
                        PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY);
                        var myObjectArray = (from item in result select item as object).ToArray();
                        IEnumerable enumerable = myObjectArray[0] as IEnumerable;
                        if (enumerable != null)
                        {
                            foreach(object element in enumerable)
                            {
                                string strDecrypted = aes.decrypt(element.ToString());
                                string strDecoded = jS.Deserialize<string>(strDecrypted);
                                historyDecrypted.Add(strDecoded);
                            }
                        }
                        result = historyDecrypted;
                    }
                    else
                    {
                        //DetailedHistory = (object[])(result[0]);
                        List<object> historyDecrypted = new List<object>();
                        var myObjectArray = (from item in result select item as object).ToArray();
                        IEnumerable enumerable = myObjectArray[0] as IEnumerable;
                        if (enumerable != null)
                        {
                            foreach(object element in enumerable)
                            {
                                historyDecrypted.Add(element.ToString());
                            }
                        }
                        result = historyDecrypted;
                    }
                    result.Add(channelName);
                   break;
                case ResponseType.Here_Now:
                    //mod for monomac
                    //var resultOccupancy = jS.DeserializeObject(jsonString);
                    Dictionary<string, object> dic = (Dictionary<string, object>)resultOccupancy;
                    result = new List<object>();
                    result.Add(dic);
                    result.Add(channelName);
                    break;
                case ResponseType.Time:
                    Time = result;
                    break;
                case ResponseType.Subscribe:
                    result.Add(channelName);
                    _subscribeMsg.AddOrUpdate(channelName, result, (key, oldValue) =>
                                                {
                                                    if (reconnect)
                                                    {
                                                        List<object> oldResult = oldValue as List<object>;
                                                        if (oldResult != null)
                                                        {
                                                            result[1] = oldResult[1];
                                                        }
                                                        return result;
                                                    }
                                                    else
                                                    {
                                                        return result;
                                                    }
                                                });
                    break;
                case ResponseType.Presence:
                    result.Add(channelName);
                    _presenceMsg.AddOrUpdate(channelName, result, (key, oldValue) =>
                                                {
                                                    if (reconnect)
                                                    {
                                                        List<object> oldResult = oldValue as List<object>;
                                                        if (oldResult != null)
                                                        {
                                                            result[1] = oldResult[1];
                                                        }
                                                        return result;
                                                    }
                                                    else
                                                    {
                                                        return result;
                                                    }
                                                });

                    break;
                default:
                    break;
            };//switch stmt end

            return result;
        }
Example #13
0
        private string jsonEncodePublishMsg(object originalMsg)
        {
            string msg = SerializeToJsonString(originalMsg);

            if (this.CIPHER_KEY.Length > 0)
            {
                PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY);
                string encryptMsg = aes.encrypt(msg);
                msg = SerializeToJsonString(encryptMsg);
            }

            return msg;
        }
Example #14
0
 private object[] decodeDecryptLoop(object[] messageArray)
 {
     if (this.CIPHER_KEY.Length > 0)
     {
         List<object> receivedMsg = new List<object>();
         foreach (object item in messageArray)
         {
             PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY);
             string decryptMsg = aes.decrypt(item.ToString());
             receivedMsg.Add(decryptMsg);
         }
         return receivedMsg.ToArray();
     }
     else
     {
         return messageArray;
     }
 }
Example #15
0
        /**
         * Publish
         *
         * Send a message to a channel
         *
         * @param String channel name.
         * @param List<object> info.
         * @return bool false on fail
         */
        public bool publish(string channel, object message, Action<object> usercallback)
        {
            if (string.IsNullOrWhiteSpace(channel) || message == null)
            {
                throw new ArgumentException("Missing Channel or Message");
            }

            //TODO: Should we validate at constructor level
            if (this.PUBLISH_KEY.Length == 0)
            {
                throw new MissingFieldException("PUBLISH_KEY cannot be empty for publish");
            }

            // Generate String to Sign
            string signature = "0";
            //mod for monomac
            /*string msg = jsonEncodePublishMsg(message);

            if (this.SECRET_KEY.Length > 0)
            {
                StringBuilder string_to_sign = new StringBuilder();
                string_to_sign
                    .Append(this.PUBLISH_KEY)
                    .Append('/')
                    .Append(this.SUBSCRIBE_KEY)
                    .Append('/')
                    .Append(this.SECRET_KEY)
                    .Append('/')
                    .Append(channel)
                    .Append('/')
                    .Append(msg); // 1

                // Sign Message
                signature = md5(string_to_sign.ToString());
            }

            // Build URL
            List<string> url = new List<string>();
            url.Add("publish");
            url.Add(this.PUBLISH_KEY);
            url.Add(this.SUBSCRIBE_KEY);
            url.Add(signature);
            url.Add(channel);
            url.Add("0");
            url.Add(msg);*/

            JavaScriptSerializer ser = new JavaScriptSerializer();
            if (this.SECRET_KEY.Length > 0)
            {
                StringBuilder string_to_sign = new StringBuilder();
                string_to_sign
                    .Append(this.PUBLISH_KEY)
                    .Append('/')
                    .Append(this.SUBSCRIBE_KEY)
                    .Append('/')
                    .Append(this.SECRET_KEY)
                    .Append('/')
                    .Append(channel)
                    .Append('/')
                    .Append(ser.Serialize(message))
                    //.Append(SerializeToJsonString(message)); // 1

                ;
                // Sign Message
                signature = md5(string_to_sign.ToString());
            }
            //string message_org = message.ToString();
            if (this.CIPHER_KEY.Length > 0)
            {
                PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY);
                //serialize the message
                message = ser.Serialize(message);
                //encrypt and encode
                message = aes.encrypt(message.ToString());
            }

            // Build URL
            List<string> url = new List<string>();
            url.Add("publish");
            url.Add(this.PUBLISH_KEY);
            url.Add(this.SUBSCRIBE_KEY);
            url.Add(signature);
            url.Add(channel);
            url.Add("0");
            //url.Add(SerializeToJsonString(message));
            url.Add(ser.Serialize(message));
            return _urlRequest(url, ResponseType.Publish,usercallback,false);
        }
Example #16
0
        public void TestObjectDecryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //Deserialized
            string strMessage= "IDjZE9BHSjcX67RddfCYYg==";
            //Decrypt
            string dec = pc.EncryptOrDecrypt(false, strMessage);
            //create an object
            Object obj = new Object();
            //Serialize the object
            JavaScriptSerializer js = new JavaScriptSerializer();
            string res = js.Serialize(obj);

            Assert.AreEqual(res, dec);
        }
Example #17
0
        public void TestObjectEncryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //create an object
            Object obj = new Object();
            //serialize
            JavaScriptSerializer js = new JavaScriptSerializer();
            string strObj = js.Serialize(obj);
            //encrypt
            string enc = pc.EncryptOrDecrypt(true, strObj);

            Assert.AreEqual("IDjZE9BHSjcX67RddfCYYg==", enc);
        }
Example #18
0
        public void TestCipher()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");

            string strCipher = pc.GetEncryptionKey();

            Assert.AreEqual("67a4f45f0d1d9bc606486fc42dc49416", strCipher);
        }
Example #19
0
 public void TestPubNubDecryption2()
 {
     PubnubCrypto pc = new PubnubCrypto("enigma");
     //Deserialized string
     string strMessage= "f42pIQcWZ9zbTbH8cyLwB/tdvRxjFLOYcBNMVKeHS54=";
     //Decrypt
     string dec = pc.EncryptOrDecrypt(false, strMessage);
     //Deserialize
     JavaScriptSerializer js = new JavaScriptSerializer();
     strMessage= js.Deserialize<string>(dec);
     Assert.AreEqual("Pubnub Messaging API 2", strMessage);
 }
Example #20
0
        public void TestGermanCharsEncryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            string strMessage= "ÜÖ";

            JavaScriptSerializer js = new JavaScriptSerializer();
            strMessage= js.Serialize(strMessage);
            Console.WriteLine(strMessage);
            string enc = pc.EncryptOrDecrypt(true, strMessage);
            Console.WriteLine(enc);
            Assert.AreEqual("stpgsG1DZZxb44J7mFNSzg==", enc);
        }
Example #21
0
        public void TestPubNubEncryption2()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //Deserialized
            string strMessage= "Pubnub Messaging API 2";
            //serialize the message
            JavaScriptSerializer js = new JavaScriptSerializer();
            strMessage= js.Serialize(strMessage);
            //encrypt
            string enc = pc.EncryptOrDecrypt(true, strMessage);

            Assert.AreEqual("f42pIQcWZ9zbTbH8cyLwB/tdvRxjFLOYcBNMVKeHS54=", enc);
        }
Example #22
0
        public void TestHashEncryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //serialized string
            string strMessage= "{\"foo\":{\"bar\":\"foobar\"}}";
            //encrypt
            string enc = pc.EncryptOrDecrypt(true, strMessage);

            Assert.AreEqual("GsvkCYZoYylL5a7/DKhysDjNbwn+BtBtHj2CvzC4Y4g=", enc);
        }
Example #23
0
        public void TestUnicodeCharsDecryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            string strMessage= "+BY5/miAA8aeuhVl4d13Kg==";
            JavaScriptSerializer js = new JavaScriptSerializer();
            //decrypt
            string dec = pc.EncryptOrDecrypt(false, strMessage);
            //deserialize
            strMessage= js.Deserialize<string>(dec);

            Assert.AreEqual("漢語", strMessage);
        }
Example #24
0
        public void TestMyObjectDecryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //Deserialized
            string strMessage= "Zbr7pEF/GFGKj1rOstp0tWzA4nwJXEfj+ezLtAr8qqE=";
            //Decrypt
            string dec = pc.EncryptOrDecrypt(false, strMessage);
            //create an object of the custom class
            CustomClass cc = new CustomClass();
            JavaScriptSerializer js = new JavaScriptSerializer();
            //Serialize it
            string res = js.Serialize(cc);

            Assert.AreEqual(res, dec);
        }
Example #25
0
 public void TestYayDecryption()
 {
     PubnubCrypto pc = new PubnubCrypto("enigma");
     //string strMessage= "\"q/xJqqN6qbiZMXYmiQC1Fw==\"";
     //Non deserialized string
     string strMessage= "\"Wi24KS4pcTzvyuGOHubiXg==\"";
     //Deserialize
     JavaScriptSerializer js = new JavaScriptSerializer();
     strMessage= js.Deserialize<string>(strMessage);
     //decrypt
     string dec = pc.EncryptOrDecrypt(false, strMessage);
     //deserialize again
     strMessage= js.Deserialize<string>(dec);
     Assert.AreEqual("yay!", strMessage);
 }
Example #26
0
        public void TestMyObjectEncryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //create an object of the custom class
            CustomClass cc = new CustomClass();
            //serialize it
            JavaScriptSerializer js = new JavaScriptSerializer();
            string res = js.Serialize(cc);
            //encrypt it
            string enc = pc.EncryptOrDecrypt(true, res);

            Assert.AreEqual("Zbr7pEF/GFGKj1rOstp0tWzA4nwJXEfj+ezLtAr8qqE=", enc);
        }
Example #27
0
        public void TestYayEncryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //deserialized string
            string strMessage= "yay!";
            //serialize the string
            JavaScriptSerializer js = new JavaScriptSerializer();
            strMessage= js.Serialize(strMessage);
            Console.WriteLine(strMessage);
            //Encrypt
            string enc = pc.EncryptOrDecrypt(true, strMessage);
            Assert.AreEqual("Wi24KS4pcTzvyuGOHubiXg==", enc);
            /*PubnubCrypto pc = new PubnubCrypto("enigma");
            string strMessage= "yay!";
            JavaScriptSerializer js = new JavaScriptSerializer();
            strMessage= js.Serialize(strMessage);
            string enc = pc.EncryptOrDecrypt(true, strMessage);*/

            //Assert.AreEqual("q/xJqqN6qbiZMXYmiQC1Fw==", enc);
        }
Example #28
0
        public void TestNullDecryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //deserialized string
            string strMessage= null;
            //decrypt
            string dec = pc.EncryptOrDecrypt(false, strMessage);

            Assert.AreEqual("", dec);
        }
Example #29
0
        public void TestNullEncryption()
        {
            PubnubCrypto pc = new PubnubCrypto("enigma");
            //serialized string
            string strMessage= null;
            //encrypt
            string enc = pc.EncryptOrDecrypt(true, strMessage);

            Assert.AreEqual("", enc);
        }
Example #30
0
        /**
         * Http Get Request
         *
         * @param List<string> request of URL directories.
         * @return List<object> from JSON response.
         */
        private bool _request(List<string> url_components, ResponseType type)
        {
            List<object> result = new List<object>();
            StringBuilder url = new StringBuilder();

            // Add Origin To The Request
            url.Append(this.ORIGIN);

            // Generate URL with UTF-8 Encoding
            foreach (string url_bit in url_components)
            {
                url.Append("/");
                //url.Append(_encodeURIcomponent(url_bit));
                url.Append(url_bit);
            }

            if (type == ResponseType.Presence || type == ResponseType.Subscribe)
            {
                url.Append("?uuid=");
                url.Append(this.sessionUUID);
            }

            if (type == ResponseType.DetailedHistory)
                url.Append(parameters);

            // Temporary fail if string too long
            if (url.Length > this.LIMIT)
            {
                result.Add(0);
                result.Add("Message Too Long.");
                // return result;
            }

            Uri requestUri = new Uri(url.ToString());

            // Force canonical path and query
            string paq = requestUri.PathAndQuery;
            /*FieldInfo flagsFieldInfo = typeof(Uri).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
            ulong flags = (ulong)flagsFieldInfo.GetValue(requestUri);
            flags &= ~((ulong)0x30); // Flags.PathNotCanonical|Flags.QueryNotCanonical
            flagsFieldInfo.SetValue(requestUri, flags);*/

            // Create Request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

            try
            {
                // Make request with the following inline Asynchronous callback
                IAsyncResult asyncResult = request.BeginGetResponse(new AsyncCallback((asynchronousResult) =>
                {
                        HttpWebRequest aRequest = (HttpWebRequest)asynchronousResult.AsyncState;
                        HttpWebResponse aResponse = (HttpWebResponse)aRequest.EndGetResponse(asynchronousResult);
                        using (StreamReader streamReader = new StreamReader(aResponse.GetResponseStream()))
                        {
                            // Deserialize the result
                            object jsonString = streamReader.ReadToEnd();

                            JavaScriptSerializer jS = new JavaScriptSerializer();
                            //deserialize
                            //object objDecrypted = jS.Deserialize<object>(jsonString.ToString());

                            //result = objDecrypted.ToString();

                            result = (List<object>)jS.Deserialize<List<object>>(jsonString.ToString());
                            var resultOccupancy = jS.DeserializeObject(jsonString.ToString());

                            if ((result.Count != 0) && (type != ResponseType.DetailedHistory))
                            {
                                if (result[0] is object[])
                                {
                                    foreach (object message in (object[])result[0])
                                    {
                                        this.ReturnMessage = message;
                                    }
                                }
                            }

                            switch (type)
                            {
                                case ResponseType.Publish:
                                    result.Add(url_components[4]);
                                    Publish = result;
                                    break;
                                case ResponseType.History:
                                    if (this.CIPHER_KEY.Length > 0)
                                    {
                                        List<object> historyDecrypted = new List<object>();
                                        PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY);
                                        foreach (object message in result)
                                        {
                                            string strDecrypted = aes.decrypt(message.ToString());
                                            string strDecoded = jS.Deserialize<string>(strDecrypted);
                                            historyDecrypted.Add(strDecoded);
                                        }
                                        History = historyDecrypted;
                                    } else
                                    {
                                        History = result;
                                    }
                                    break;
                                case ResponseType.DetailedHistory:
                                    if (this.CIPHER_KEY.Length > 0)
                                    {
                                        List<object> historyDecrypted = new List<object>();
                                        PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY);
                                        var myObjectArray = (from item in result select item as object).ToArray();
                                        IEnumerable enumerable = myObjectArray[0] as IEnumerable;
                                        if (enumerable != null)
                                        {
                                            foreach(object element in enumerable)
                                            {
                                                string strDecrypted = aes.decrypt(element.ToString());
                                                string strDecoded = jS.Deserialize<string>(strDecrypted);
                                                historyDecrypted.Add(strDecoded);
                                            }
                                        }
                                        DetailedHistory = historyDecrypted;
                                    }
                                    else
                                    {
                                        //DetailedHistory = (object[])(result[0]);
                                        List<object> historyDecrypted = new List<object>();
                                        var myObjectArray = (from item in result select item as object).ToArray();
                                        IEnumerable enumerable = myObjectArray[0] as IEnumerable;
                                        if (enumerable != null)
                                        {
                                            foreach(object element in enumerable)
                                            {
                                                historyDecrypted.Add(element.ToString());
                                            }
                                        }
                                        DetailedHistory = historyDecrypted;
                                    }
                                    break;
                                case ResponseType.Here_Now:
                                    Dictionary<string, object> dic = (Dictionary<string, object>)resultOccupancy;
                                    List<object> presented = new List<object>();
                                    presented.Add(dic);
                                    Here_Now = (List<object>)presented;
                                    break;
                                case ResponseType.Time:
                                    Time = result;
                                    break;
                                case ResponseType.Subscribe:
                                    result.Add(url_components[2]);
                                    Subscribe = result;
                                    break;
                                case ResponseType.Presence:
                                    result.Add(url_components[2]);
                                    Presence = result;
                                    break;
                                default:
                                    break;
                            }
                        }
                    }), request

                );
                //asyncResult.AsyncWaitHandle.WaitOne();
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }