public static SERVER_CLIENT_RSA_KEYS SERVER_CLIENT_RSA_PAIR()
        {
            SERVER_CLIENT_RSA_KEYS obj = new SERVER_CLIENT_RSA_KEYS();

            obj.CLIENT_PRIVATE_KEY = RSA_MODULE.client_side_private_key_generator();
            obj.CLIENT_PUBLIC_KEY  = RSA_MODULE.client_side_public_key_generator();
            obj.SESSION_KEY        = EDITIONAL_METHODS.unique_code_generator(16);
            return(obj);
        }
        public static string ENCODE_DATA(dynamic dynamic_data)
        {
            string data = JsonConvert.SerializeObject(dynamic_data);

            try
            {
                string final_data     = string.Empty;
                string ENCRYPTED_DATA = AES_MODULE.AES_ENCRYPTION_DATA(data, CORE_MODULE.SERVER_SIDE_AES_KEY, CORE_MODULE.SERVER_SIDE_AES_IV);

                CLIEINT_AES_KEYS obj = new CLIEINT_AES_KEYS();
                obj.KEY = CORE_MODULE.SERVER_SIDE_AES_KEY;
                obj.IV  = CORE_MODULE.SERVER_SIDE_AES_IV;
                string AES_ENCRYPTIN_KEY_PAIR = JsonConvert.SerializeObject(obj);

                string RSA_ENCRYPTED_KEY_PAIR = RSA_MODULE.RSA_Encrypt(AES_ENCRYPTIN_KEY_PAIR, RSA_MODULE.server_side_public_key_generator());
                final_data = RSA_ENCRYPTED_KEY_PAIR + "." + ENCRYPTED_DATA;
                return(final_data);
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
        }
        public static string DECODE_DATA(string data)
        {
            try
            {
                string final_output = string.Empty;

                if (string.IsNullOrEmpty(data))
                {
                    throw new HttpResponseException(HttpStatusCode.NoContent);
                }
                string[] values = data.Split('.');

                string RSA_ENCRYPTED_AES_KEY = values[0].Replace(" ", "+");
                string ENCRYPTED_DATA        = values[1].Replace(" ", "+");

                string AES_KEY_PAIR = RSA_MODULE.RSA_Decrypt(RSA_ENCRYPTED_AES_KEY, RSA_MODULE.server_side_private_key_generator());

                if (string.IsNullOrEmpty(AES_KEY_PAIR))
                {
                    throw new HttpResponseException(HttpStatusCode.NoContent);
                }

                CLIEINT_AES_KEYS obj_AES = JsonConvert.DeserializeObject <CLIEINT_AES_KEYS>(AES_KEY_PAIR);

                final_output = AES_MODULE.AES_DECRYPTION_DATA(ENCRYPTED_DATA, obj_AES.KEY, obj_AES.IV);
                if (string.IsNullOrEmpty(final_output))
                {
                    throw new HttpResponseException(HttpStatusCode.NoContent);
                }

                return(final_output);
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
        }
Exemplo n.º 4
0
        public static string DECODE_DATA(string data)
        {
            string token_json_format = string.Empty;
            string final_output      = string.Empty;

            //spliting string into string array
            if (string.IsNullOrEmpty(data))
            {
                throw new HttpResponseException(HttpStatusCode.NoContent);
            }
            string[] values = data.Split('.');

            //assigning parameters
            string TOKEN = values[0].Replace(" ", "+");
            string RSA_ENCRYPTED_AES_KEY = values[1].Replace(" ", "+");
            string ENCRYPTED_DATA        = values[2].Replace(" ", "+");

            try
            {
                //decrypting token to get token json string
                token_json_format = AES_MODULE.AES_DECRYPTION_DATA(TOKEN, CORE_MODULE.TOKEN_AES_KEY, CORE_MODULE.TOKEN_AES_IV);
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }
            //converting json string into model object
            TOKEN_MODEL token_params = JsonConvert.DeserializeObject <TOKEN_MODEL>(token_json_format);

            //checking expiry time
            if (!expiry_time_check(token_params.EXPIRY_TIME))
            {
                throw new HttpResponseException(HttpStatusCode.RequestTimeout);
            }
            //checking roles
            if (!roles_check(token_params.ROLES, CORE_MODULE.roles))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }
            //checking issuer
            if (!issuer_check(token_params.ISSUER))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }
            //checking audience
            if (!audience_check(token_params.AUDIENCE))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            try
            {
                //decrypting aes key pair for data decryption
                string AES_KEY_PAIR = RSA_MODULE.RSA_Decrypt(RSA_ENCRYPTED_AES_KEY, RSA_MODULE.server_side_private_key_generator());

                if (string.IsNullOrEmpty(AES_KEY_PAIR))
                {
                    throw new HttpResponseException(HttpStatusCode.NoContent);
                }

                //creating model object for key pair
                CLIEINT_AES_KEYS obj_AES = JsonConvert.DeserializeObject <CLIEINT_AES_KEYS>(AES_KEY_PAIR);

                //decryption of data using AES key pair
                final_output = AES_MODULE.AES_DECRYPTION_DATA(ENCRYPTED_DATA, obj_AES.KEY, obj_AES.IV);
                if (string.IsNullOrEmpty(final_output))
                {
                    throw new HttpResponseException(HttpStatusCode.NoContent);
                }

                CORE_MODULE.roles.Clear();

                //returning decrypted value
                return(final_output);
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
        }
Exemplo n.º 5
0
        //INITIALIZATION OF ALL METHODS
        public static void Initialize(int KEY_LENGTH, string Audience = "", string Issuer = "")
        {
            //INITIALIZING KEY LENGTH
            CORE_MODULE.KEY_LENGTH = KEY_LENGTH;

            //GETTING PRIVATE KEY GENERATION FOR CLIENT SIDE PUBLIC KEY AND SERVER SIDE PRIVATE KEY
            string RSA_SERVER_parameters = RSA_MODULE.server_side_random_private_key_generator();

            //CREATION OF XML INSTANCE
            XmlDocument server_xml_doc = new XmlDocument();

            //LOADING STRING INTO XML
            server_xml_doc.LoadXml(RSA_SERVER_parameters);
            //SERILIZING XML DATA INTO JSON FORMAT
            string server_jsosn_text = JsonConvert.SerializeXmlNode(server_xml_doc);
            //deserialization of data using model
            RSA_Conversion_Model server_obj = JsonConvert.DeserializeObject <RSA_Conversion_Model>(server_jsosn_text);

            //setting parameters into server rsa parameter variables
            SERVER_EXPONENT  = server_obj.RSAParameters.Exponent;
            SERVER_MODULES   = server_obj.RSAParameters.Modulus;
            SERVER_P         = server_obj.RSAParameters.P;
            SERVER_Q         = server_obj.RSAParameters.Q;
            SERVER_D         = server_obj.RSAParameters.D;
            SERVER_DP        = server_obj.RSAParameters.DP;
            SERVER_DQ        = server_obj.RSAParameters.DQ;
            SERVER_INVERSE_Q = server_obj.RSAParameters.InverseQ;


            //GETTING PRIVATE KEY GENERATION FOR SERVER SIDE PUBLIC KEY AND CLIENT SIDE PRIVATE KEY
            string RSA_CLIENT_parameters = RSA_MODULE.server_side_random_private_key_generator();
            //CREATION OF XML INSTANCE
            XmlDocument client_xml_doc = new XmlDocument();

            //LOADING STRING INTO XML
            client_xml_doc.LoadXml(RSA_CLIENT_parameters);
            //SERILIZING XML DATA INTO JSON FORMAT
            string client_jsosn_text        = JsonConvert.SerializeXmlNode(client_xml_doc);
            RSA_Conversion_Model client_obj = JsonConvert.DeserializeObject <RSA_Conversion_Model>(client_jsosn_text);

            //setting parameters into client rsa parameter variables
            CLIENT_EXPONENT  = client_obj.RSAParameters.Exponent;
            CLIENT_MODULES   = client_obj.RSAParameters.Modulus;
            CLIENT_P         = client_obj.RSAParameters.P;
            CLIENT_Q         = client_obj.RSAParameters.Q;
            CLIENT_D         = client_obj.RSAParameters.D;
            CLIENT_DP        = client_obj.RSAParameters.DP;
            CLIENT_DQ        = client_obj.RSAParameters.DQ;
            CLIENT_INVERSE_Q = client_obj.RSAParameters.InverseQ;

            //Unique number assigning as a key for AES_KEY and IV values of both SERVER SIDE AES and TOKEN Generation
            SERVER_SIDE_AES_KEY = EDITIONAL_METHODS.unique_code_generator(16);
            SERVER_SIDE_AES_IV  = EDITIONAL_METHODS.unique_code_generator(16);

            TOKEN_AES_KEY = EDITIONAL_METHODS.unique_code_generator(16);
            TOKEN_AES_IV  = EDITIONAL_METHODS.unique_code_generator(16);

            try
            {
                //Adding add responses to final response and assigning values
                Response_Dictionary = new Dictionary <string, string>();
                roles          = new List <string>();
                token_issuer   = Issuer;
                token_audience = Audience;
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
        }