Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
NewEraCracker opened this issue Feb 28, 2015 · 0 comments

Comments

@NewEraCracker
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant