Decrypt() public static method

Decrypts a response using AES, filtering out encryption flags
public static Decrypt ( string toDecode ) : string
toDecode string The string to decrypt
return string
Exemplo n.º 1
0
        /// <summary>
        ///     Get the parsed response of a server url
        /// </summary>
        /// <param name="postfix">The postfix to attach to the server address</param>
        /// <returns>The parsed response</returns>
        public static Response GetResponse(string postfix)
        {
            try
            {
                var rawResponse = GetRawResponse(postfix);
                var encrypted   = rawResponse.StartsWith("#!en");
                if (encrypted)
                {
                    rawResponse = Authentication.Decrypt(rawResponse);
                }

                if (string.IsNullOrEmpty(rawResponse))
                {
                    Log.Error(LogName, "No response recieved");
                    return(new Response());
                }

                if (!rawResponse.StartsWith("#!ihc"))
                {
                    return(new Response(rawResponse, encrypted));
                }

                return(Authentication.HandShake() ? GetResponse(postfix) : new Response());
            }
            catch (Exception ex)
            {
                Log.Error(LogName, "Could not contact FOG server");
                Log.Error(LogName, ex);
            }

            return(new Response());
        }
Exemplo n.º 2
0
        /// <summary>
        ///     POST data to a URL
        /// </summary>
        /// <param name="postfix">The text to append to the URL</param>
        /// <param name="param">The params to post</param>
        /// <returns>The response of the server</returns>
        public static Response Post(string postfix, string param)
        {
            Log.Entry(LogName, "POST URL: " + Configuration.ServerAddress + postfix);

            try
            {
                // Set custom certificate policy manager
                ServicePointManager.ServerCertificateValidationCallback =
                    CertificatePolicy.CertValidationCallback;

                // Create a request using a URL that can receive a post.
                var request = (HttpWebRequest)WebRequest.Create(Configuration.ServerAddress + postfix);
                request.Method            = "POST";
                request.AllowAutoRedirect = false;

                // Create POST data and convert it to a byte array.
                var byteArray = Encoding.UTF8.GetBytes(param);
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = byteArray.Length;

                // Get the request stream.
                var dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();

                // Get the response.
                var response = request.GetResponse();
                dataStream = response.GetResponseStream();

                // Open the stream using a StreamReader for easy access.
                var reader      = new StreamReader(dataStream);
                var rawResponse = reader.ReadToEnd();

                // Clean up the streams.
                reader.Close();
                dataStream?.Close();
                response.Close();

                var encrypted = rawResponse.StartsWith("#!en");

                if (encrypted)
                {
                    rawResponse = Authentication.Decrypt(rawResponse);
                }
                return(new Response(rawResponse, encrypted));
            }
            catch (Exception ex)
            {
                Log.Error(LogName, "Failed to POST data");
                Log.Error(LogName, ex);
            }

            return(new Response());
        }
Exemplo n.º 3
0
        /// <summary>
        ///     POST data to a URL
        /// </summary>
        /// <param name="postfix">The text to append to the URL</param>
        /// <param name="param">The params to post</param>
        /// <returns>The response of the server</returns>
        public static Response Post(string postfix, string param)
        {
            Log.Entry(LogName, "POST URL: " + Configuration.ServerAddress + postfix);

            try
            {
                // Set custom certificate policy manager
                ServicePointManager.ServerCertificateValidationCallback =
                    CertificatePolicy.CertValidationCallback;

                // Create a request using a URL that can receive a post.
                var request = (HttpWebRequest)WebRequest.Create(Configuration.ServerAddress + postfix);
                request.Method            = "POST";
                request.AllowAutoRedirect = false;

                // Create POST data and convert it to a byte array.
                var byteArray = Encoding.UTF8.GetBytes(param);
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = byteArray.Length;

                // Get the request stream.
                var dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();

                // Get the response.
                var response     = request.GetResponse();
                var httpResponse = (HttpWebResponse)response;
                dataStream = response.GetResponseStream();

                // Open the stream using a StreamReader for easy access.
                var reader      = new StreamReader(dataStream);
                var rawResponse = reader.ReadToEnd();
                if (httpResponse.StatusCode == HttpStatusCode.Found)
                {
                    var uri = new Uri(httpResponse.Headers["Location"]);
                    Log.Entry(LogName, "Received HTTP redirect, retrying POST to " + uri.GetLeftPart(UriPartial.Path));
                    if (uri.Scheme.Equals("https"))
                    {
                        Log.Entry(LogName, "This is a HTTPS redirect and so we switch to that in settings file.");
                        Settings.Set("HTTPS", "1", false);
                    }
                    Configuration.ServerAddress = uri.GetLeftPart(UriPartial.Authority) + Settings.Get("WebRoot");
                    return(Post(postfix, param));
                }

                // Clean up the streams.
                reader.Close();
                dataStream?.Close();
                response.Close();

                var encrypted = rawResponse.StartsWith("#!en");

                if (encrypted)
                {
                    rawResponse = Authentication.Decrypt(rawResponse);
                }
                return(new Response(rawResponse, encrypted));
            }
            catch (Exception ex)
            {
                Log.Error(LogName, "Failed to POST data");
                Log.Error(LogName, ex);
            }

            return(new Response());
        }