tags:

views:

111

answers:

3
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?

+1  A: 

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
+1  A: 

This SO post may help:

tripledes encryption not yielding same results in PHP and C#

micahwittman
He is not using array as IV instead using simple text. I think following are problems when i try doing some replication in PHP. 1) private static readonly byte[] IV = new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 };Not able to convertarray to string properly for mcrypt2) ASCIIEncoding.ASCII.GetBytes(key)Whats alternative for "ASCIIEncoding.ASCII.GetBytes" in PHP?
Johal
A: 
f13o
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 10Warning: mdecrypt_generic() [function.mdecrypt-generic]: Operation disallowed prior to mcrypt_generic_init(). in C:\xampplite\htdocs\dotnet.php on line 11I want to decrtpy following from .Net: $encryptedData ="7RuuCInGDUcdsEKsMimFYk8aqq3pd9tK"$key = "FakeKey"$iv = new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 }
Johal
<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.
Johal
I've edited my answer with working example to this.
f13o
This should work with multi-byte strings as seen in example.
f13o
Does this help you? Accept if answer is ok.
f13o