2

Below is my C# code which decrypts the encoded string in C#.

RijndaelManaged RijndaelCipher = new RijndaelManaged(); 
string DecryptedData; 
byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted); 
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString()); 
//Making of the key for decryption 
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt); 
//Creates a symmetric Rijndael decryptor object. 
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)); 
byte[] plainText = Decryptor.TransformFinalBlock(EncryptedData, 0, EncryptedData.Length); 

//Converting to string 
DecryptedData = Encoding.Unicode.GetString(plainText); 

But I want above same code in PHP. I tried by below code but it is not giving me correct output.

function Decrypt( $encrypted, $key, $iv ){
$encrypted=base64_decode($encrypted);
return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_CFB, $iv);
}

Here i am getting output $)íqyZiG¤õ¡¹¡ I passed $s = 'cJ4ZJD3Vkf3Dv5uxrWiTQg==' and $key = '123' (which is same, used while encrypting in C#).

What should I pass to $iv so that I get the decrypted output as "Snehal"?

2
  • 7
    Try mcrypt_decrypt instead of encrypt
    – Anigel
    Aug 2, 2013 at 7:02
  • 2
    $s is the salt, but cJ4ZJD3Vkf3Dv5uxrWiTQg== is not the salt, its the salt encoded in Base 64 format. Try with base64_decode('cJ4ZJD3Vkf3Dv5uxrWiTQg==')
    – Havenard
    Aug 2, 2013 at 7:02

2 Answers 2

2

Firstly you need MCRYPT_RIJNDAEL_128 not 256 and CBC mode. But, the bigger problem is PasswordDeriveBytes. I don't think this is available for PHP. But... with a bit of work browsing MS source and Mono source it is possible to create something similar.

The easy bit is:

<?php
$encb64 = "cJ4ZJD3Vkf3Dv5uxrWiTQg==";
$pwd = "123";
$salt = "3";

$enc = base64_decode($encb64);
$decpad = Decrypt($enc, $pwd, $salt);
// Remove the padding
$pad = ord($decpad[($len = strlen($decpad)) - 1]);
$dec = substr($decpad, 0, strlen($decpad) - $pad);

echo "Enc: " . bin2hex($enc) . "\r\n";
echo "Dec: " . $dec . "\r\n";

function Decrypt($ciphertext, $password, $salt)
{
  $key = PBKDF1($password, $salt, 100, 32);
  $iv = PBKDF1($password, $salt, 100, 16);

  // NB: Need 128 not 256 and CBC mode to be compatible
  return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext, MCRYPT_MODE_CBC, $iv);
}

?>

Then using the PBKDF1 function from this post as a starting point and looking at both the Mono and Microsoft source we can come up with the function below.

PLEASE take note of the warnings. This is a VERY BAD way of doing things for many reasons but it is sufficient to show how to achieve what was asked.

<?php
function PBKDF1($pass, $salt, $count, $cb)
{
  // This is very approximately the way that the Microsoft version of 
  // PasswordDeriveBytes works.

  ///
  /// !!!WARNING!!!
  ///
  // This is a BAD function!
  // Irrespective of the fact that the use of PBKDF1 is not recommended anyway.
  //
  // This really should be put into a class with a constructor taking the 
  // $pass, $salt and $count.
  // Then there should be a Reset() method to start from scratch each time a new pwd/salt is used.
  // And there should be a GetBytes(int) method to get the required info.
  // But for the sake of simplicity we are assuming the same pwd and salt for each call to 
  // this function. This will not stand up to any scrutiny!

  static $base;
  static $extra;
  static $extracount= 0;
  static $hashno;
  static $state = 0;

  if ($state == 0)
  {
    $hashno = 0;
    $state = 1;

    $key = $pass . $salt;
    $base = sha1($key, true);
    for($i = 2; $i < $count; $i++)
    {
      $base = sha1($base, true);
    }
  }

  $result = "";

  // Check if we have any bytes left over from a previous iteration.
  // This is the way MS appears to do it. To me it looks very badly wrong
  // in the line: "$result = substr($extra, $rlen, $rlen);"
  // I'm sure it should be more like "$result = substr($extra, $extracount, $rlen);"
  // Mono have provided what looks like a fixed version at
  // https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Security.Cryptography/PasswordDeriveBytes.cs
  // But I'm no cryptographer so I might be wrong.
  // But this seems to work for low values of $hashno and seems to work
  // with C# implementations.

  if ($extracount > 0)
  {
    $rlen = strlen($extra) - $extracount;
    if ($rlen >= $cb)
    {
      $result = substr($extra, $extracount, $cb);
      if ($rlen > $cb)
      {
        $extracount += $cb;
      }
      else
      {
        $extra = null;
        $extracount = 0;
      }
      return $result;
    }
    $result = substr($extra, $rlen, $rlen);
  }

  $current = "";
  $clen = 0;
  $remain = $cb - strlen($result);
  while ($remain > $clen)
  {
    if ($hashno == 0)
    {
      $current = sha1($base, true);
    }
    else if ($hashno < 1000)
    {
      $n = sprintf("%d", $hashno);
      $tmp = $n . $base;
      $current .= sha1($tmp, true);
    }
    $hashno++;
    $clen = strlen($current);     
  }

  // $current now holds at least as many bytes as we need
  $result .= substr($current, 0, $remain);

  // Save any left over bytes for any future requests
  if ($clen > $remain)
  {
    $extra = $current;
    $extracount = $remain;
  }

  return $result; 
}
?>
1
  • Thanks for your help.This was the code I was looking for. Thanks for your quick response.
    – Ash
    Aug 5, 2013 at 5:05
0

The problem seems to be the C# code you're using.

In order to encrypt / decrypt using RindjaelManaged you would do something like this (taken from MSDN example):

myRijndael = new RijndaelManaged();

myRijndael.GenerateKey();
myRijndael.GenerateIV();

// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);

// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

You need to retrieve that IV (initialisation vector) GenerateIV() creates using RindjaelManaged's IV attribute in that piece of C# code you showed in order to use it into your PHP routine. Even if you have to use ICryptoTransform instead of the method above. AFAIK, there's no way of retrieving it when only salt and key are given.

Not the answer you're looking for? Browse other questions tagged or ask your own question.