title: Decrypt Feistel Cipher in PostgreSQL
body: <p>I updated a bunch of fields in my db using this <a href="http://wiki.postgresql.org/wiki/Pseudo_encrypt" rel="nofollow">Feistel Cipher</a>. According to the documentation, the cipher can be undone to get the original value.  How can I undo the values if need be?</p>
  
  <p>Here is the original cipher function:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION pseudo_encrypt(VALUE int) returns bigint AS $$
  DECLARE
  l1 int;
  l2 int;
  r1 int;
  r2 int;
  i int:=0;
  BEGIN
   l1:= (VALUE &gt;&gt; 16) &amp; 65535;
   r1:= VALUE &amp; 65535;
   WHILE i &lt; 3 LOOP
     l2 := r1;
     r2 := l1 # ((((1366.0 * r1 + 150889) % 714025) / 714025.0) * 32767)::int;
     l1 := l2;
     r1 := r2;
     i := i + 1;
   END LOOP;
   RETURN ((l1::bigint &lt;&lt; 16) + r1);
  END;
  $$ LANGUAGE plpgsql strict immutable;
  </code></pre>