1

I'm trying to achive the same DES encription that I've in an C# code but in PHP.

The C# code looks like the following:

public static string EncriptarCadena(string strEncriptar)
{
    DESCryptoServiceProvider provider;

    MemoryStream stream;
    CryptoStream stream2;
    string str2;
    string str = "29393651";
    byte[] buffer2 = new byte[] { 0x45, 50, 0xa5, 0x18, 0x67, 0x58, 0xac, 0xba };
    byte[] bytes = new byte[0];
    try
    {
        bytes = Encoding.UTF8.GetBytes(str.Substring(0, 8));
        provider = new DESCryptoServiceProvider();
        byte[] buffer = Encoding.UTF8.GetBytes(strEncriptar);
        stream = new MemoryStream();
        stream2 = new CryptoStream(stream, provider.CreateEncryptor(bytes, buffer2), CryptoStreamMode.Write);
        stream2.Write(buffer, 0, buffer.Length);
        stream2.FlushFinalBlock();
        str2 = Convert.ToBase64String(stream.ToArray());
    }
    catch (Exception)
    {
        str2 = "";
    }
    finally
    {
        provider = null;
        stream = null;
        stream2 = null;
    }
    return str2;
}

And the code that I've done till now in PHP is the following:

function encrypt($string) {
    //Key
    $key = "29393651";
    $ivArray=array( 0x45, 50, 0xa5, 0x18, 0x67, 0x58, 0xac, 0xba );
    $iv=null;
    foreach ($ivArray as $element)
        $iv.=CHR($element);

    echo "Key: $key    IV: $iv<br>";

    $encrypted_string = mcrypt_encrypt(MCRYPT_DES, $key, $string, MCRYPT_MODE_CBC, $iv);

    return base64_encode($encrypted_string);
}

But I can't find where is the problem, as they don't give the same result for the same input.

Any help will be very welcome :)

Gabe
  • 84,912
  • 12
  • 139
  • 238
fernandojsg
  • 1,090
  • 18
  • 27
  • You're setting CBC mode in the PHP code; I don't see a call to set CBC mode in the C# code? AFAIK, most libraries default to ECB. – staticsan Nov 22 '10 at 23:33
  • Hi staticsan, I checked that the default mode in C# it's CBC, anyway I've tried changing in the php code between all the available modes (ecb,cbc,cfb,ofb,nofb,stream) and didn't work either :( – fernandojsg Nov 22 '10 at 23:38
  • Did you verify that the input bytes is exactly the same? The c# one is doing a substring and making it utf8, and the php one is just looking at $string – superfro Nov 22 '10 at 23:52
  • Hi superfro, I've try both with normal string, or converting it to a char array and folding it, that at the end I guess in php is the same, at least I got the same result in both parameters in php, but still different from C# :( – fernandojsg Nov 23 '10 at 07:51

2 Answers2

2

Thank you very much guys. Helped me with same problem. PKCS7 padding here: How to add/remove PKCS7 padding from an AES encrypted string? just change ecb to cbc

here is a short snapshot i used:

$key = "29393651";
$iv = $key;
$pass_enc = $mypassword;
$block = mcrypt_get_block_size('des', 'cbc');
$pad = $block - (strlen($pass_enc) % $block);
$pass_enc .= str_repeat(chr($pad), $pad);
$pass_enc = mcrypt_encrypt(MCRYPT_DES, $key, $pass_enc, MCRYPT_MODE_CBC, $iv);
$pass_enc = base64_encode ($pass_enc);
Community
  • 1
  • 1
user654299
  • 36
  • 2
2

Check your padding. PHP internally pads the data to be encrypted with binary NULLs \x00 by default which is definitively not the default padding mode in .NET (most likely they use PKCS7 padding by default).

Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
  • Hey Stefan, I fixed it and it works! :) It was the problem, I've implemented PKCS7 padding for the input in php and it works properly. Thank you very much ;) – fernandojsg Nov 23 '10 at 14:56