简体   繁体   中英

AES Encrypt and Decrypt between C# and PHP MCrypt

I seem to be having problems getting an AES256 string to decode between a PHP and .NET application. I get an error in the .Net application stating "Padding is invalid and cannot be removed." This error fires in the using statement for the CrytoStream. The workflow is pretty straight forward. The PHP application encrypts a value and passes it to the .NET application as a URL parameter. The .NET application needs to decrypt that value for later use. The .NET method works from .NET to .NET, but PHP to .NET is the problem.

The PHP code:

   function encrypt($text) {
    $key = "M2AZULUALPHA";
    $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $padding = $block - (strlen($text) % $block);
    $text .= str_repeat(chr($padding), $padding);

    $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, 'TripBuilder2017');
    return base64_encode($crypttext);
 }

The .NET Decrypt method:

    private string Decrypt(string cipherText)
    {
        string EncryptionKey = "M2AZULUALPHA";
        byte[] saltArray = Encoding.ASCII.GetBytes("TripBuilder2017");

        cipherText = cipherText.Replace(" ", "+");
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, saltArray);
            encryptor.KeySize = 256;
            encryptor.Padding = PaddingMode.PKCS7;
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);

            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return cipherText;
    }

UPDATE I changed the mode in PHP to Rijndael256 and I also changed AES to RijndaelManaged in .NET. Again, I am able to get this working between .NET applications, but not with the PHP application. I am wondering if there is an issue with the padding the PHP application is using.

I updated answer completely after messages :-)

php:

function encrypt($text) 
    {

    //$key = "M2AZULUALPHA"; //  type 2 -- mcrypt_encrypt(): Key of size 12 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported -- at line 7
    $key = "M2AZULUALPHA1234";
    //$vi ='TripBuilder2017'; // type 2 -- mcrypt_encrypt(): Received initialization vector of size 15, but size 16 is required for this encryption mode -- at line 9
    $vi ='TripBuilder20170';
    $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $padding = $block - (strlen($text) % $block);
    $text .= str_repeat(chr($padding), $padding);
    $crypttext = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $vi));
    return $crypttext;
    }

    $result = encrypt("abcdcddsfdafdfe");
    echo"$result";

output is "pPPH8amRhqbdX6D83jr74A=="

c# with RijndaelManaged

public String Decrypt(string cipherText)
        {
            var result = "";
            var cypher = Convert.FromBase64String(cipherText);
            var encoding = System.Text.Encoding.UTF8;
            var Key = encoding.GetBytes("M2AZULUALPHA1234");
            var IV = encoding.GetBytes("TripBuilder20170"); 
            using (var rj = new RijndaelManaged())
            {
                rj.Padding = PaddingMode.PKCS7;
                rj.Mode = CipherMode.CBC;
                rj.KeySize = 256;
                rj.Key = Key;
                rj.IV = IV;
                var ms = new MemoryStream(cypher);
                using (var cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Read))
                using (var sr = new StreamReader(cs))
                    result = sr.ReadToEnd();
            }

            return result;
        }

test:

var result = Decrypt("pPPH8amRhqbdX6D83jr74A==");
Debug.WriteLine(result);

output is "abcdcddsfdafdfe"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM