Replicating the same AES encryption with C # in PHP

I am working on a project where I have a C # application that has an encryption class that can encrypt and decrypt a string value. Now I want the web interface to work with my C # application using PHP.

I am trying to do the same encryption that my C # project does on my PHP website, but I cannot decide what I need to do.

Below is the code for my C # application.

public static string encrypt(string encryptionString) { byte[] clearTextBytes = Encoding.UTF8.GetBytes(encryptionString); SymmetricAlgorithm rijn = SymmetricAlgorithm.Create(); MemoryStream ms = new MemoryStream(); byte[] rgbIV = Encoding.ASCII.GetBytes("PRIVATE"); byte[] key = Encoding.ASCII.GetBytes("PRIVATE"); CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write); cs.Write(clearTextBytes, 0, clearTextBytes.Length); cs.Close(); return Convert.ToBase64String(ms.ToArray()); } 

I am trying to use the following code in my PHP web interface

 define("CIPHERKEY", "PRIVATE"); function encrypt($data) { //$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_ECB, ''); $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); //$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($cipher), MCRYPT_RAND); $iv = 'PRIVATE'; //$key = substr(CIPHERKEY, 0, mcrypt_enc_get_key_size($cipher)); $key =CIPHERKEY; if (mcrypt_generic_init($cipher, $key, $iv) != 1) { $cipherData = mcrypt_generic($cipher, $data); mcrypt_generic_deinit($cipher); mcrypt_module_close($cipher); $sanitizedCipherData = trim(base64_encode($cipherData)); return $sanitizedCipherData; } } 

I tried various options, but can't find the right way to do this.

the iv variable uses the same key as the rgbIV variable in the C # application, and CIPHERKEY in the PHP web interface uses the same key as the key variable in my C # application.

Thanks for any help you can provide.

UPDATE At the moment, I continue to get different results. I am testing it by passing the password string.

In the current code above in PHP, I get NHHloywxlybbANIH5dS7SQ== as an encrypted string.

However, with the same line, I get the result n86Mwc5MRXzhT3v3A/uxEA==

+4
source share
2 answers

The reason you get different results is because the default Cipher mode in C # is CBC, while in PHP you use ECB mode. See Wikipedia for information on two different modes.

CBC is safer than ECB, so I recommend sticking with the standard .NET implementation and modifying your PHP code to use CBC, however you have two options.

Option 1 Change .NET to use ECB mode (if you have any legacy code and you need to use it). But please read about it, ECB mode will leave artifacts in your encrypted text and allow attackers to gain some knowledge about what you have encrypted (see the Penguin image in the Wikipedia article).

To change the .NET code to use ECB, simply add a line for the mode:

 // Start of your code ... SymmetricAlgorithm rijn = SymmetricAlgorithm.Create(); rijn.Mode = CipherMode.ECB; MemoryStream ms = new MemoryStream(); byte[] rgbIV = Encoding.ASCII.GetBytes("PRIVATE"); // Rest of your code ... 

Option 2 - change your PHP script to use CBC mode

 $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); 

Update I checked this in more detail and found that you also need to add an add-on to your plain text. The following code will give you a match:

PHP code:

 function encrypt($data) { $iv = "AAAAAAAAAAAAAAAA"; $key = CIPHERKEY; return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, addpadding($data), MCRYPT_MODE_CBC, $iv)); } function addpadding($string, $blocksize = 16) { $len = strlen($string); $pad = $blocksize - ($len % $blocksize); $string .= str_repeat(chr($pad), $pad); return $string; } 

C # code will automatically add a PKCS7-based add-on.

Update 2 Filling the strip: As indicated in the comments, the add-on should be removed after decryption.

 function strippadding($string) { $slast = ord(substr($string, -1)); $slastc = chr($slast); $pcheck = substr($string, -$slast); if(preg_match("/$slastc{".$slast."}/", $string)){ $string = substr($string, 0, strlen($string)-$slast); return $string; } else { return false; } } 
+3
source

try the following:

  function encrypt_str($str) { $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_DEV_URANDOM); $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, "PRIVATE", $str, MCRYPT_MODE_ECB, $iv); return rtrim($encrypted); } 

And add this to your C #

 rijn.Mode = CipherMode.ECB; 
+1
source

All Articles