title: AES/CBC/PKCS#5 Encryption Algorithm In PHP
body: <p>Im attempting to integrate the SagePay payment gateway into a website using the 'Form Integration' method.</p>
  
  <p>Basically the form integration method works by inserting a FORM in a webpage and POSTing information to SagePay's servers whenever the Submit button for the FORM is selected. Before the information can be sent to SagePay's servers it must be encrypted using the AES/CBC/PKCS#5 algorithm, before being Base 64 encoded.</p>
  
  <p>I have a basic knowledge of encryption but I have no experience of using it in PHP. Can anyone please help me formulate the AES/CBC/PKCS#5 algorithm in PHP please?</p>
  
  <p>Here's my efforts so far:</p>
  
  <pre><code>$CRYPT = "Text Goes Here";
  
  $blocksize = 16;//Does 16 Refer To 16 Bytes Or 16 Bits? 16 Bytes = 128 Bits.
  $cryptLength = strlen($CRYPT);
  $cryptLengthModuloBlocksize = $cryptLength % $blocksize;
  $pad = $blocksize - $cryptLengthModuloBlocksize;
  $padString = str_repeat(chr($pad), $pad);
  $CRYPT = $CRYPT . $padString;
  
  $encryptionPassword = 'password';
  $Encrypted_CRYPT = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $encryptionPassword, $CRYPT, MCRYPT_MODE_CBC);
  
  $Base64_Encrypted_CRYPT = base64_encode($Encrypted_CRYPT);
  
  echo    "&lt;form action = 'https://test.sagepay.com/gateway/service/vspform-register.vsp' method = 'POST'&gt;
  
              &lt;input type = 'hidden' name = 'VPSProtocol' value = '3.00' /&gt;//
              &lt;input type = 'hidden' name = 'TxType' value = 'PAYMENT' /&gt;
              &lt;input type = 'hidden' name = 'Vendor' value = 'vendorName' /&gt;
              &lt;input type = 'hidden' name = 'Crypt' value = '" . $Base64_Encrypted_CRYPT . "' /&gt;
              &lt;input type= 'submit' value = 'Submit'&gt;
  
          &lt;/form&gt;";
  </code></pre>
  
  <p>Any help is much appreciated.</p>