title: OpenSSL text max size?
body: <p>Using OpenSSL I have created a basic encrypt/decrypt class. My goal is to encrypt text (JSON) to be sent over http to/from a remote web_service. 
  <em>(yeah, I know I could use https instead, but there're some technical constraints not to use it in this case)</em></p>
  
  <pre><code>class Crypt {
     static private $default_method = "aes-256-cbc";
  
     public static function doEncrypt($text_to_encrypt) {
        // ...some preparation stuff here
        $result = base64_encode(openssl_encrypt($text_to_encrypt, self::$default_method, $seckey, OPENSSL_RAW_DATA, $iv));
        return $result;
      }
  
     public static function doDecrypt($encrypted_text) {
        // ...some preparation stuff here
        $result = openssl_decrypt(base64_decode($encrypted_text), self::$default_method, $seckey, OPENSSL_RAW_DATA, $iv);
  
        return $result;
     }
  }
  </code></pre>
  
  <p>I have conducted many tests using texts of different sizes and it works fine. But ...where is the limit?</p>
  
  <p><strong>My doubt:</strong></p>
  
  <p>Where is the limit of <code>text_to_encrypt</code> max size? In my tests I got it work properly with texts up to 100Mb. With bigger texts I got http time-out error ...but that's due to my current test-server limit. I know I can modify it, or I can look for more powerful servers which will process faster.</p>
  
  <p>In case I had unlimited process capacity, where would be the bottle-neck? What would set the limit of the <code>text_to_encrypt</code> max size? Will encrypt/decrypt process fail at some point? Will be PHP the limit?</p>
  
  <p>This is how I test:</p>
  
  <pre><code>define("_MIN_SIZE", 10000); // size of text, in bytes
  define("_MAX_SIZE", 250000);    // size of text, in bytes
  define("_SIZE_INCREASE", 1033); // size incresase for tests, in bytes
  
  echo '&lt;h2&gt;Test encrypt/decrypt&lt;/h2&gt;';
  for($i = _MIN_SIZE; $i &lt; _MAX_SIZE; $i = $i + _SIZE_INCREASE){
      $string = generateRandomString($i);
      $encstr = crypt::doEncrypt($string);
      if($string != ($descstr = crypt::doDecrypt($encstr) ) ){
          echo "&lt;p style='color: red'&gt;ERROR!".strlen($string);
  
      } else {
          echo "&lt;p style='color: green'&gt;OK ".strlen($string);
  
      }
      echo '&lt;/p&gt;';
  }
  echo '&lt;h2&gt;Done!&lt;/h2&gt;';
  
  function generateRandomString($length = 25) {
      $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.,_:;&lt;&gt;çÇ´`ñÑáÁàÀéÉèíÍìÌïÏóÓòÒúÚùÙüÜ¿?=)(/&amp;%$·"!ªº@|¡+[]{}€';
      $charactersLength = strlen($characters);
      $randomString = '';
      for ($i = 0; $i &lt; $length; $i++) {
          $randomString .= $characters[rand(0, $charactersLength - 1)];
      }
      return $randomString;
  }
  </code></pre>
  
  <blockquote>
    <p>Note: I know there are some missing parts regarding the full data
    workflow. I.e. Authentication, as pointed in a comment, seckey
    and iv generation and management, etc.). But for a sake of simplicity I
    haven't shown the whole process in the question. My concern is about
    where is the size limit for encrypting strings in PHP using OpenSSL.</p>
  </blockquote>