How to encrypt with Twofish using ECB cipher mode and PKCS7 block padding mode?

331 views Asked by At

I want to access an API with PHP. The specifications are:

  • Twofish algorithm
  • ECB cipher mode
  • PKCS7 block padding mode

I have tried a lot of different PHP functions and libraries, but none seems to work.

Here is my code:

function encrypt($data, $key)
{
  // Pad for PKCS7
  $blockSize = mcrypt_get_block_size(MCRYPT_TWOFISH, MCRYPT_MODE_ECB);
  $len = strlen($data);
  $pad = $blockSize - ($len % $blockSize);
  $data .= str_repeat(chr($pad), $pad);

  $encryptedData = mcrypt_encrypt( MCRYPT_TWOFISH, $key, $data, MCRYPT_MODE_ECB);

  return $encryptedData;
}

Do you see a problem with this code?

1

There are 1 answers

2
Luke Joshua Park On

mcrypt_encrypt does not support PKCS7 padding. It has also been deprecated for a significant period of time.

If the API you want to talk to is using Twofish and ECB mode then you probably don't want to use that API at all - if they're happy to whack an incredibly insecure encryption scheme together for the front-facing API then the rest of their codebase is probably pretty shammy too.