views:

235

answers:

2

Hello,

I have the following .Net code which takes two inputs. 1) A 128 bit base 64 encoded key and 2) the userid. It outputs the AES encrypted token. I need the php equivalent of the same code, but dont know which corresponding php classes are to be used for RNGCryptoServiceProvider,RijndaelManaged,ICryptoTransform,MemoryStream and CryptoStream. Im stuck so any help regarding this would be really appreciated.

using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;

class AESToken
{
  [STAThread]
  static int Main(string[] args)
  {
    if (args.Length != 2)
    {
      Console.WriteLine("Usage: AESToken key userId\n");
      Console.WriteLine("key Specifies 128-bit AES key base64 encoded supplied by MediaNet to the partner");
      Console.WriteLine("userId specifies the unique id");
      return -1;
    }

    string key = args[0];
    string userId = args[1];

    StringBuilder sb = new StringBuilder();
    // This example code uses the magic string “CAMB2B”. The implementer
    // must use the appropriate magic string for the web services API.
    sb.Append("CAMB2B");
    sb.Append(args[1]);     // userId
    sb.Append('|');  // pipe char
    sb.Append(System.DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ssUTC")); //timestamp
    Byte[] payload = Encoding.ASCII.GetBytes(sb.ToString());

    byte[] salt = new Byte[16];  // 16 bytes of random salt
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    rng.GetBytes(salt);  // the plaintext is 16 bytes of salt followed by the payload.

    byte[] plaintext = new byte[salt.Length + payload.Length];
    salt.CopyTo(plaintext, 0);
    payload.CopyTo(plaintext, salt.Length);

    // the AES cryptor: 128-bit key, 128-bit block size, CBC mode
    RijndaelManaged cryptor = new RijndaelManaged();
    cryptor.KeySize = 128;
    cryptor.BlockSize = 128;
    cryptor.Mode = CipherMode.CBC;
    cryptor.GenerateIV();
    cryptor.Key = Convert.FromBase64String(args[0]); // the key
    byte[] iv = cryptor.IV;   // the IV.

    // do the encryption
    ICryptoTransform encryptor = cryptor.CreateEncryptor(cryptor.Key, iv);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
    cs.Write(plaintext, 0, plaintext.Length);
    cs.FlushFinalBlock();
    byte[] ciphertext = ms.ToArray();
    ms.Close();
    cs.Close();

    // build the token
    byte[] tokenBytes = new byte[iv.Length + ciphertext.Length];
    iv.CopyTo(tokenBytes, 0);
    ciphertext.CopyTo(tokenBytes, iv.Length);
    string token = Convert.ToBase64String(tokenBytes);
    Console.WriteLine(token);
    return 0;
   }
}

Please help. Thank You.

A: 

You would use the mcrypt library in PHP to implement the same functionality.

Yann Ramin
A: 

We are also trying figure out the same C# in PHP. You can post your code without the key.

First approach:

    // Open the cipher:
    // Using Rijndael 128 in CBC mode.
    $m = mcrypt_module_open('rijndael-128', '', 'cbc', ''); 
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($m), MCRYPT_RAND); 

    // Initialize the encryption:
    mcrypt_generic_init($m, (base64_decode($key_)), $iv);

    // Encrypt the data:
    $cryptext = mcrypt_generic($m, $plain_text);

    //echo "IV SIZE ".mcrypt_enc_get_iv_size($m);
    $tx2 =  base64_encode($iv.$cipherText);

    // Close the encryption handler:
    mcrypt_generic_deinit($m);

    // Close the cipher:
    mcrypt_module_close($m);

Second approach for initialization:

    $m = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    $iv_size = mcrypt_enc_get_iv_size($m); 
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($m), MCRYPT_RAND); 
    $key128 = base64_decode($key_);

    // Encrypt the data:
    $cryptext = mcrypt_generic($m, $plain_text);

    $tx2 =  base64_encode($iv.$cipherText);

    // Close the encryption handler:
    mcrypt_generic_deinit($m); 
MCRYPT_RIJNDAEL