0
private static readonly byte[] IV = new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 };

public  string Decrypt(string encryptedData,string key)
{
    encryptedData = HttpUtility.UrlDecode(encryptedData);
    byte[] buf = Convert.FromBase64String(encryptedData);
    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
    des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
    des.IV = IV;
    return Encoding.ASCII.GetString(
        des.CreateDecryptor().TransformFinalBlock(
            buf,
            0,
            buf.Length
            )
        );
    }
}

Is there any way to convert above .Net code to PHP so that i can decrypt data in PHP that's sent from .Net code?

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Johal
  • 597
  • 1
  • 7
  • 23
  • This SO post may help: [tripledes encryption not yielding same results in PHP and C#](http://stackoverflow.com/questions/2467419/tripledes-encryption-not-yielding-same-results-in-php-and-c) – micahwittman Jun 24 '10 at 21:14
  • 2
    I'm voting to close this question as off-topic because its asking for a translation – BradleyDotNET Dec 01 '16 at 22:40

2 Answers2

1

You'll find all functionality you need for this within the PHP mcrypt extension. Look here for the full function list and here for an example on how to use it.

svens
  • 11,438
  • 6
  • 36
  • 55
1

I am no encryption expert, but this should be working example...

# original C# code

#  private static readonly byte[] IV = new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 };  
#        public  string Decrypt(string encryptedData,string key)
#        {
#                encryptedData = HttpUtility.UrlDecode(encryptedData);
#                byte[] buf = Convert.FromBase64String(encryptedData);
#                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
#                MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
#                des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
#                des.IV = IV;
#                return Encoding.ASCII.GetString(
#                    des.CreateDecryptor().TransformFinalBlock(
#                    buf,
#                    0,
#                    buf.Length
#                    )
#                    );

#        }

#    }

// packs array into bytestring
function pack_array($a) {
    $out = null;
    foreach ($a as $i) { $out .= chr(ord(substr($i,0,1))); }
    var_dump(($out));
    return $out;
}

class Sample {
    public static $IV = array( 240, 3, 45, 29, 0, 76, 173, 59 );
    public static $key = 's3cr3tk3yl0ng1n4ph';

    // 3des encryption
    public static function Encrypt($data) {
        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');

        $ks = mcrypt_enc_get_key_size($td);

        // create key
        $key = substr(md5(self::$key), 0, $ks);
        // pack IV
        $IVPacked = pack_array(self::$IV);
        mcrypt_generic_init($td, $key, $IVPacked);
        $encryptedData = mcrypt_generic($td, $data);
        mcrypt_generic_deinit($td);

        $encryptedData = base64_encode($encryptedData);
        $encryptedData = urlencode($encryptedData);
        return $encryptedData;
    }


        // 3des decryption
    public static function Decrypt($encryptedData, $key) {
        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');

        $encryptedData = urldecode($encryptedData);
        $encryptedData = base64_decode($encryptedData);

        $key = substr(md5($key), 0, mcrypt_enc_get_key_size($td));
        $IVPacked = pack_array(self::$IV);
        mcrypt_generic_init($td, $key, $IVPacked);
        $decryptedData =  mdecrypt_generic($td, $encryptedData);
        mcrypt_generic_deinit($td);

        return $decryptedData;
    }
}

// __main__
$data = "methodname=GetClientшђшђ";   

var_dump($data);
//var_dump(unpack_str($data, strlen($data)));
$encryptedData = Sample::Encrypt($data);

//var_dump(bin2hex(Sample::$IV));
var_dump (bin2hex($encryptedData));

$decryptedData = Sample::Decrypt($encryptedData, Sample::$key);
var_dump($decryptedData);
echo "\n";
f13o
  • 436
  • 2
  • 11
  • I tried using your function. When i try to run it gives me following warning and do nothing. I do not have experience with mcrypt. Warning: mcrypt_generic_init() expects parameter 3 to be string, array given in C:\xampplite\htdocs\dotnet.php on line 10 Warning: mdecrypt_generic() [function.mdecrypt-generic]: Operation disallowed prior to mcrypt_generic_init(). in C:\xampplite\htdocs\dotnet.php on line 11 I want to decrtpy following from .Net: $encryptedData ="7RuuCInGDUcdsEKsMimFYk8aqq3pd9tK" $key = "FakeKey" $iv = new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 } – Johal Jun 25 '10 at 13:54
  • class Sample { private static $IV = array( 240, 3, 45, 29, 0, 76, 173, 59 ); public function Decrypt($encryptedData, $key) { $IV = self::$IV; $iv_string = ""; foreach(array_values($IV) as $chr) { $iv_string .= chr($chr); } $encryptedData = urldecode($encryptedData); $buf = base64_decode($encryptedData); $key = md5($key); $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''); mcrypt_generic_init($td, $key, $iv_string); return mdecrypt_generic($td, $encryptedData); } } This doesnt give required result. – Johal Jun 25 '10 at 16:14
  • Does this help you? Accept if answer is ok. – f13o Jun 28 '10 at 16:09