-1

this is the first time I have come across this error and I am very confused as to how to fix this error. I will post my code below. Kindly request for whatever else is needed to facilitate in this. Any help will be greatly appreciated.

    function encryptAes($string, $key)
{
    $string = $this->addPKCS5Padding($string);
    $crypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $key);
    return  strtoupper(bin2hex($crypt));
}

UPDATE: code on line number 777

    function _encode_crypt($post = NULL)
{
    return "@".$this->encryptAes($post,$this->encryption_password);
}
DEV
  • 647
  • 4
  • 19
  • 31

1 Answers1

0

I'm going to assume the error you're getting is accurate and that you've actually made an honest mistake in having your encryption key ($key) and your initialisation vector (also $key) as the same value. This is wrong, in so many ways...

Instead try this:

$iv = mcrypt_create_iv(
    mcrypt_get_iv_size(
        MCRYPT_RIJNDAEL_128,
        MCRYPT_MODE_CBC
    ),
    MCRYPT_DEV_URANDOM
);

encryptAes('blah blah', 'some super secret key', $iv);

function encryptAes($string, $key, $iv)
{
    $string = $this->addPKCS5Padding($string);
    $crypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $iv);
    return  strtoupper(bin2hex($crypt));
}

You will need to store this initialisation vector somewhere to be able to decrypt.

Obviously, the $key value you were passing through was the incorrect length for the initialisation vector of the encryption algorithm you are using. You are using MCRYPT_RIJNDAEL_128 and as such the iv size should be 128 bits (16 bytes).

Narf
  • 14,600
  • 3
  • 37
  • 66
Garry Welding
  • 3,599
  • 1
  • 29
  • 46
  • `Parse error: syntax error, unexpected '$iv' (T_VARIABLE), expecting function (T_FUNCTION)` Getting this error now. With the exact thing above – DEV Mar 08 '16 at 11:36
  • You've copied something wrong then, or missed out part of the code as I've just tested it and it works fine. – Garry Welding Mar 08 '16 at 11:38
  • `Message: Missing argument 3 for sagepay_server::encryptAes() 'path/to/file' in line number 777 and defined` Now this, I will post the code present in line number 777, in the question – DEV Mar 08 '16 at 11:41
  • you're not passing through the $iv like I said above. you notice my example now says... encryptAes('blah blah', 'some super secret key', $iv); – Garry Welding Mar 08 '16 at 11:46
  • `encryptAes('blah blah', 'some super secret key', 'test');` No error but the button isn't appearing. – DEV Mar 08 '16 at 11:51
  • the IV cannot be 'test'... you need to read my answer about what size the $iv value needs to be... – Garry Welding Mar 08 '16 at 11:54
  • As I explained, I am new to mcrypt. What does the `MCRYPT_RAND` do? – DEV Mar 08 '16 at 11:55
  • Go take a look at http://php.net/manual/en/function.mcrypt-create-iv.php, it explains there. – Garry Welding Mar 08 '16 at 14:11