title: Is AES/ CBC/ PKCS5Padding encryption decryption algorithm and SHA-1 hashing algorithm avaible in all type of Android devices
body: <p>I had tried to run the following <em>AES/ CBC/ PKCS5Padding</em> encryption and decryption code, with <em>SHA-1</em> as key generation, in Nexus 5. It works very well so far.</p>
  
  <p>However, my only concern is, Is AES/ CBC/ PKCS5Padding encryption decryption algorithm and SHA-1 hashing algorithm available in all type of Android devices?</p>
  
  <p>Is there any chance that the following code will fail to run on certain Android devices? If so, is there any fall back plan?</p>
  
  <h2>AES/ CBC/ PKCS5Padding</h2>
  
  <pre><code>// http://stackoverflow.com/questions/3451670/java-aes-and-using-my-own-key
  public static byte[] generateKey(String key) throws GeneralSecurityException, UnsupportedEncodingException {
      byte[] binary = key.getBytes("UTF-8");
      MessageDigest sha = MessageDigest.getInstance("SHA-1");
      binary = sha.digest(binary);
      // Use only first 128 bit.
      binary = Arrays.copyOf(binary, 16);
      return binary;
  }
  
  // http://stackoverflow.com/questions/17322002/what-causes-the-error-java-security-invalidkeyexception-parameters-missing
  public static String encrypt(byte[] key, String value) throws GeneralSecurityException {
      // Argument validation.
      if (key.length != 16) {
          throw new IllegalArgumentException("Invalid key size.");
      }
  
      // Setup AES tool.
      SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
  
      // Do the job with AES tool.
      byte[] original = value.getBytes(Charset.forName("UTF-8"));
      byte[] binary = cipher.doFinal(original);
      return Base64.encodeToString(binary, Base64.DEFAULT);
  }
  
  // // http://stackoverflow.com/questions/17322002/what-causes-the-error-java-security-invalidkeyexception-parameters-missing
  public static String decrypt(byte[] key, String encrypted) throws GeneralSecurityException {
      // Argument validation.
      if (key.length != 16) {
          throw new IllegalArgumentException("Invalid key size.");
      }
  
      // Setup AES tool.
      SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
  
      // Do the job with AES tool.
      byte[] binary = Base64.decode(encrypted, Base64.DEFAULT);
      byte[] original = cipher.doFinal(binary);
      return new String(original, Charset.forName("UTF-8"));
  }
  </code></pre>
  
  <h2>Usage</h2>
  
  <pre><code>byte[] key = generateKey("my secret key");
  String ciphertext = encrypt(key, "my plain content");
  String plainContent = decrypt(key, ciphertext);
  </code></pre>