4

I am attempting to convert a C# function into PHP.

Here is the C# function:

    public string Encrypt(string Value)
    {
        string RetVal = "";
        if(Value != string.Empty && Value != null)
        {
            MemoryStream Buffer = new MemoryStream();
            RijndaelManaged RijndaelManaged = new RijndaelManaged();
            UnicodeEncoding UnicodeEncoder = new UnicodeEncoding();

            byte[] KeyArray = new Byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
            byte[] IVArray = new Byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };     
            try
            {
                byte[] ValueBytes = UnicodeEncoder.GetBytes(Value);

                CryptoStream EncryptStream = new CryptoStream(Buffer,
                    RijndaelManaged.CreateEncryptor(KeyArray, IVArray),
                    CryptoStreamMode.Write);

                EncryptStream.Write(ValueBytes, 0, ValueBytes.Length);
                EncryptStream.FlushFinalBlock();

                // Base64 encode the encrypted data
                RetVal = Convert.ToBase64String(Buffer.ToArray());
            }
            catch
            {
                throw;
            }
        }

        return RetVal;
    }

and here is my attempt in PHP:

function EncryptString ($cleartext) 
{

    $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');     
    $key128 = '111111111111111111111111111';
    $iv = '111111111111111111111111111';

    if (mcrypt_generic_init($cipher, $key128, $iv) != -1)  //Parameter iv will be ignored in ECB mode
    {
        $cipherText = mcrypt_generic($cipher,$cleartext );
        mcrypt_generic_deinit($cipher);     
        $encrypted = (bin2hex($cipherText));       
        return base64_encode($encrypted); 
    }
}

Currently, when I encode a test phrase "test" using these two functions, I get different values. It looks like the PHP version takes a string for $key and $iv values, where the C# version takes an array of bytes.

How would I modify my PHP function to mimic the C# function?

[edit] the c# function is 3rd party and I have no access to change it; I need to write the equivalent in PHP to encode a given string in the same manner

kkuni
  • 67
  • 1
  • 1
  • 8
  • 2
    I don't know how Rijndael's function works, but does it matter that the number of `1`'s in your PHP version is 11 more then the number of `1`'s in your .NET version? – mellamokb Jun 25 '12 at 19:00
  • 3
    `try { ... } catch { throw; }` is just code pollution. – H H Jun 25 '12 at 19:11
  • 1
    possible duplicate of [Rewrite Rijndael 256 C# Encryption Code in PHP](http://stackoverflow.com/questions/3505453/rewrite-rijndael-256-c-sharp-encryption-code-in-php) – H H Jun 25 '12 at 21:19
  • And [another bunch](http://stackoverflow.com/questions/tagged/encryption+php+c%23). – H H Jun 25 '12 at 21:20
  • Thanks, I actually looked over a few of those similar posts. I think my question here relates more to the format of the key/iv between the 2 functions, rather than the encryption itself. since PHP doesn't have byte types, how would I convert the byte arrays in c# to the $key128 and $iv variables in PHP ? – kkuni Jun 25 '12 at 22:05
  • You can see how the key/iv are set in most of those questions. Even the DES and AES code is applicable. – H H Jun 26 '12 at 13:06
  • Look at PHP's pack() function for converting byte to string. – Chris Jul 17 '12 at 20:31

2 Answers2

1

Take a look at the RijndaelManaged documentation and copy the:

  • encryption mode (cbc)
  • block size
  • padding mode (pkcs#7)

Note that you may retrieve the padding from elsewhere.

Now make sure that you use the exact same input for both sides. You can do this by printing out the key, IV and plain text in hexadecimal values. The key size and IV size should be the exact amount of bits required by the algorithm. To get to the same plain text you need to use identical s.

Finally you need the same ciphertext encoding, but it seems that you've got this covered.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
-1

I would try using, instead of the string of 27 1's, this:

$key128 = '';
for ( $i = 0; $i < 27; ++$i )
   $key128 .= chr(1);
$iv = $key128; // copy

I'm not entirely sure what those functions do, but that would turn your strings into byte arrays of repeated 1's.

Ryan Knuesel
  • 131
  • 1
  • 1
  • 5