Tamara
Tamara

Reputation: 2980

PHP MCrypt: Encrypt/Decrypt data by chunks. Why result depends on size of the chunk?

I want to encrypt/decrypt files by chunks, because file size can be quite large (50-100Mb). I found code of encryption class on stack overflow and changed it just a bit:

class filecrypt{

var $_CHUNK_SIZE;
var $_CHIPHER;
var $_MODE;

function __construct($chipher, $mode){
    $this->_CHUNK_SIZE = 100*1024; // 100Kb
    $this->_CHIPHER = $chipher;
    $this->_MODE = $mode;
}

public function setChunkSize($value)
{
    $this->_CHUNK_SIZE = $value;
}

public function encrypt($string, $key, $vector){
    $key = pack('H*', $key);
    if (extension_loaded('mcrypt') === true) return mcrypt_encrypt($this->_CHIPHER, substr($key, 0, mcrypt_get_key_size($this->_CHIPHER, $this->_MODE)), $string, $this->_MODE, $vector);
    return false;
}

public function decrypt($string, $key, $vector){
    $key = pack('H*', $key);
    if (extension_loaded('mcrypt') === true) return mcrypt_decrypt($this->_CHIPHER, substr($key, 0, mcrypt_get_key_size($this->_CHIPHER, $this->_MODE)), $string, $this->_MODE, $vector);
    return false;
}

public function encryptFileChunks($source, $destination, $key, $vector){
    return $this->cryptFileChunks($source, $destination, $key, 'encrypt', $vector);
}

public function decryptFileChunks($source, $destination, $key, $vector){
    return $this->cryptFileChunks($source, $destination, $key, 'decrypt', $vector);
}

private function cryptFileChunks($source, $destination, $key, $op, $vector){

    if($op != "encrypt" and $op != "decrypt") return false;

    $buffer = '';
    $inHandle = fopen($source, 'rb');
    $outHandle = fopen($destination, 'wb+');

    if ($inHandle === false) return false;
    if ($outHandle === false) return false;

    while(!feof($inHandle)){
        $buffer = fread($inHandle, $this->_CHUNK_SIZE);
        if($op == "encrypt") $buffer = $this->encrypt($buffer, $key, $vector);
        elseif($op == "decrypt") $buffer = $this->decrypt($buffer, $key, $vector);
        fwrite($outHandle, $buffer);
    }
    fclose($inHandle);
    fclose($outHandle);
    return true;
}

public function printFileChunks($source, $key, $vector){

    $buffer = '';
    $inHandle = fopen($source, 'rb');

    if ($inHandle === false) return false;

    while(!feof($inHandle)){
        $buffer = fread($inHandle, $this->_CHUNK_SIZE);
        $buffer = $this->decrypt($buffer, $key, $vector);
        echo $buffer;
    }
    return fclose($inHandle);
}
}

So, I tested this class in my script:

$chipher = MCRYPT_RIJNDAEL_128;
$mode = MCRYPT_MODE_CFB;

$filecrypt = new filecrypt($chipher, $mode);
$key = '3da541559918a808c2402bba5012f6c60b27661c'; // Your encryption key

$vectorSize = mcrypt_get_iv_size($chipher, $mode);
$vector  =   mcrypt_create_iv($vectorSize, MCRYPT_DEV_URANDOM);

//Encrypt file
$filecrypt->setChunkSize(8*1024);
$filecrypt->encryptFileChunks(APPLICATION_PATH.'/../data/resources/img1.jpg', APPLICATION_PATH.'/../data/resources/img1_en.jpg', $key, $vector);

//Decrypt file
$filecrypt->setChunkSize(8*1024);
$filecrypt->decryptFileChunks(APPLICATION_PATH.'/../data/resources/img1_en.jpg', APPLICATION_PATH.'/../data/resources/img1_res.jpg', $key, $vector);

Everything works fine and restores image is absolutely the same as source image.

BUT if I set different chunk size for encryption and decryption processes, restored image will be corrupted. This is source image:

Source image

This is restored image after encryption/decryption with different chunk size: enter image description here

Here is a code:

$chipher = MCRYPT_RIJNDAEL_128;
$mode = MCRYPT_MODE_CFB;

$filecrypt = new filecrypt($chipher, $mode);
$key = '3da541559918a808c2402bba5012f6c60b27661c'; // Your encryption key

$vectorSize = mcrypt_get_iv_size($chipher, $mode);
$vector  =   mcrypt_create_iv($vectorSize, MCRYPT_DEV_URANDOM);

//Encrypt file
$filecrypt->setChunkSize(8*1024);
$filecrypt->encryptFileChunks(APPLICATION_PATH.'/../data/resources/img1.jpg', APPLICATION_PATH.'/../data/resources/img1_en.jpg', $key, $vector);

//Decrypt file
$filecrypt->setChunkSize(4*1024);
$filecrypt->decryptFileChunks(APPLICATION_PATH.'/../data/resources/img1_en.jpg', APPLICATION_PATH.'/../data/resources/img1_res.jpg', $key, $vector);

My question is how does chunk size influence on encryption/decryption process? Does it connected with block cipher mode and paddings? How can I encrypt data by chunks?

Maybe I should use another cipher for this purpose?

Upvotes: 1

Views: 520

Answers (1)

Narf
Narf

Reputation: 14762

Does it connected with block cipher mode and paddings?

Spot on.

Maybe I should use another cipher for this purpose?

You could do that and use a stream-like cipher mode such as CTR, which doesn't require padding.

OR you could take care of the chunk sizes that you pick. It should be divisible by whatever mcrypt_get_block_size() returns for your cipher.

Basically, block cipher modes will split your data into "chunks" too, only those would be called blocks - hence, block size. Whenever the condition $dataSize % $blockSize !== 0 is true, the last block will be padded (by MCrypt) with NUL-bytes.

Then, during the decryption phase, MCrypt doesn't trim these NUL-bytes, and your image gets corrupted.

Upvotes: 1

Related Questions