Mellivora/Encryption/Crypt.php
protected function padText($text, $mode, $blockSize, $paddingType)
    {
        $paddingSize = 0;
        $padding     = '';

        if ($mode === 'cbc' || $mode === 'ecb') {
            $paddingSize = $blockSize - (strlen($text) % $blockSize);
            if ($paddingSize >= 256) {
                throw new InvalidArgumentException("Block size [$blockSize] is bigger than 256");
            }

            switch ($paddingType) {
                case self::PADDING_ANSI_X_923:
                    $padding = str_repeat(chr(0), $paddingSize - 1) . chr($paddingSize);

                    break;
                case self::PADDING_PKCS7:
                    $padding = str_repeat(chr($paddingSize), $paddingSize);

                    break;
                case self::PADDING_ISO_10126:
                    $padding = '';
                    foreach (range(0, $paddingSize - 2) as $i) {
                        $padding .= chr(rand());
                    }
                    $padding .= chr($paddingSize);

                    break;
                case self::PADDING_ISO_IEC_7816_4:
                    $padding = chr(0x80) . str_repeat(chr(0), $paddingSize - 1);

                    break;
                case self::PADDING_ZERO:
                    $padding = str_repeat(chr(0), $paddingSize);

                    break;
                case self::PADDING_SPACE:
                    $padding = str_repeat(' ', $paddingSize);

                    break;
                default:
                    $paddingSize = 0;

                    break;
            }
        }

        if (!$paddingSize) {
            return $text;
        }

        if ($paddingSize > $blockSize) {
            throw new InvalidArgumentException("Invalid padding size [$paddingSize]");
        }

        return $text . substr($padding, 0, $paddingSize);
    }
protected function padText($text, $mode, $blockSize, $paddingType)
    {
        $paddingSize = 0;
        $padding     = '';

        if ($mode === 'cbc' || $mode === 'ecb') {
            $paddingSize = $blockSize - (strlen($text) % $blockSize);
            if ($paddingSize >= 256) {
                throw new InvalidArgumentException("Block size [$blockSize] is bigger than 256");
            }

            switch ($paddingType) {
                case self::PADDING_ANSI_X_923:
                    $padding = str_repeat(chr(0), $paddingSize - 1) . chr($paddingSize);

                    break;
                case self::PADDING_PKCS7:
                    $padding = str_repeat(chr($paddingSize), $paddingSize);

                    break;
                case self::PADDING_ISO_10126:
                    $padding = '';
                    foreach (range(0, $paddingSize - 2) as $i) {
                        $padding .= chr(rand());
                    }
                    $padding .= chr($paddingSize);

                    break;
                case self::PADDING_ISO_IEC_7816_4:
                    $padding = chr(0x80) . str_repeat(chr(0), $paddingSize - 1);

                    break;
                case self::PADDING_ZERO:
                    $padding = str_repeat(chr(0), $paddingSize);

                    break;
                case self::PADDING_SPACE:
                    $padding = str_repeat(' ', $paddingSize);

                    break;
                default:
                    $paddingSize = 0;

                    break;
            }
        }

        if (!$paddingSize) {
            return $text;
        }

        if ($paddingSize > $blockSize) {
            throw new InvalidArgumentException("Invalid padding size [$paddingSize]");
        }

        return $text . substr($padding, 0, $paddingSize);
    }
protected function padText ( $ text , $ mode , $ blockSize , $ paddingType ) { $ paddingSize = 0 ; $ padding = '' ; if ( $ mode === 'cbc' || $ mode === 'ecb' ) { $ paddingSize = $ blockSize - ( strlen ( $ text ) % $ blockSize ) ; if ( $ paddingSize >= 256 ) { throw new InvalidArgumentException ( "Block size [$blockSize] is bigger than 256" ) ; } switch ( $ paddingType ) { case self :: PADDING_ANSI_X_923 : $ padding = str_repeat ( chr ( 0 ) , $ paddingSize - 1 ) . chr ( $ paddingSize ) ; break ; case self :: PADDING_PKCS7 : $ padding = str_repeat ( chr ( $ paddingSize ) , $ paddingSize ) ; break ; case self :: PADDING_ISO_10126 : $ padding = '' ; foreach ( range ( 0 , $ paddingSize - 2 ) as $ i ) { $ padding .= chr ( rand ( ) ) ; } $ padding .= chr ( $ paddingSize ) ; break ; case self :: PADDING_ISO_IEC_7816_4 : $ padding = chr ( 0x80 ) . str_repeat ( chr ( 0 ) , $ paddingSize - 1 ) ; break ; case self :: PADDING_ZERO : $ padding = str_repeat ( chr ( 0 ) , $ paddingSize ) ; break ; case self :: PADDING_SPACE : $ padding = str_repeat ( ' ' , $ paddingSize ) ; break ; default : $ paddingSize = 0 ; break ; } } if ( ! $ paddingSize ) { return $ text ; } if ( $ paddingSize > $ blockSize ) { throw new InvalidArgumentException ( "Invalid padding size [$paddingSize]" ) ; } return $ text . substr ( $ padding , 0 , $ paddingSize ) ; }
Pads texts before encryption

@see http://www.di-mgt.com.au/cryptopad.html

@param mixed $text
@param mixed $mode
@param mixed $blockSize
@param mixed $paddingType
Pads texts before encryption