2

How to add zero byte padding in phpseclib with encryption method 3des in EBC mode?

Here my current code:

$cipher = new TripleDES(TripleDES::MODE_ECB);
$cipher->setKey('1234567890ABCDEFGHIJKLMN');
oentoro
  • 740
  • 1
  • 11
  • 32
  • How about `$cipher->enablePadding();`? Since `TripleDES` extends `DES` extends `BlockCipher` extends `SymmetricKey`, you might use the `enablePadding()` function defined in `SymmetricKey` class: https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Crypt/Common/SymmetricKey.php. Didn't actually try, though. – Raptor Feb 25 '19 at 06:14
  • can we customize padding character in enablePadding method? for example $cipher -> enablePadding('\0'); – oentoro Feb 25 '19 at 07:15

1 Answers1

2

Just figure it out:

  1. Disable padding:

    $cipher->disablePadding();
    
  2. Manually pad message:

    $message_padded = $urlencoded;
    if (strlen($message_padded) % 8) {
        $message_padded = str_pad($message_padded,
            strlen($message_padded) + 8 - strlen($message_padded) % 8, "\0");
    }
    
  3. encrypt:

    $result = $cipher -> encrypt($message_padded);
    
Raptor
  • 53,206
  • 45
  • 230
  • 366
oentoro
  • 740
  • 1
  • 11
  • 32
  • Your approach looks okay, but why do you want to do so? Btw, you can add a new method to wrap the above codes, so that you can reuse the customized padding method easily. – Raptor Feb 26 '19 at 06:57
  • 1
    1. Because there is a service(partner) that require the enryption method above. 2. I'll try it, thank you for the advice – oentoro Feb 26 '19 at 07:56