Skip to content

ASCrypt/php/classes/XXTEA.php - str2long may not process entire string #7

Description

@NewEraCracker

str2long processes each 4 bytes into a 32 bit integer without checking for any possible remainder. This causes strings with, let's say for example, 142 chars to be processed only up to the 140th char.

A quick workaround to this problem is appending nullbytes as padding. Things like this are handled in a javascript implementation of the same encryption technique.

https://html-encrypter.googlecode.com/svn/trunk/hea5.js
(The only difference, is that this implementation base64's the final result, apart from that, it is much similar to yours)

So I'd suggest the following changes

    /**
     * Converts string to long array.
     */
    private static function str2long($s)
    {
        // Fill last 4-char block
        $i = strlen($s);
        $r = $i % 4;
        if($r != 0) $s = str_pad($s, $i + 4 - $r, "\0");

        // Convert and return
        return array_values(unpack('V*', $s));
    }
    /**
     * Converts long array to string.
     */
    private static function long2str($v)
    {
        // Convert
        $s = '';
        for ($i = 0; $i < count($v); $i++)
        {
            $s .= pack('V', $v[$i]);
        }

        // Strip trailing null chars resulting from filling last 4-char block and return
        return rtrim($s, "\0");
    }

Regards,
NewEraCracker

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions