title: obfuscate or encrypt some plain text data in PHP
body: <p>I need to obfuscate or encrypt some plain text data in my php 5.2 application.</p>
  
  <p>I'd prefer a solution that would have input string and output string retain the same length.</p>
  
  <p>This does not need to extremely strong, as there are numerous other layers of security in place.  Strong would be good, but this will just keep programmers/dba/support people/etc from accidentally reading the text from within the database.</p>
  
  <p>key considerations</p>
  
  <ul>
  <li><strong>EDIT ADD</strong> I'd prefer a solution that would have input string and output string retain the same length.</li>
  <li>only string text will be obfuscated/encrypted for storage in a database</li>
  <li>the php application will need to obfuscate/encrypt the data before the database save and will need to un-obfuscate/dencrypt following the database read</li>
  <li>this is a modification to an existing application</li>
  <li>only some columns will need to be obfuscated/encrypted</li>
  <li>only some rows will need to be obfuscated/encrypted, based on a Type field</li>
  <li>there are only a few load/save points to handle</li>
  <li>max column size is already determined for some fields, but not for others, but I'd prefer a solution to work within the existing size of the restricted fields</li>
  <li><strong>EDIT, ADD</strong> the key will be probably be a composite of some Primary key info +uneditable fields</li>
  </ul>
  
  <p>here is a sample database table and data:</p>
  
  <pre><code>int           char(1) varchar(24)              int      date
  MyPrimaryKey  RowType UserText                 UserNo   DateChange
  ------------  ------- ------------------------ -------- ----------------
  1             N       nothing special here     43       6/20/2009 12:11am
  2             N       same thing, wow!         78       6/23/2009 1:03pm
  3             S       fBJKg}.jkjWfF78dlg@45kjg 43       6/25/2009 6:45am
  4             N       same old, same old text  21       6/25/2009 8:11am
  </code></pre>
  
  <p>The application would load and display rows 1,2, and 4 normally.  However it would conditionally (based on row type) handle the text in row 3 using this obfuscate/encrypt and un-obfuscate/decrypt logic.</p>
  
  <p>Can anyone provide obfuscate/encrypt and un-obfuscate/decrypt functions code, links, and or pointer that would help here?</p>
  
  <p>thanks!</p>
  
  <p><strong>EDIT</strong><br>
  I like the simple base64 encoding idea, but is there a method that can keep the data within a fixed size. All methods listed so far have the output value larger than the input value.  This will be a problem for some columns, where the user can enter in 50 characters and it is stored in a varchar(50) column.</p>