将c#TripleDes PKCS7代码转换为php代码

时间:2017-06-02 04:31:10

标签: c# php pkcs#7 tripledes

我在C#中有这段代码

var dataBytes = Encoding.UTF8.GetBytes('clear text');

var symmetric = SymmetricAlgorithm.Create("TripleDes");
symmetric.Mode = CipherMode.ECB;
symmetric.Padding = PaddingMode.PKCS7;

var encryptor = symmetric.CreateEncryptor(Convert.FromBase64String('myKey'), new byte[8]);

var signedData = Convert.ToBase64String(encryptor.TransformFinalBlock(dataBytes, 0, dataBytes.Length));

现在我需要将c#代码转换为php代码。 方面

1 个答案:

答案 0 :(得分:-1)

我使用此功能并为我工作

function encryptData($plainText,$key)
{

    $byte = mb_convert_encoding($key, 'ASCII');

    $desKey = base64_decode($key);
    $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');

    return base64_encode($encData);
}