title: AES-256-CBC Encryption in Android
body: <p>Am using library <a href="https://github.com/fukata/AES-256-CBC-Example" rel="nofollow noreferrer">https://github.com/fukata/AES-256-CBC-Example</a> for Encryption and decryption And with the help of <a href="https://stackoverflow.com/questions/44497058/issue-with-key-and-iv-on-aes-256-cbc">Issue with key and iv on AES 256-CBC</a>. Decryption is working properly. 
  But in the Case of Encryption this error happens                                                                       </p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vinu.aessamble/com.example.vinu.aessamble.MainActivity}: java.lang.RuntimeException: javax.crypto.IllegalBlockSizeException:error:1e06c06a:Cipher functions:EVP_EncryptFinal_ex:DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
  </code></pre>
  
  <p>And i am using </p>
  
  <pre><code>String encrypted = AESUtil.encrypt("My PlainText to encrypt");
  </code></pre>
  
  <p>In AESUtils </p>
  
  <pre><code>public static String encrypt(String src) {
      try {
          Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
          cipher.init(Cipher.ENCRYPT_MODE, makeKey(), makeIv());
          return Base64.encodeBytes(cipher.doFinal(src.getBytes()));
      } catch (Exception e) {
          throw new RuntimeException(e);
      }
  }
  </code></pre>
  
  <p>Using this for PKCS5Padding encryption is working but can't decrypted using the method. When using NoPadding for encryption it shows above error. </p>
  
  <pre><code>public static String decrypt(String src) {
          String decrypted = "";
          try {
              Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
              byte[] array= Base64.decode(src);
              byte[] ivData=Arrays.copyOfRange(array,0,16);
              byte[] encrypted = Arrays.copyOfRange(array, 16, array.length);
              // Init the cipher with decrypt mode, key, and IV bytes array (no more hardcoded)
              cipher.init(Cipher.DECRYPT_MODE, makeKey(), new IvParameterSpec(ivData));
              // Decrypt same old way
              decrypted = new String(cipher.doFinal(encrypted));
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
          return decrypted;
      }
  </code></pre>
  
  <p>makeIv()</p>
  
  <pre><code>static AlgorithmParameterSpec makeIv() {
          try {
              return new IvParameterSpec(sha256digest16(ENCRYPTION_IV));
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (NoSuchAlgorithmException e) {
              e.printStackTrace();
          }
          return null;
      }
  </code></pre>
  
  <p>makeKey()</p>
  
  <pre><code> static Key makeKey() {
          try {
              byte[] key = ENCRYPTION_KEY.getBytes("UTF-8");
              return new SecretKeySpec(key, "AES");
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          }
  
          return null;
      }
  </code></pre>
  
  <p>Iv and key are</p>
  
  <pre><code>private static final String ENCRYPTION_KEY = qwertyuiopasdfghjklzxcvbnmqwerty";
      private static final String ENCRYPTION_IV = "qbmocwtttkttpqvv";
  </code></pre>
  
  <p>Help me to do encryption.</p>