CodingPleasure

cultivate passion for everything else that goes on around programming

C# vs PHP Encryption using Rijndael/AES

While trying to encrypt and decrypt some strings in PHP and in C# using the same algorithm, I wasn’t able to found a good hint on the Internet. After some research I came to the following conclusions:

The following encryption codes are equivalent and will produce the same result. But with one additional note, worth to mention: Although in PHP the key’s length is not important, in C# you must use a key with the length of 32 bytes!

   1: $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
   2: $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
   3: $key = $pwd_key;//"This is a very secret key";
   4: return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv));
   1:  
   2:             // set up encrytion object
   3:             RijndaelManaged aes256 = new RijndaelManaged();
   4:             aes256.KeySize = 256;
   5:             aes256.BlockSize = 256;
   6:             aes256.Padding = PaddingMode.Zeros;
   7:             aes256.Mode = CipherMode.ECB;
   8:             aes256.Key = ASCII_ENCODING.GetBytes(passKey);
   9:             aes256.GenerateIV();
  10:  
  11:             // form the string for encrypting
  12:             // and put into byte array
  13:             byte[] plainTextBytes = ASCII_ENCODING.GetBytes(message);
  14:  
  15:             // encrypt the data            
  16:             ICryptoTransform encryptor = aes256.CreateEncryptor();
  17:             MemoryStream ms = new MemoryStream();
  18:  
  19:             CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
  20:             StreamWriter mSWriter = new StreamWriter(cs);
  21:  
  22:             mSWriter.Write(message);
  23:             mSWriter.Flush();
  24:             cs.FlushFinalBlock();
  25:  
  26:             // convert our encrypted data from a memory stream into a byte array.
  27:             byte[] cypherTextBytes = ms.ToArray();
  28:  
  29:             // close memory stream
  30:             ms.Close();
  31:  
  32:             return Convert.ToBase64String(cypherTextBytes);

Additional resources:

Usefull resource related to the same topic

Example of code conversion on stackoverflow.com

Advertisements

3 responses to “C# vs PHP Encryption using Rijndael/AES

  1. Pedro April 9, 2011 at 18:29

    Thanks pal : )

  2. Giacomo April 15, 2015 at 16:25

    “Although in PHP the key’s length is not important, in C# you must use a key with the length of 32 “. This info saved my life! Thanks mate

  3. sarju kabariya August 20, 2015 at 08:40

    Thanks dude..:)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: