sunco

AES 256 file

Jan 17th, 2021 (edited)
619
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // encrypt large files with aes-256-cbc
  4. // this is a mod from https://thegermancoder.com/2018/10/01/decrypting-files-with-aes-256-in-php/
  5.  
  6. static $file_encryption_blocks = 10000;
  7.  
  8. public static function encrypt_file($path, $secret, $dest) {
  9.        
  10.     $salt = openssl_random_pseudo_bytes(self::$block_size);
  11.    
  12.     $key = hash_pbkdf2('sha1', $secret, $salt, self::$iterations, self::$key_size, true);
  13.    
  14.     $iv = openssl_random_pseudo_bytes(self::$block_size);
  15.  
  16.     $error = false;
  17.     if ($fpOut = fopen($dest, 'w')) {
  18.  
  19.         // Put the initialzation vector to the beginning of the file
  20.         fwrite($fpOut, $iv);
  21.         fwrite($fpOut, $salt);
  22.  
  23.         if ($fpIn = fopen($path, 'rb')) {
  24.             while (!feof($fpIn)) {
  25.                 $plaintext = fread($fpIn, self::$block_size * self::$file_encryption_blocks);
  26.                
  27.                 $ciphertext = openssl_encrypt($plaintext, self::$encryption_method, $key, OPENSSL_RAW_DATA, $iv);
  28.  
  29.                 // Use the first 16 bytes of the ciphertext as the next initialization vector
  30.                 $iv = substr($ciphertext, 0, self::$block_size);
  31.  
  32.                 fwrite($fpOut, $ciphertext);
  33.             }
  34.             fclose($fpIn);
  35.         } else {
  36.             $error = true;
  37.         }
  38.         fclose($fpOut);
  39.     } else {
  40.         $error = true;
  41.     }
  42.  
  43.     return $error ? false : $dest;
  44.  
  45. }
  46.  
  47. public static function decrypt_file($path, $secret, $dest) {
  48.    
  49.     // leer el salt, son 16 bytes, iniciando desde el byte 16 (los primeros 16 son del iv)
  50.     $salt = file_get_contents($path, FALSE, NULL, self::$block_size, self::$block_size);
  51.    
  52.     $key = hash_pbkdf2('sha1', $secret, $salt, self::$iterations, self::$key_size, true);
  53.    
  54.     $error = false;
  55.    
  56.     if ($fpOut = fopen($dest, 'w')) {
  57.         if ($fpIn = fopen($path, 'rb')) {
  58.  
  59.             // Get the initialzation vector from the beginning of the file
  60.             $iv = fread($fpIn, self::$block_size);
  61.            
  62.             // leer siguientes 16 bytes para hacer bypass al salt (que ya fue leído arriba)
  63.             $tmp = fread($fpIn, self::$block_size);
  64.  
  65.             while (!feof($fpIn)) {
  66.                 $ciphertext = fread($fpIn, self::$block_size * (self::$file_encryption_blocks + 1)); // we have to read one block more for decrypting than for encrypting
  67.                
  68.                 $plaintext = openssl_decrypt($ciphertext, self::$encryption_method, $key, OPENSSL_RAW_DATA, $iv);
  69.  
  70.                 // Use the first 16 bytes of the ciphertext as the next initialization vector
  71.                 $iv = substr($ciphertext, 0, self::$block_size);
  72.  
  73.                 fwrite($fpOut, $plaintext);
  74.             }
  75.             fclose($fpIn);
  76.         } else {
  77.             $error = true;
  78.         }
  79.         fclose($fpOut);
  80.     } else {
  81.         $error = true;
  82.     }
  83.  
  84.     return $error ? false : $dest;
  85.  
  86. }
  87.  
  88. ?>
RAW Paste Data