0

I've found a few answers to Encrypt in PHP, and Decrypt in C#, but as yet have been unable to reverse the process...

The background is I want to:

In C#: AES encrypt a file's contents. Upload the data (likely via http via POST) to a server.

In PHP: Receive and save the file.

And in PHP (at a later date): Decrypt the file.

I specifically want to encrypt it outside of using SSL/TLS (though I might have to do this as well), as I need to know the file remains encrypted (and decryptable!) when stored on the server.

To encrypt in C# I'm using:

Rijndael RijndaelAlg = Rijndael.Create();
RijndaelAlg.KeySize = 128;
RijndaelAlg.Mode = CipherMode.CBC;
CryptoStream cStream = new CryptoStream(fStream, RijndaelAlg.CreateEncryptor(Key, IV),
                                        CryptoStreamMode.Read);

and to decrypt in PHP:

 mcrypt_cbc(MCRYPT_RIJNDAEL_128, $key, $buffer, MCRYPT_DECRYPT, $iv);
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Tim
  • 48
  • 1
  • 5
  • 3
    Can you explain exactly where you are having problems, or what your question is? – ZoFreX Nov 16 '10 at 09:33
  • Googling for PHP AES turned up http://www.phpaes.com/ though I can't vouch for how well it works as I've never used it. Might be useful for you though. Also, the mhash extension supports many encryption methods, though AES is not among them. If you are allowed to use an encryption method other than AES then your best bet is probably picking a scheme that's present both in C# and in the mhash extension. – GordonM Nov 16 '10 at 11:48
  • 1
    Can you build a test case for us? Encrypt a simple string in C#. Then, encrypt the same string with your current AES implementation in PHP and post it. Post your keys as well. I can compare against my implementations and try to figure out which one is working, and which isn't. – Brad Nov 16 '10 at 15:11
  • I've tried a number of approaches in C#, been able to 'encrypt' the file, but never managed to succesfully decrypt it in php. Not sure therefore which end is not working. The perfect answer would be one line of c# showing encrypt, and one line of php showing decrypt. – Tim Nov 20 '10 at 21:56
  • Hi to be more precise, I need the line of C# code which would encrypt to allow me to decrypt in PHP using "mcrypt_cbc(MCRYPT_RIJNDAEL_128, $key, $buffer, MCRYPT_DECRYPT, $iv);" please? – Tim Nov 22 '10 at 13:39

4 Answers4

1

Generally it only depends on selecting the right options on both sides:

  • Plaintext character format

    how plaintext characters are encoded in the bit string

  • Padding

    how to pad the plaintext to be an exact multiple of the block size

  • Key length

    must be agreed if there is a choice

  • Key derivation

    how to create the bit string to be used for the key

  • Mode

    which mode of encryption to use

  • Storage format

    how we store the ciphertext

Please see here for a lot of information about these things. Especially the padding seems to be the root of most interoperability problems as PHP's mcrypt uses a NULL-padding by default and has no built-in support for any other padding mode, while e.g. .NET doesn't even provide an option to use a NULL-padding (as it may cause issues when encrypting binary data).

Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
1

I know this was asked a while ago but I thought I'd post my solution for others. I wrote up a quick code example in PHP and C# that lets you encrypt/decrypt both ways. I had a few issues with getting the settings on both sides to work out. A difference in padding would let it decrypt one way but not the other

https://github.com/dchymko/.NET--PHP-encryption

hope that helps some people.

DarylChymko
  • 1,048
  • 1
  • 8
  • 11
0

Are you using the same mode with both? I.e. are you using CBC with both (and not ECB). If you don't understand what I just said then drop a comment and I'll explain in detail, as it has fairly major security repercussions.

ZoFreX
  • 8,812
  • 5
  • 31
  • 51
  • Hi, yes I think I am, to encrypt in C# I'm using: Rijndael RijndaelAlg = Rijndael.Create(); RijndaelAlg.KeySize = 128; RijndaelAlg.Mode = CipherMode.CBC; CryptoStream cStream = new CryptoStream(fStream, RijndaelAlg.CreateEncryptor(Key, IV), CryptoStreamMode.Read); and to decrypt in PHP: mcrypt_cbc(MCRYPT_RIJNDAEL_128, $key, $buffer, MCRYPT_DECRYPT, $iv); – Tim Nov 23 '10 at 12:34
0

I had a similar problem a few months ago - I had a project that had to use AES encryption and I had to make sure that the exact same algorithm is used between a C# and A C++ component. I ended up implementing a shared DLL library used by both based on the AES crypto wrapper from this codeplex article:

http://www.codeproject.com/KB/security/WinAESwithHMAC.aspx

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335