Please do not mark it as duplicate. I have already gone through almost all the previous post for same issue, but was not able to tackle my error.  

I am trying to upgrade the Sagepay Protocol from v2.23 to v3.00  

I have also deployed PHP kit provided by Sagepay and getting the same error.  

Below is the AES encryption I am using to make it compatible for v3.00 in my includes.php  

Any guidance would be much appreciated. 

```
<?

/* Base 64 Encoding function **
** PHP does it natively but just for consistency and ease of maintenance, let's declare our own function **/
function base64Encode($plain) {
  // Initialise output variable
  $output = "";

  // Do encoding
  $output = base64_encode($plain);

  // Return the result
  return $output;
}

/* Base 64 decoding function **
** PHP does it natively but just for consistency and ease of maintenance, let's declare our own function **/
function base64Decode($scrambled) {
  // Initialise output variable
  $output = "";

  // Fix plus to space conversion issue
  $scrambled = str_replace(" ","+",$scrambled);

  // Do encoding
  $output = base64_decode($scrambled);

  // Return the result
  return $output;
}

/*  The SimpleXor encryption algorithm                                                                                **
**  NOTE: This is a placeholder really.  Future releases of Form will use AES or TwoFish.  Proper encryption      **
**  This simple function and the Base64 will deter script kiddies and prevent the "View Source" type tampering        **
**  It won't stop a half decent hacker though, but the most they could do is change the amount field to something     **
**  else, so provided the vendor checks the reports and compares amounts, there is no harm done.  It's still          **
**  more secure than the other PSPs who don't both encrypting their forms at all                                      */

function simpleXor($InString, $Key) {
  // Initialise key array
  $KeyList = array();
  // Initialise out variable
  $output = "";

  // Convert $Key into array of ASCII values
  for($i = 0; $i < strlen($Key); $i++){
    $KeyList[$i] = ord(substr($Key, $i, 1));
  }

  // Step through string a character at a time
  for($i = 0; $i < strlen($InString); $i++) {
    // Get ASCII code from string, get ASCII code from key (loop through with MOD), XOR the two, get the character from the result
    // % is MOD (modulus), ^ is XOR
    $output.= chr(ord(substr($InString, $i, 1)) ^ ($KeyList[$i % strlen($Key)]));
  }

  // Return the result
  return $output;
}

//** Wrapper function do encrypt an encode based on strEncryptionType setting **
function encryptAndEncode($strPost) {

        global $strEncryptionType
              ,$strEncryptionPassword;

        if ($strEncryptionType=="XOR") 
        {
                //** XOR encryption with Base64 encoding **
                return base64Encode(simpleXor($strPost,$strEncryptionPassword));
        } 
        else 
        {
                //** AES encryption, CBC blocking with PKCS5 padding then HEX encoding - DEFAULT **

                //** use initialization vector (IV) set from $strEncryptionPassword
        $strIV = $strEncryptionPassword;

        //** add PKCS5 padding to the text to be encypted
        $strPost = addPKCS5Padding($strPost);

        //** perform encryption with PHP's MCRYPT module
                $strCrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $strEncryptionPassword, $strPost, MCRYPT_MODE_CBC, $strIV);

                //** perform hex encoding and return
                return "@" . bin2hex($strCrypt);
        }
}

//** Wrapper function do decode then decrypt based on header of the encrypted field **
function decodeAndDecrypt($strPost) {

        global $strEncryptionPassword;

        if (substr($strPost,0,1)=="@") 
        {
                //** HEX decoding then AES decryption, CBC blocking with PKCS5 padding - DEFAULT **

                //** use initialization vector (IV) set from $strEncryptionPassword
        $strIV = $strEncryptionPassword;

        //** remove the first char which is @ to flag this is AES encrypted
        $strPost = substr($strPost,1); 

        //** HEX decoding
        $strPost = pack('H*', $strPost);

        //** perform decryption with PHP's MCRYPT module
                return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $strEncryptionPassword, $strPost, MCRYPT_MODE_CBC, $strIV); 
        } 
        else 
        {
                //** Base 64 decoding plus XOR decryption **
                return simpleXor(base64Decode($strPost),$strEncryptionPassword);
        }
}

//** PHP's mcrypt does not have built in PKCS5 Padding, so we use this
function addPKCS5Padding($input)
{
   $blocksize = 16;
   $padding = "";

   // Pad input to an even block size boundary
   $padlength = $blocksize - (strlen($input) % $blocksize);
   for($i = 1; $i <= $padlength; $i++) {
      $padding .= chr($padlength);
   }

   return $input . $padding;
}

/*************
function pkcs5_pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        //echo "<br/>Padding:".str_repeat(chr($pad), $pad)."<";
        return $text . str_repeat(chr($pad), $pad);
    }

    function encryptFieldData($input)
    {
        $key = "[mykey]";
        $iv = $key;

        $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_CBC, "");
        if (mcrypt_generic_init($cipher, $key, $iv) != -1)
        {
            $cipherText = mcrypt_generic($cipher,$input );
            mcrypt_generic_deinit($cipher);

            $enc = bin2hex($cipherText);
        }
        return $enc;
    }
    $str = "Currency=GBP";
    $datapadded = pkcs5_pad($str,16);
    $cryptpadded = "@" . encryptFieldData($datapadded);
*************************/

?>

```