【问题标题】:Convert PHP cipher decryption to C#将 PHP 密码解密转换为 C#
【发布时间】:2012-08-24 23:59:25
【问题描述】:

我继承了一些 C# 代码,其中包含函数 &decrypt 的 PHP 页面被编译/转换为独立的 Windows EXE。我想使用这个 PHP 代码并将功能放入 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;
}

我发现了几个提到此代码的问题,但没有提供太多见解: Just want to decode the code into plain text"MD5 decrypt" php function to MySQL stored function

有用的链接: PHP base64_decode C# equivalent

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

我可以得到正确的结果,除了倒数第二个和第三个字符。我认为我的 base64 解码仍然关闭。

编辑:更新到正在使用的最新代码。删除了中间编辑。

    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;
        }
    }

【问题讨论】:

  • 为什么不做一个快速而肮脏的逐行翻译呢?它会更短更容易使用..
  • 是的,如果 C# 不像 PHP 那样严格或具有相同的功能,它会。你将如何处理字符串的异或?我找到了 EncryptOrDecrypt 方法。这是正确的方法吗? C# 没有像 PHP 那样的“MD5”函数。我在“getMD5”函数中找到了代码。这是正确的方法吗? PHP 允许您创建一个值大于字符串长度的子字符串,而 C# 则不允许。 C# 没有“打包”功能。我找到了一个使用 ToInt32 的实现(在 for "key +=" 语句中。这是正确的方法吗?谢谢。

标签: c# php encryption


【解决方案1】:

循环中的代码不完整(因此注释掉的代码),但是,我提出的问题得到了回答。编码、XOR 问题、php 包复制是通过问题中的有用链接完成的。

    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;
        }
    }

【讨论】:

    猜你喜欢
    • 2019-06-06
    • 2014-11-03
    • 2011-02-26
    • 2020-11-06
    • 2012-06-20
    • 2014-04-21
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    相关资源
    最近更新 更多