Note that there are some explanatory texts on larger screens.

plurals
  1. POTripleDES Encrypting in C# and PHP not coming out the same (PKCS7, ECB)?
    primarykey
    data
    text
    <p>I've spent a couple hours now trying to figure this out, but I just can't get it to work. I've got a C# encryption routine that I need to match in php. I can't change the C# version, that's not an option (3rd party is firm on this).</p> <p>Here's the C# code:</p> <pre><code>//In C# // Console.WriteLine(ApiEncode("testing", "56dsfkj3kj23asdf83kseegflkj43458afdl")); // Results in: // XvHbR/CsLTo= public static string ApiEncode(string data, string secret) { byte[] clear; var encoding = new UTF8Encoding(); var md5 = new MD5CryptoServiceProvider(); byte[] key = md5.ComputeHash(encoding.GetBytes(secret)); TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.Key = key; des.Mode = CipherMode.ECB; des.Padding = PaddingMode.PKCS7; byte[] input = encoding.GetBytes(data); try { clear = des.CreateEncryptor().TransformFinalBlock(input, 0, input.Length); } finally { des.Clear(); md5.Clear(); } return Convert.ToBase64String(clear); } </code></pre> <p>Here's the best of what I've come up with in PHP:</p> <pre><code>//In PHP // echo apiEncode("testing", "56dsfkj3kj23asdf83kseegflkj43458afdl"); // Results in: // 5aqvY6q1T54= function apiEncode($data, $secret) { //Generate a key from a hash $key = md5(utf8_encode($secret), true); //Create init vector $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_ecb), MCRYPT_RAND); //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'); //, $iv); return base64_encode($encData); } </code></pre> <p>To the best of my knowledge, I'm handling the PKCS7 padding properly on the PHP side. I'm not sure what else to try.</p> <p>One thing to note, the C# is happening on windows, and the PHP on linux, not sure that should make a difference.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload