I changed PHP to 7 and must change mcrypt_decrypt to openssl_decrypt with MCRYPT_RIJNDAEL_128

80
April 10, 2018, at 07:39 AM

I'm using AES class I found on Internet years ago, but now I have updated/upgrade my server. On localhost (test) I have PHP 7

Encrypt: submit POST file (max 3MB) into PHP loop in buffer parts and create new encrypted one (*.enc) and save in directory. Key and I've-key are in DB.

Decrypt: using key and I've-key I decrypt *.enc file and open it in browser by response.

class AES {
const CIPHER = MCRYPT_RIJNDAEL_128; // Rijndael-128 is AES
const MODE = MCRYPT_MODE_CBC;
public $key; // needs to be 32 bytes for aes
public $iv; // needs to be 16 bytes for aes
public function __construct($key = '', $iv = '') {
    $this->key = $key;
    $this->iv = $iv;
}
function rand_key($length = 32) {
    $key = openssl_random_pseudo_bytes($length);
    return $key;
}
function rand_iv() {
    $ivSize = mcrypt_get_iv_size(self::CIPHER, self::MODE);
    $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
    return $iv;
}
public function encrypt($plaintext) {
    $ciphertext = mcrypt_encrypt(self::CIPHER, $this->key, $plaintext, self::MODE, $this->iv);
    return base64_encode($ciphertext);
}
public function decrypt($ciphertext) {
    $ciphertext = base64_decode($ciphertext);
    $plaintext = mcrypt_decrypt(self::CIPHER, $this->key, $ciphertext, self::MODE, $this->iv);
    return rtrim($plaintext, "\0");
}
public function encrypt_file($input_file, $output_file) {
    if($input_file != '' && $output_file != '') {
        $input_file_handle = @fopen($input_file, "r");
        $output_file_handle = @fopen($output_file, 'wb');
        if (!$input_file_handle) {
            throw new Exception("Could not open input file");
        }
        if (!$output_file_handle) {
            throw new Exception("Could not open output file");
        }
        while (!feof($input_file_handle)) {
            $buffer = base64_encode(fread($input_file_handle, 4096));
            $encrypted_string = $this->encrypt($buffer);
            fwrite($output_file_handle, $encrypted_string);
        }
        fclose($input_file_handle);
        fclose($output_file_handle);
    }else{
        throw new Exception("Input or output string is empty");
    }
    return true;
}
public function decrypt_file($input_file) {
    $input_file_handle = @fopen($input_file, "r");
    if (!$input_file_handle) {
        throw new Exception("Could not open input file");
    }
    $decrypted_string = '';
    while (!feof($input_file_handle)) {
        $buffer = fread($input_file_handle, 7296);
        $decrypted_string .= base64_decode($this->decrypt($buffer));
    }
    fclose($input_file_handle);
    return $decrypted_string;
}
}

I am using this like this (decode and open file in browser below)

$iv = base64_decode($iv); //base_64
$aes = new AES($key, $iv);
$response = new Response();
$response->headers->set('Content-type', 'application/octet-stream');
$response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $originalName));
$response->setContent($aes->decrypt_file($filePath, $directory . $originalName));
$response->setStatusCode(200);
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Expires', '0');
return $response;

I can't re-decode-encode all files in loop, just change encode/decode functions. I tried all I found in Internet, but my decoded files are 0 bytes.

READ ALSO
PHP HTML Images in a directory table

PHP HTML Images in a directory table

Any idea how i can generate a table with the images I have in a directory, example, Assignment directory? I have tried a number of options but I cant get this sorted

42
Nest while Statment Php Dependent

Nest while Statment Php Dependent

The code below is my third attempt at sorting these queries i know i am missing something easy, but i have cannot find what i am doingthe current code outputs the correct information for the results of the top query but each line of the bottom query is the same ( i know its because...

31
Page Hit Counter With IP Info

Page Hit Counter With IP Info

I am trying to make my own page hit counter but have run into some errors

38