duanjian9148 2015-07-27 14:44
浏览 61
已采纳

PHP中的C#TripleDES ECB加密

I mostly work with PHP, but I'm sending data to an api that's using C# for encryption, so I'm trying to encrypt a password in PHP, using the same method used in the following C# code:

System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
System.Security.Cryptography.MD5CryptoServiceProvider hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(Key));
DES.Mode = System.Security.Cryptography.CipherMode.ECB;
System.Security.Cryptography.ICryptoTransform DESEncrypt = DES.CreateEncryptor();
Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Plaintext);
string TripleDES = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
return TripleDES;

This is what I have so far in PHP:

    function encryptData($key, $plainText)
    {

        $byte = mb_convert_encoding($key, 'ASCII');
        $desKey = md5(utf8_encode($byte), true);
        $data = mb_convert_encoding($plainText, 'ASCII');

        // add PKCS#7 padding
        $blocksize = mcrypt_get_block_size('tripledes', 'ecb');
        $paddingSize = $blocksize - (strlen($data) % $blocksize);
        $data .= str_repeat(chr($paddingSize), $paddingSize);

        // encrypt password
        $encData = mcrypt_encrypt('tripledes', $desKey, $data, 'ecb');

        echo base64_encode($encData);
    }

I know I needed to add the true argument for the md5 function, and I know I needed to add the PKCS7 padding.

I haven't had a chance to check it against the C# code, because I'm still installing visual studio on my computer. Is there anything I'm missing? Do I need to add an IV?

EDIT: I tested the C# code, and saw that it was not giving same result. I fixed some things, and now have the DES.Key and Buffer variables from C# in PHP, giving the correct results.

EDIT again: It's fixed. All I had to do was append the first 8 characters to the end of the hashed key.

$desKey .= substr($desKey,0,8);
  • 写回答

1条回答 默认 最新

  • dougang1965 2015-07-27 18:21
    关注

    This is what I eventually came up with. It's worked so far for me on the few examples I've tried.

    function encryptData($key, $plainText)
    {
    
        $byte = mb_convert_encoding($key, 'ASCII');
        $desKey = md5(utf8_encode($byte), true);
        $desKey .= substr($desKey,0,8);
    
        $data = mb_convert_encoding($plainText, 'ASCII');
    
        // add PKCS#7 padding
        $blocksize = mcrypt_get_block_size('tripledes', 'ecb');
        $paddingSize = $blocksize - (strlen($data) % $blocksize);
        $data .= str_repeat(chr($paddingSize), $paddingSize);
    
        // encrypt password
        $encData = mcrypt_encrypt('tripledes', $desKey, $data, 'ecb');
    
        echo base64_encode($encData);
    }
    
    encryptData('key', 'pass');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA