PHP中的C#TripleDES ECB加密 [英] C# TripleDES ECB Encryption in PHP

查看:67
本文介绍了PHP中的C#TripleDES ECB加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要使用PHP,但是我正在将数据发送到使用C#进行加密的api,因此,我尝试使用以下C#代码中使用的相同方法来加密PHP中的密码:

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;

这是我到目前为止在PHP中所拥有的:

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

我知道我需要为md5函数添加true参数,并且我需要添加PKCS7填充.

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

我没有机会对照C#代码检查它,因为我仍在计算机上安装Visual Studio.有什么我想念的吗?我需要添加静脉注射吗?

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?

我测试了C#代码,发现它没有给出相同的结果.我修复了一些问题,现在有了PHP中C#中的DES.Key和Buffer变量,可以提供正确的结果.

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.

再次已修复.我要做的就是将前8个字符附加到哈希键的末尾.

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

推荐答案

这是我最终想到的.到目前为止,在我尝试过的几个示例上,对我来说都是有效的.

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');

这篇关于PHP中的C#TripleDES ECB加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆