From:             link705 at hotmail dot com
Operating system: Win2000
PHP version:      5.1.1
PHP Bug Type:     mcrypt related
Bug description:  Is it a bug reated to 3DES(padding)

Description:
------------
this code blow for use 3DES,but I can't get result in expect.
for mistake only occured on tail of result, and I compare then php code to
C# code(c# get the right result),
in c#,has this line
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7
but I can't found the way to set it in php
so
is a bug of php?
what can i do to get it in except.

Reproduce code:
---------------
///php code
function GetIV(){
  $res = "";
  for($i = 1; $i <= 8; $i++)
    $res .= chr($i);
  return $res;
}

function fmt3DESEx($s){
  $key = pack('H48',"9F8C243AEE347183B39DD81B20941E86BC11529B034C8842");
  $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
  $iv = GetIV();   //like c# new byte[]{1,2,3,4,5,6,7,8}
  mcrypt_generic_init($td, $key, $iv);
  $encrypted_data = mcrypt_generic($td, $s);
  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);
  return $encrypted_data;
}
$S = "123456";
echo(BASE64_Encode(fmt3DESEx(sha1($S, true))));

//********************C# code ******************
    private string fmt3DES(byte[] Value){
      byte[] bytKey = new
byte[]{0x9F,0x8C,0x24,0x3A,0xEE,0x34,0x71,0x83,0xB3,0x9D,0xD8,0x1B,0x20,0x94,0x1E,0x86,0xBC,0x11,0x52,0x9B,0x03,0x4C,0x88,0x42};
      ICryptoTransform ct;
      MemoryStream ms;
      CryptoStream cs;

      SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
      mCSP.IV = new
byte[]{1,2,3,4,5,6,7,8};//Convert.FromBase64String("12345678");//
      mCSP.Key = bytKey;
      mCSP.Mode = System.Security.Cryptography.CipherMode.CBC;
      mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
      
      ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
      ms = new MemoryStream();
      cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
      cs.Write(Value, 0, Value.Length);
      cs.FlushFinalBlock();     
      cs.Close();      
      return Convert.ToBase64String(ms.ToArray());
    }

    private void btnTest_Click(object sender, System.EventArgs e) {    
      string s;
      Byte[] clearBytes = Encoding.UTF8.GetBytes(txtSrc.Text);
      Byte[] hashedBytes =
((HashAlgorithm)CryptoConfig.CreateFromName("SHA1")).ComputeHash(clearBytes);
      s = Convert.ToBase64String(hashedBytes);
      txt2.Text = fmt3DES(hashedBytes);
    }


Expected result:
----------------
xUL2ewBUn7mqNdrgbTzoZyfUjOMZw6r2

Actual result:
--------------
xUL2ewBUn7mqNdrgbTzoZ+vkYhgNnLbQ
                     ^^^^^^^^^^^ difference

-- 
Edit bug report at http://bugs.php.net/?id=35653&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=35653&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): 
http://bugs.php.net/fix.php?id=35653&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=35653&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=35653&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=35653&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=35653&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=35653&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=35653&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=35653&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=35653&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=35653&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=35653&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=35653&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=35653&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=35653&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=35653&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=35653&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=35653&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=35653&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=35653&r=mysqlcfg

Reply via email to