Question

I'm trying to create a simple Encryption class but the resulting string is being padded with non-ascii characters. I've tried rtrim(), converting to utf8, etc. as mentioned in some other answers. What exactly am I missing? Here is what the characters show up when pasting the results into Notepad++

enter image description here

Pastebin containing the characters here. Appears as 't' in html, but copying that into notepad shows the random bits of data after it.

class Crypter implements ICrypter {

private $Key;
private $Algo;

public function __construct($Algo = MCRYPT_BLOWFISH) {

    $this->Key = substr('key', 0, mcrypt_get_key_size($Algo, MCRYPT_MODE_ECB));
    $this->Algo = $Algo;

}

public function Encrypt($data) {

    //$iv_size = mcrypt_get_iv_size($this->Algo, MCRYPT_MODE_ECB);
    //$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

    $blocksize = mcrypt_get_block_size('blowfish', 'ecb'); // get block size
    $pkcs = $blocksize - (strlen($data) % $blocksize); // get pkcs5 pad length
    $data.= str_repeat(chr($pkcs), $pkcs); // append pkcs5 padding to the data


    $crypt = mcrypt_encrypt($this->Algo, $this->Key, $data, MCRYPT_MODE_ECB);
    return rtrim(base64_encode($crypt));

}

public function Decrypt($data) {

    $crypt = base64_decode($data);
    $iv_size = mcrypt_get_iv_size($this->Algo, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $decrypt = mcrypt_decrypt($this->Algo, $this->Key, $crypt, MCRYPT_MODE_ECB, $iv);
    return rtrim($decrypt);
}
 }
Was it helpful?

Solution

You need to handle the padding in the decryption same as the encryption. Here is a working example of your code:

<?php
class Crypter{

    private $Key;
    private $Algo;

    public function __construct($Algo = MCRYPT_BLOWFISH) {

        $this->Key = substr('key', 0, mcrypt_get_key_size($Algo, MCRYPT_MODE_ECB));
        $this->Algo = $Algo;

    }

    public function Encrypt($data) {

        //$iv_size = mcrypt_get_iv_size($this->Algo, MCRYPT_MODE_ECB);
        //$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

        $blocksize = mcrypt_get_block_size('blowfish', 'ecb'); // get block size
        $pkcs = $blocksize - (strlen($data) % $blocksize); // get pkcs5 pad length
        $data.= str_repeat(chr($pkcs), $pkcs); // append pkcs5 padding to the data


        $crypt = mcrypt_encrypt($this->Algo, $this->Key, $data, MCRYPT_MODE_ECB);

        return rtrim(base64_encode($crypt));

    }

    public function Decrypt($data) {

        $crypt = base64_decode($data);
        $iv_size = mcrypt_get_iv_size($this->Algo, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $decrypt = mcrypt_decrypt($this->Algo, $this->Key, $crypt, MCRYPT_MODE_ECB, $iv);

        $block = mcrypt_get_block_size('blowfish', 'ecb');
        $pad = ord($decrypt[($len = strlen($decrypt)) - 1]);
        return substr($decrypt, 0, strlen($decrypt) - $pad);
    }
}

$crypter = new Crypter();
$data = "Some data to encrypt";
$encryptedData = $crypter->Encrypt($data);
$decryptedData = $crypter->Decrypt($encryptedData);
echo "Decrypted Data = [$decryptedData]\n";

Notice the three lines I replaced your original Decrypt() return line with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top