2

I have some data provided by our third party which is encrypted using a C# algorithm. Please see below:

using System.Security.Cryptography;

private static int KEY_SIZE = 32;
private static byte[] IV = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

public static string EncryptString(string plaintext, string password)
{
    byte[] key = new byte[KEY_SIZE];
    byte[] passwordbytes = Encoding.UTF8.GetBytes(password);

    for (int i = 0; i < KEY_SIZE; i++)
    {
        if (i >= passwordbytes.Length)
            key[i] = 0;
        else
            key[i] = passwordbytes[i];
    }

    byte[] encrypted;

    // Create an AesCryptoServiceProvider object
    // with the specified key and IV.
    using (AesManaged aesAlg = new AesManaged())
    {
        aesAlg.Mode = CipherMode.CBC;
        aesAlg.KeySize = KEY_SIZE * 8;

        // Create a decrytor to perform the stream transform.
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(key, IV);

        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {
                    //Write all data to the stream.
                    swEncrypt.Write(plaintext);
                }
                encrypted = msEncrypt.ToArray();
            }
        }
    }

    return Convert.ToBase64String(encrypted);
}

I would like to write the decryption for this in PHP. I was also provided just a key they called it "AES256 Encryption key".

Also the decryption algorithm they provided me in C# is as follows:

public static string DecryptString(string cipherText, string password)
{
    byte[] key = new byte[KEY_SIZE];
    byte[] passwordbytes = Encoding.UTF8.GetBytes(password);

    for (int i = 0; i < KEY_SIZE; i++)
    {
        if (i >= passwordbytes.Length)
            key[i] = 0;
        else
            key[i] = passwordbytes[i];
    }

    byte[] CipherTextBytes = Convert.FromBase64String(cipherText);

    // Declare the string used to hold
    // the decrypted text.
    string plaintext = null;

    // Create an AesCryptoServiceProvider object
    // with the specified key and IV.
    using (AesManaged aesAlg = new AesManaged())
    {
        aesAlg.Mode = CipherMode.CBC;
        aesAlg.KeySize = KEY_SIZE * 8;

        // Create a decrytor to perform the stream transform.
        ICryptoTransform decryptor = aesAlg.CreateDecryptor(key, IV);

        // Create the streams used for decryption.
        using (MemoryStream msDecrypt = new MemoryStream(CipherTextBytes))
        {
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                {
                    // Read the decrypted bytes from the decrypting stream
                    // and place them in a string.
                    plaintext = srDecrypt.ReadToEnd();
                }
            }
        }

    }
    return plaintext;
}

But I want to write the decryption in PHP. As I am very new to encryption stuff please guide me in the right direction.

Many thanks....

Faheem Hameed
  • 73
  • 2
  • 9

1 Answers1

4

We have figured out as below. I hope it would be useful for someone.

function encrypt($string = '', $key = '') {
    $key = utf8_encode($key);

    //make it 32 chars long. pad with \0 for shorter keys
    $key = str_pad($key, 32, "\0");

    //make the input string length multiples of 16. This is necessary
    $padding = 16 - (strlen($string) % 16);
    $string .= str_repeat(chr($padding), $padding);

    //emtpy IV - initialization vector
    $iv = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

    $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $iv));
    return rtrim($encrypted);
}

function decrypt($string = '', $key = '') {
    $key = $key . "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
    $iv = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
    $string = base64_decode($string);

    return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $iv);
}
Faheem Hameed
  • 73
  • 2
  • 9
  • absolutely useful!! Thank you very much! – Cris Feb 12 '14 at 12:28
  • I used the $key as it is and ignored the padding parts in the first line of decrypt function and it worked for me. – julie Oct 27 '17 at 11:07
  • Just a comment, in PHP 7, `mcrypt_encrypt` has been [deprecated](http://php.net/manual/pt_BR/function.mcrypt-encrypt.php), you can take a look [here](https://stackoverflow.com/questions/41272257/mcrypt-is-deprecated-what-is-the-alternative) for alternatives. – h3nr1ke Apr 20 '18 at 21:28
  • You should set you own answer as the correct answer since, you know, is the one that worked for you. Also, people looking for this information will know easily that this post has a correct answer. – ikerbera Feb 11 '19 at 07:21