从 c# 到 php 的 3DES 加密

3DES encryption from c# to php

我需要php 3DES 加密来生成时间戳以连接到网络服务

我想在 php 中生成时间戳,我有 C# 示例代码。但是我的 php 代码生成的代码与源代码不同。

这是我的 php 代码

class trytry{


    public function encrypt2($data, $secret)
    {
        //Generate a key from a hash
        $key = md5(utf8_encode($secret), true);

        //Take first 8 bytes of $key and append them to the end of $key.
        $key .= substr($key, 0, 8);

        //Pad for PKCS7
        $blockSize = mcrypt_get_block_size('tripledes', 'ecb');
        $len = strlen($data);
        $pad = $blockSize - ($len % $blockSize);
        $data .= str_repeat(chr($pad), $pad);

        //Encrypt data
        $encData = mcrypt_encrypt('tripledes', $key, $data, 'ecb');

        return base64_encode($encData);
    }

    public function decrypt2($data, $secret)
    {
        //Generate a key from a hash
        $key = md5(utf8_encode($secret), true);

        //Take first 8 bytes of $key and append them to the end of $key.
        $key .= substr($key, 0, 8);

        $data = base64_decode($data);

        $data = mcrypt_decrypt('tripledes', $key, $data, 'ecb');

        $block = mcrypt_get_block_size('tripledes', 'ecb');
        $len = strlen($data);
        $pad = ord($data[$len-1]);

        return substr($data, 0, strlen($data) - $pad);
    }

    public function return_timestap(){

        date_default_timezone_set('GMT');
        return $date = date('D, d M Y H:i:s')." GMT" ;

        //." GMT" 
       // this is code to generate 
    }

}

C# 示例代码

using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace MemberSite.OddsDisplay.Helpers
{
    public class DES3
    {
        private byte[] bKey;
        private byte[] bIV;
        private SymmetricAlgorithm mCSP = new AesCryptoServiceProvider();

        public DES3(byte[] key)
        {
            bKey = key;
        }
        public DES3(byte[] key, byte[] iv)
        {
            bKey = key;
            bIV = iv;
        }
    }

    public string EncryptString(string Value)
        {
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            mCSP.Key = bKey;
            mCSP.IV = bIV;
            mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
            mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
            byt = Encoding.UTF8.GetBytes(Value);
            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return Convert.ToBase64String(ms.ToArray());
        }

    public string DecryptString(string Value)
        {
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            mCSP.Key = bKey;
            mCSP.IV = bIV;
            mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
            mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
            byt = Convert.FromBase64String(Value);
            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return Encoding.UTF8.GetString(ms.ToArray());
        }

    public class Hash
    {
        public static string StringMD5(string data)
        {
            return (
                System.BitConverter.ToString(
                    System.Security.Cryptography.MD5.Create().ComputeHash(
                        System.Text.Encoding.UTF8.GetBytes(data)
                    )
                )
            );
        }
        public static byte[] BytesMD5(string data)
        {
            return (
                System.Security.Cryptography.MD5.Create().ComputeHash(
                    System.Text.Encoding.UTF8.GetBytes(data)
                )
            );
        }
    }
}



public string GetTimeStamp(string accessKey, string ivKey)
    {
        string timeStamp = string.Empty;

        byte[] key = Hash.BytesMD5(accessKey);
        byte[] ivbyte = Hash.BytesMD5(ivKey);
        DES3 des3 = new DES3(key, ivbyte);
        timeStamp = des3.EncryptString(getTimestampString(DateTime.Now));

        return timeStamp;
    }

时间戳数据:2019 年 6 月 6 日,星期四 09:38:03 GMT

结果PHP

QMaMk7ipbL73QLy6tbGBBG6vWJPBqsTEUt2mIpjKhjc=

结果 C#

CKdZRaEoT0UgH9KVbc5Oyc1WXspLu/uoIGqCxxnavXc=

谁能解释并指出为什么我的代码不起作用?

任何帮助将不胜感激

提前致谢

首先,您的 PHP 使用三元组,但 C# 使用 AES

其次,你输入的C#代码和PHP不相等

在php你有

    //Generate a key from a hash
    $key = md5(utf8_encode($secret), true);

    //Take first 8 bytes of $key and append them to the end of $key.
    $key .= substr($key, 0, 8);

您将 8 个字节附加到密钥,使其成为 24 个字节的密钥长度

但在 C# 中

    byte[] key = Hash.BytesMD5(accessKey);

这是 16 字节的密钥长度。

因为你的要求是PHP像C#一样加密和解密,所以我只专注于改变你的php代码

    public function encrypt2($data, $secret)
    {
        //Generate a key from a hash
        $key = md5(utf8_encode($secret), true);

        // Remove this
        //Take first 8 bytes of $key and append them to the end of $key.
        //$key .= substr($key, 0, 8);

        //Pad for PKCS7
        $blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $len = strlen($data);
        $pad = $blockSize - ($len % $blockSize);
        $data .= str_repeat(chr($pad), $pad);

        //Encrypt data MCRYPT_RIJNDAEL_128, ECB equal to C# AES
        $encData = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB);

        return base64_encode($encData);
    }

    function decrypt2($data, $secret)
    {
        //Generate a key from a hash
        $key = md5(utf8_encode($secret), true);

        // Remove this
        //Take first 8 bytes of $key and append them to the end of $key.
        //$key .= substr($key, 0, 8);

        $data = base64_decode($data);

        $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB);

        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $len = strlen($data);
        $pad = ord($data[$len-1]);

        return substr($data, 0, strlen($data) - $pad);
    }