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?

2

2 Answers 2

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";
Sign up to request clarification or add additional context in comments.

3 Comments

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 }
<pre> 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); } } </pre> This doesnt give required result.
Does this help you? Accept if answer is ok.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.