title: How to decode data encrypted using AES-256 in CBC mode and PKCS7 padding?
body: <p>We are using below java code to decrypt the data which is encrypted using AES-256 in CBC mode and PKCS7 padding.</p>
  <p><strong>Java Code:</strong></p>
  <pre><code>import javax.crypto.spec.SecretKeySpec;
  import javax.crypto.Cipher;
  import javax.crypto.spec.IvParameterSpec;
  import java.security.*;
  import org.apache.commons.codec.binary.Base64;
  import org.apache.commons.lang.StringUtils;
  import org.apache.log4j.Logger;
  
  
  public class AES256 {
  
      private static byte[] initVector = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  
      public String decrypt (String encryptedDataBase64, String keyBase64)
      {
         try {
             Security.setProperty(&quot;crypto.policy&quot;, &quot;unlimited&quot;);
             IvParameterSpec ivSpec = new IvParameterSpec(initVector);  // Get the init vector
  
             // Get the Base64-encoded key
             byte[] key = Base64.decodeBase64(keyBase64.getBytes(&quot;UTF-8&quot;));
             Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5PADDING&quot;);  // AES / CBC / PKCS5 padding
             SecretKeySpec skeySpec = new SecretKeySpec(key, &quot;AES&quot;);
             cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
             byte[] encryptedData = Base64.decodeBase64(encryptedDataBase64.getBytes(&quot;UTF-8&quot;));
             byte[] decryptedData = cipher.doFinal(encryptedData);
  
             return new String(decryptedData);
         }
         catch (Exception e) {
             logger.error(&quot;AES256 Decrypt: Decryption exception: &quot;+ e.getMessage());
             return null;
         }
      }
  }
  </code></pre>
  <p>Now we need to convert this decryption logic to Javascript as our app is sending the encrypted data in the headers while requesting for index.html from server. I tried to decrypt using Crypto.js but I am getting decoded string as empty. Below is the Javascript code I was trying to use.</p>
  <pre><code>var key = CryptoJS.enc.Base64.parse(keyBase64);
  var data = CryptoJS.enc.Base64.parse(encryptedDataBase64);
  var dec_data = CryptoJS.AES.decrypt(data, key);
  </code></pre>
  <p>dec_data is coming as empty.</p>
  <p><a href="https://i.stack.imgur.com/o69Vl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o69Vl.png" alt="enter image description here" /></a></p>
  <p>I tried this after reading some of stack over flow questions. Can any one please let me know if there is any error in the code.</p>