title: What are best practices for using AES encryption in Android?
body: <p><strong>Why I ask this question:</strong></p>
  
  <p>I know there have been a lot of questions about AES encryption, even for Android. And there are lots of code snippets if you search the Web. But on every single page, in every Stack Overflow question, I find another implementation with major differences.</p>
  
  <p>So I created this question to find a "best practice". I hope we can collect a list of the most important requirements and set up an implementation that is really secure!</p>
  
  <p>I read about initialization vectors and salts. Not all implementations I found had these features. So do you need it? Does it increase the security a lot? How do you implement it? Should the algorithm raise exceptions if the encrypted data cannot be decrypted? Or is that insecure and it should just return an unreadable string? Can the algorithm use Bcrypt instead of SHA?</p>
  
  <p>What about these two implementations I found? Are they okay? Perfect or some important things missing? What of these is secure?</p>
  
  <p>The algorithm should take a string and a "password" for encryption and then encrypt the string with that password. The output should be a string (hex or base64?) again. Decryption should be possible as well, of course.</p>
  
  <p><strong>What is the perfect AES implementation for Android?</strong></p>
  
  <p><strong>Implementation #1:</strong></p>
  
  <pre><code>import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  import java.security.NoSuchProviderException;
  import java.security.SecureRandom;
  
  import javax.crypto.Cipher;
  import javax.crypto.SecretKey;
  import javax.crypto.SecretKeyFactory;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.PBEKeySpec;
  import javax.crypto.spec.SecretKeySpec;
  
  public class AdvancedCrypto implements ICrypto {
  
          public static final String PROVIDER = "BC";
          public static final int SALT_LENGTH = 20;
          public static final int IV_LENGTH = 16;
          public static final int PBE_ITERATION_COUNT = 100;
  
          private static final String RANDOM_ALGORITHM = "SHA1PRNG";
          private static final String HASH_ALGORITHM = "SHA-512";
          private static final String PBE_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC";
          private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
          private static final String SECRET_KEY_ALGORITHM = "AES";
  
          public String encrypt(SecretKey secret, String cleartext) throws CryptoException {
                  try {
  
                          byte[] iv = generateIv();
                          String ivHex = HexEncoder.toHex(iv);
                          IvParameterSpec ivspec = new IvParameterSpec(iv);
  
                          Cipher encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM, PROVIDER);
                          encryptionCipher.init(Cipher.ENCRYPT_MODE, secret, ivspec);
                          byte[] encryptedText = encryptionCipher.doFinal(cleartext.getBytes("UTF-8"));
                          String encryptedHex = HexEncoder.toHex(encryptedText);
  
                          return ivHex + encryptedHex;
  
                  } catch (Exception e) {
                          throw new CryptoException("Unable to encrypt", e);
                  }
          }
  
          public String decrypt(SecretKey secret, String encrypted) throws CryptoException {
                  try {
                          Cipher decryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM, PROVIDER);
                          String ivHex = encrypted.substring(0, IV_LENGTH * 2);
                          String encryptedHex = encrypted.substring(IV_LENGTH * 2);
                          IvParameterSpec ivspec = new IvParameterSpec(HexEncoder.toByte(ivHex));
                          decryptionCipher.init(Cipher.DECRYPT_MODE, secret, ivspec);
                          byte[] decryptedText = decryptionCipher.doFinal(HexEncoder.toByte(encryptedHex));
                          String decrypted = new String(decryptedText, "UTF-8");
                          return decrypted;
                  } catch (Exception e) {
                          throw new CryptoException("Unable to decrypt", e);
                  }
          }
  
          public SecretKey getSecretKey(String password, String salt) throws CryptoException {
                  try {
                          PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), HexEncoder.toByte(salt), PBE_ITERATION_COUNT, 256);
                          SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM, PROVIDER);
                          SecretKey tmp = factory.generateSecret(pbeKeySpec);
                          SecretKey secret = new SecretKeySpec(tmp.getEncoded(), SECRET_KEY_ALGORITHM);
                          return secret;
                  } catch (Exception e) {
                          throw new CryptoException("Unable to get secret key", e);
                  }
          }
  
          public String getHash(String password, String salt) throws CryptoException {
                  try {
                          String input = password + salt;
                          MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM, PROVIDER);
                          byte[] out = md.digest(input.getBytes("UTF-8"));
                          return HexEncoder.toHex(out);
                  } catch (Exception e) {
                          throw new CryptoException("Unable to get hash", e);
                  }
          }
  
          public String generateSalt() throws CryptoException {
                  try {
                          SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
                          byte[] salt = new byte[SALT_LENGTH];
                          random.nextBytes(salt);
                          String saltHex = HexEncoder.toHex(salt);
                          return saltHex;
                  } catch (Exception e) {
                          throw new CryptoException("Unable to generate salt", e);
                  }
          }
  
          private byte[] generateIv() throws NoSuchAlgorithmException, NoSuchProviderException {
                  SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
                  byte[] iv = new byte[IV_LENGTH];
                  random.nextBytes(iv);
                  return iv;
          }
  
  }
  </code></pre>
  
  <p>Source: <a href="http://pocket-for-android.1047292.n5.nabble.com/Encryption-method-and-reading-the-Dropbox-backup-td4344194.html" rel="noreferrer">http://pocket-for-android.1047292.n5.nabble.com/Encryption-method-and-reading-the-Dropbox-backup-td4344194.html</a></p>
  
  <p><strong>Implementation #2:</strong></p>
  
  <pre><code>import java.security.SecureRandom;
  
  import javax.crypto.Cipher;
  import javax.crypto.KeyGenerator;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.SecretKeySpec;
  
  /**
   * Usage:
   * &lt;pre&gt;
   * String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)
   * ...
   * String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)
   * &lt;/pre&gt;
   * @author ferenc.hechler
   */
  public class SimpleCrypto {
  
      public static String encrypt(String seed, String cleartext) throws Exception {
          byte[] rawKey = getRawKey(seed.getBytes());
          byte[] result = encrypt(rawKey, cleartext.getBytes());
          return toHex(result);
      }
  
      public static String decrypt(String seed, String encrypted) throws Exception {
          byte[] rawKey = getRawKey(seed.getBytes());
          byte[] enc = toByte(encrypted);
          byte[] result = decrypt(rawKey, enc);
          return new String(result);
      }
  
      private static byte[] getRawKey(byte[] seed) throws Exception {
          KeyGenerator kgen = KeyGenerator.getInstance("AES");
          SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
          sr.setSeed(seed);
          kgen.init(128, sr); // 192 and 256 bits may not be available
          SecretKey skey = kgen.generateKey();
          byte[] raw = skey.getEncoded();
          return raw;
      }
  
  
      private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
          byte[] encrypted = cipher.doFinal(clear);
          return encrypted;
      }
  
      private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.DECRYPT_MODE, skeySpec);
          byte[] decrypted = cipher.doFinal(encrypted);
          return decrypted;
      }
  
      public static String toHex(String txt) {
          return toHex(txt.getBytes());
      }
      public static String fromHex(String hex) {
          return new String(toByte(hex));
      }
  
      public static byte[] toByte(String hexString) {
          int len = hexString.length()/2;
          byte[] result = new byte[len];
          for (int i = 0; i &lt; len; i++)
              result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
          return result;
      }
  
      public static String toHex(byte[] buf) {
          if (buf == null)
              return "";
          StringBuffer result = new StringBuffer(2*buf.length);
          for (int i = 0; i &lt; buf.length; i++) {
              appendHex(result, buf[i]);
          }
          return result.toString();
      }
      private final static String HEX = "0123456789ABCDEF";
      private static void appendHex(StringBuffer sb, byte b) {
          sb.append(HEX.charAt((b&gt;&gt;4)&amp;0x0f)).append(HEX.charAt(b&amp;0x0f));
      }
  
  }
  </code></pre>
  
  <p>Source: <a href="http://www.tutorials-android.com/learn/How_to_encrypt_and_decrypt_strings.rhtml" rel="noreferrer">http://www.tutorials-android.com/learn/How_to_encrypt_and_decrypt_strings.rhtml</a></p>