Note that there are some explanatory texts on larger screens.

plurals
  1. POEncrypting on PHP but can't decrypt on C#
    primarykey
    data
    text
    <p>I have a test.php with the following: <pre><code>echo encrypt("something"); function encrypt($str) { $enc_key = "my key is 8 char long"; $ivArray=array( 0x10, 0x12, 5, 0x11, 0x23, 1, 0x55, 0x43 ); $iv=null; foreach ($ivArray as $element) $iv.=CHR($element); return strtoupper(bin2hex(base64_encode(mcrypt_encrypt(MCRYPT_DES, $enc_key, $str, MCRYPT_MODE_CBC, $iv)))); } </code></pre> <hr> <p>Then on my C# code I have:</p> <pre><code>private static byte[] iv = new byte[] { 0x10, 0x12, 5, 0x11, 0x23, 1, 0x55, 0x43 }; public string ConvertString(string input, string myKey) { try { input = byteArrayString(input); byte[] bytes = Encoding.UTF8.GetBytes(myKey); byte[] buffer = Convert.FromBase64String(input); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, iv), CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); return Encoding.UTF8.GetString(stream.ToArray()); } catch { return string.Empty; } } private string byteArrayString(string input) { byte[] buffer = new byte[input.Length / 2]; for (int i = 0; i &lt; input.Length; i += 2) { if (i &lt; input.Length) { buffer[i / 2] = (byte)((Uri.FromHex(input[i]) * 0x10) + Uri.FromHex(input[i + 1])); } } return Encoding.UTF8.GetString(buffer); } </code></pre> <hr> <p>If I do it all on C# it works the difference from the PHP to the C# is 12 bytes on the resulting encryption from the PHP.</p> <p><strong>What am I doing wrong at the PHP side to match the encryption ?</strong></p> <hr> <p>Update (PKCS7):</p> <pre><code>$block = mcrypt_get_block_size('des', 'cbc'); $len = strlen($str); $padding = $block - ($len % $block); $str .= str_repeat(chr($padding),$padding); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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