title: What Java encryption algorithms should I use?
body: <p>I'm working on a program that needs to store binary information encrypted at rest. Unfortunately, I can't seem to find a resource that explains which encryption schemes are best for different applications.</p>
  
  <p>Since encryption is complicated and I'm not an expert, I've decided to use a library called <a href="http://www.jasypt.org/" rel="noreferrer">Jasypt</a> that wraps Java's built-in encryption functions. To figure out what algorithms are available to me, I wrote a few unit tests.</p>
  
  <p>The first test calls Jasypt's <code>AlgorithmRegistry.getAllPBEAlgorithms()</code> function and lists out all available encryption algorithms:</p>
  
  <pre><code>PBEWITHHMACSHA1ANDAES_128
  PBEWITHHMACSHA1ANDAES_256
  PBEWITHHMACSHA224ANDAES_128
  PBEWITHHMACSHA224ANDAES_256
  PBEWITHHMACSHA256ANDAES_128
  PBEWITHHMACSHA256ANDAES_256
  PBEWITHHMACSHA384ANDAES_128
  PBEWITHHMACSHA384ANDAES_256
  PBEWITHHMACSHA512ANDAES_128
  PBEWITHHMACSHA512ANDAES_256
  PBEWITHMD5ANDDES
  PBEWITHMD5ANDTRIPLEDES
  PBEWITHSHA1ANDDESEDE
  PBEWITHSHA1ANDRC2_128
  PBEWITHSHA1ANDRC2_40
  PBEWITHSHA1ANDRC4_128
  PBEWITHSHA1ANDRC4_40
  </code></pre>
  
  <p>At runtime, Jasypt will throw an <code>EncryptionOperationNotPossibleException</code> if you try to use an algorithm that for some reason isn't supported or violates Java's encryption rules. Interestingly, if I attempt to use each of the 'available' algorithms to encrypt and then decrypt some arbitrary data, and only print out the ones that don't throw that exception, I get this slimmed down list:</p>
  
  <pre><code>PBEWITHMD5ANDDES
  PBEWITHSHA1ANDDESEDE
  PBEWITHSHA1ANDRC2_128
  PBEWITHSHA1ANDRC2_40
  PBEWITHSHA1ANDRC4_128
  PBEWITHSHA1ANDRC4_40
  </code></pre>
  
  <p>The list of available algorithms can be expanded by pulling in the <a href="http://www.bouncycastle.org/" rel="noreferrer">BouncyCastle</a> JCE and registering it by executing <code>Security.addProvider(new BouncyCastleProvider())</code>. If I repeat the previous test after doing this, I get a much bigger list of algorithms to choose from:</p>
  
  <pre><code>PBEWITHMD2ANDDES
  PBEWITHMD5AND128BITAES-CBC-OPENSSL
  PBEWITHMD5AND192BITAES-CBC-OPENSSL
  PBEWITHMD5AND256BITAES-CBC-OPENSSL
  PBEWITHMD5ANDDES
  PBEWITHMD5ANDRC2
  PBEWITHSHA1ANDDES
  PBEWITHSHA1ANDDESEDE
  PBEWITHSHA1ANDRC2
  PBEWITHSHA1ANDRC2_128
  PBEWITHSHA1ANDRC2_40
  PBEWITHSHA1ANDRC4_128
  PBEWITHSHA1ANDRC4_40
  PBEWITHSHA256AND128BITAES-CBC-BC
  PBEWITHSHA256AND192BITAES-CBC-BC
  PBEWITHSHA256AND256BITAES-CBC-BC
  PBEWITHSHAAND128BITAES-CBC-BC
  PBEWITHSHAAND128BITRC2-CBC
  PBEWITHSHAAND128BITRC4
  PBEWITHSHAAND192BITAES-CBC-BC
  PBEWITHSHAAND2-KEYTRIPLEDES-CBC
  PBEWITHSHAAND256BITAES-CBC-BC
  PBEWITHSHAAND3-KEYTRIPLEDES-CBC
  PBEWITHSHAAND40BITRC2-CBC
  PBEWITHSHAAND40BITRC4
  PBEWITHSHAANDIDEA-CBC
  PBEWITHSHAANDTWOFISH-CBC
  </code></pre>
  
  <p>Unfortunately, now I have no idea which of these many algorithms is most appropriate for my application. I have an inkling that AES is the right way to go, and it looks like <code>PBEWITHSHA256AND256BITAES-CBC-BC</code> is the AES implementation with the longest key length, but I don't know where to go to confirm that suspicion.</p>
  
  <p>Which of these schemes would provide the highest security levels and which have obvious security issues?</p>
  
  <p><strong>EDIT:</strong> I want to be able to distribute my code without requiring the end user to install the unlimited cryptography files, as that will almost certainly be beyond the capabilities of not so tech savvy users. What I really want is the strongest encryption that I can get without using the unlimited strength jurisdiction files.</p>