Example #1
0
        public static string CreateToken(string IPAddress, string Token, long ticks)
        {
            string hashLeft  = string.Empty;
            string hashRight = string.Empty;
            string encry1    = string.Empty;
            string encry2    = string.Empty;

            try
            {
                string key      = Convert.ToString(ConfigurationManager.AppSettings["keyValue"]);
                string IV       = Convert.ToString(ConfigurationManager.AppSettings["IVValue"]);
                string UniqueID = Convert.ToString(ConfigurationManager.AppSettings["UniqueID"]);

                // [encry1] CLientIDToken : IPAddress : ticks
                encry1 = string.Join(":", new string[] { Token, IPAddress, ticks.ToString() });

                // [encry2] UniqueID + ticks
                hashLeft  = Convert.ToBase64String(TripleDESAlgorithm.Encryption(encry1, key, IV));
                hashRight = string.Join(":", new string[] { UniqueID, ticks.ToString() });

                // [CLientIDToken : IPAddress : ticks + UniqueID + ticks]

                var basestring = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Join(":", hashRight, hashLeft)));

                return(basestring);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #2
0
        void DownloadString_Callback(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                #region ErrorCode
                object objException = e.Error.GetBaseException();

                Type _type = typeof(WebException);
                if (_type != null)
                {
                    WebException objErr = (WebException)e.Error.GetBaseException();
                    WebResponse  rsp    = objErr.Response;
                    using (Stream respStream = rsp.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(respStream);
                        string       text   = reader.ReadToEnd();
                    }
                }
                else
                {
                    Exception objErr = (Exception)e.Error.GetBaseException();
                }
                #endregion
            }
            else if (e.Result != null || !string.IsNullOrEmpty(e.Result))
            {
                string finalData = JToken.Parse(e.Result).ToString();


                string data = TripleDESAlgorithm.Decryption(finalData, keyValue, IVValue);


                Employee AccountDetail = JsonConvert.DeserializeObject <Employee>(data);
            }
        }
Example #3
0
        public static string GetAPIKey()
        {
            string hashLeft  = string.Empty;
            string hashRight = string.Empty;
            string encry1    = string.Empty;
            string ticks;

            try
            {
                APIKeyModel _Model = new APIKeyModel();
                ticks = DateTime.UtcNow.Ticks.ToString();
                // [encry1] CLientIDToken : IPAddress : ticks
                encry1 = string.Join(":", new string[] { _Model.Token, _Model.IPAddress, ticks });

                // [encry2] UniqueID + ticks
                hashLeft  = Convert.ToBase64String(TripleDESAlgorithm.Encryption(encry1, _Model.EncryKey, _Model.IVKey));
                hashRight = string.Join(":", new string[] { _Model.UniqueID, ticks.ToString() });

                // [CLientIDToken : IPAddress : ticks + UniqueID + ticks]

                var basestring = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Join(":", hashRight, hashLeft)));

                return(basestring);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #4
0
        public HttpResponseMessage Get(string Id)
        {
            if (Id != null)
            {
                int _Id = Convert.ToInt32(Id);
                //Getting Employee Data from Database According to Id Passed.
                var Response = _EmployeeRepository.EmployeeDetailsByEmployeeNo(_Id);

                //Serializing Object which we have got from Database.
                string SerializeData = JsonConvert.SerializeObject(Response);

                //Encrypting Serialized Object.
                byte[] buffer = TripleDESAlgorithm.Encryption(SerializeData, ShareKeys.keyValue, ShareKeys.IVValue);

                //Sending Response.
                return(Request.CreateResponse(HttpStatusCode.OK, Convert.ToBase64String(buffer)));
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee ID not found"));
            }
        }
Example #5
0
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            StringBuilder sb = new StringBuilder(1024);

            try
            {
                IEnumerable <string> tokenHeaders;
                if (actionContext.Request.Headers.TryGetValues("APIKEY", out tokenHeaders))
                {
                    string   tokens = tokenHeaders.First();
                    string   key1   = Encoding.UTF8.GetString(Convert.FromBase64String(tokens));
                    string[] Data   = key1.Split(new char[] { ':' });

                    if (tokens != null && Data != null)
                    {
                        string encry1 = Data[0]; //UniqueID
                        string encry2 = Data[1]; //DateTime with Ticks
                        string encry3 = Data[2]; //ClientToken + IPAddress +Ticks

                        if (_IRegistration.GetEncryptionDecryptionKeys(encry1) == null)
                        {
                            return(false);
                        }
                        else
                        {
                            var KeysValues = _IRegistration.GetEncryptionDecryptionKeys(encry1);
                            //Hash Decryption
                            string   DecryHash2 = TripleDESAlgorithm.Decryption(encry3, KeysValues.EncryKey, KeysValues.IVKey);
                            string[] Key2       = DecryHash2.Split(new char[] { ':' });

                            // 1)ClientToken
                            string ClientToken = Key2[0];

                            // 2)IPAddress
                            string IPAddress = Key2[1];

                            // 3)Ticks
                            long ticks = long.Parse(Key2[2]);

                            //ReValidating token Exists in Database or not
                            if (_IRegistration.ValidateToken(ClientToken.ToLower()) == null)
                            {
                                return(false);
                            }
                            else
                            {
                                var Returndata = _IRegistration.ValidateToken(ClientToken.ToLower());

                                ShareKeys.IVValue  = Returndata.IVKey;
                                ShareKeys.keyValue = Returndata.EncryKey;
                                DateTime currentdate = new DateTime(ticks);

                                //Comparing Current Date with date sent
                                bool timeExpired = Math.Abs((DateTime.UtcNow - currentdate).TotalMinutes) > 10;

                                if (!timeExpired)
                                {
                                    if (string.Equals(ClientToken.ToLower(), Returndata.Token.ToLower(), comparisonType: StringComparison.InvariantCulture) == true)
                                    {
                                        return(true);
                                    }
                                    else
                                    {
                                        return(false);
                                    }
                                }
                                else
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Example #6
0
 public static string Decryption(string CypherText, APIKeyModel Model)
 {
     return(TripleDESAlgorithm.Decryption(CypherText, Model.EncryKey, Model.IVKey));
 }
Example #7
0
        public static string Decryption(string CypherText)
        {
            APIKeyModel _Model = new APIKeyModel();

            return(TripleDESAlgorithm.Decryption(CypherText, _Model.EncryKey, _Model.IVKey));
        }
Example #8
0
 public static byte[] Encryption(string PlainText, APIKeyModel Model)
 {
     return(TripleDESAlgorithm.Encryption(PlainText, Model.EncryKey, Model.IVKey));
 }
Example #9
0
        public static byte[] Encryption(string PlainText)
        {
            APIKeyModel _Model = new APIKeyModel();

            return(TripleDESAlgorithm.Encryption(PlainText, _Model.EncryKey, _Model.IVKey));
        }