AES 256 encryption between .NET and PHP

I finally got around to creating some sample code to demonstrate how to do Rijndael (AES-256) encryption between PHP and .NET servers.

They key probelm I found was that mcrypt pads the key with zeroes, where the default behaviour in .NET is PKCS7 padding. Use this line in your/net project to mimic the PHP behaviour.

symmetricKey.Padding = PaddingMode.Zeros;



You also have to remove the padded nulls from the end of string after you do the decryption:

result= result.TrimEnd('\0');



The complete working C# and PHP projects are here:
https://github.com/dchymko/.NET–PHP-encryption

 

 

One Response to AES 256 encryption between .NET and PHP
  1. pepepaco

    Hello, this is a great example, I wonder if its possible to do exactly the same but using Silverlight and Perl.

    regards

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>