Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8424755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:05:29+00:00 2026-06-10T04:05:29+00:00

I have inherited some C# code where a PHP page containing the function &decrypt

  • 0

I have inherited some C# code where a PHP page containing the function &decrypt was compiled/transformed into a standalone Windows EXE. I am wanting to take this PHP code and put the functionality into C#.

function &decrypt($enc_text, $password)
{
    $iv_len = 16;
    $enc_text = base64_decode($enc_text);
    $i = $iv_len;
    $n = strlen($enc_text);
    $plain_text = '';
    $iv = substr($password ^ substr($enc_text, 0, $iv_len), 0, 512);
    while ($i < $n)
    {
        $block = substr($enc_text, $i, 16);
        $plain_text .= $block ^ pack('H*', md5($iv));
        $iv = substr($block . $iv, 0, 512) ^ $password;
        $i += 16;
        }
    return $plain_text;
}

I have found a couple a questions that referred to this code, but did not provide much insight:
Just want to decode the code into plain text
and "MD5 decrypt" php function to MySQL stored function

Helpful links:
PHP base64_decode C# equivalent

http://nuronconsulting.com/c-pack-h.aspx

I can get correct results except for the 2nd and 3rd to last characters. I think my base64 decode is still off.

Edit: Updated to latest code being used. Removed intermediate edits.

    private string decipher(string ienc_text, string ipassword)
    {
        int iv_len = 16;
        byte[] toEncryptArray = Convert.FromBase64String(ienc_text);
        string encryptedStringx = System.Text.Encoding.Default.GetString(toEncryptArray);
        string encryptedString = Encoding.GetEncoding(28591).GetString(toEncryptArray);
        string password = ipassword;
        int i = iv_len;
        int n = encryptedString.Length;
        string plain_text = "";
        string iv = phpXOR(ipassword, encryptedString.Substring(0, iv_len));

        while (i < n)
        {
            string block = encryptedString.Substring(i, iv_len);

            string md5 = getMD5(iv);

            byte[] testPack = PackH(md5);
            string testPackstring = Encoding.Default.GetString(testPack);

            string tmp = phpXOR(block, testPackstring);

            plain_text += tmp;

            string block_iv = block + iv;
            string tmp_iv = block_iv;

            if (block_iv.Length > 512)
            {
                tmp_iv = block_iv.Substring(0, 512);
            }

            iv = phpXOR(tmp_iv, password);
            i += 16;
        }

        return plain_text;
    }

    public static byte[] PackH(string hex)
    {
        if ((hex.Length % 2) == 1) hex += '0';
        byte[] bytes = new byte[hex.Length / 2];
        for (int i = 0; i < hex.Length; i += 2)
        {
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        }
        return bytes;
    }

    string phpXOR(string text, string key)
    {
        byte[] result = new byte[key.Length];

        for (int c = 0; c < key.Length; c++)
            result[c] = (byte)(((byte)text[c]) ^ ((byte)key[c]));

        return Encoding.Default.GetString(result);
    }

    public static string getMD5(string iValue)
    {
        byte[] textBytes = System.Text.Encoding.Default.GetBytes(iValue);
        //byte[] textBytes = System.Text.Encoding.GetEncoding(28591).GetBytes(password);

        try
        {
            System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
            cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hash = cryptHandler.ComputeHash(textBytes);
            string ret = "";
            foreach (byte a in hash)
            {
                if (a < 16)
                    ret += "0" + a.ToString("x");
                else
                    ret += a.ToString("x");
            }
            return ret;
        }
        catch
        {
            throw;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:05:31+00:00Added an answer on June 10, 2026 at 4:05 am

    The code is not complete in the loop (hence the commented out code), however, the question I posed is answered. The encoding, the XOR issue, the php pack replication is accomplished via helpful links that are in the question.

        private string decipher(string ienc_text, string ipassword)
        {
            int iv_len = 16;
            byte[] toEncryptArray = Convert.FromBase64String(ienc_text);
            string encryptedString = Encoding.GetEncoding("iso-8859-1").GetString(toEncryptArray);
            string password = ipassword;
            int i = iv_len;
            int n = encryptedString.Length;
            string plain_text = "";
            string iv = phpXOR(ipassword, encryptedString.Substring(0, iv_len));
    
            while (i < n)
            {
                string block = encryptedString.Substring(i, iv_len);
    
                string md5 = getMD5(iv);
    
                byte[] testPack = PackH(md5);
                string testPackstring = Encoding.GetEncoding("iso-8859-1").GetString(testPack);
    
                string tmp = phpXOR(block, testPackstring);
    
                plain_text += tmp;
    
                //string block_iv = block + iv;
                //string tmp_iv = block_iv;
    
                //if (block_iv.Length > 512)
                //{
                //    tmp_iv = block_iv.Substring(0, 512);
                //}
    
                //iv = phpXOR(tmp_iv, password);
                //i += 16;
            }
    
            return plain_text;
        }
    
        public static byte[] PackH(string hex)
        {
            if ((hex.Length % 2) == 1) hex += '0';
            byte[] bytes = new byte[hex.Length / 2];
            for (int i = 0; i < hex.Length; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            }
            return bytes;
        }
    
        string phpXOR(string text, string key)
        {
            byte[] result = new byte[key.Length];
    
            for (int c = 0; c < key.Length; c++)
                result[c] = (byte)(((byte)text[c]) ^ ((byte)key[c]));
    
            return Encoding.Default.GetString(result);
        }
    
        public static string getMD5(string iValue)
        {
            byte[] textBytes = System.Text.Encoding.Default.GetBytes(iValue);
    
            try
            {
                System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
                cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] hash = cryptHandler.ComputeHash(textBytes);
                string ret = "";
                foreach (byte a in hash)
                {
                    if (a < 16)
                        ret += "0" + a.ToString("x");
                    else
                        ret += a.ToString("x");
                }
                return ret;
            }
            catch
            {
                throw;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inherited some legacy PHP code what was written back when it was
I have inherited some PHP code (but I've little PHP experience) and can't find
I have inherited some code: Process p = new ProcessBuilder(/bin/chmod, 777, path).start(); p.waitFor(); Basically,
I have inherited some code (from zip file) from a developer and git initialzed,
I have inherited some HTML code and have been asked to align the two
I have a jquery function loading a php file which will output some data
I have a section of code like the following: ---- file.php ---- require_once(mylib.php); function($a,$b)
I've just inherited this code in PHP which seems to do some kind of
Some elements in my page are rendered through php code. Sometimes, on link clicks
I have the following code in specman that I inherited: some_method() is { var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.