title: Decrypting text file in openssl:-Error:EVP_DecryptFinal_ex:wrong final block length:evp_enc.c:518:
body: <p>I'm writing a small application to learn more of encryption/decryption.
  From my code I am generating AES key and then encrypting text file with AES key.After that I am encrypting AES key using RSA public key.</p>
  
  <p>Below is the code snippet</p>
  
  <pre><code>SecretKey secretAesKey ;
  KeyGenerator keyGen = KeyGenerator.getInstance("AES");
  keyGen.init(256);
  secretAesKey = keyGen.generateKey();
  if (secretAesKey != null) {
      Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      aesCipher.init(Cipher.ENCRYPT_MODE, secretAesKey);
      long aesEncryptStartTime = SystemClock.elapsedRealtime();
      CipherInputStream aesCis = new CipherInputStream(fis, aesCipher);
      int read;
      byte[] buffer = new byte[4096];
      while ((read = aesCis.read(buffer)) != -1) {
          aesFos.write(buffer, 0, read);
          aesFos.flush();
      }
  
      // Encrypt the generated key
      if (!encKeyFile.exists()) {
          encKeyFile.createNewFile();
      }
  
      try {
          byte[] encryptedAesKey = null;
          Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
          rsaCipher.init(Cipher.ENCRYPT_MODE, readRsaPublicKeyFromResource(context));
          encryptedAesKey = rsaCipher.doFinal(secretAesKey.getEncoded());
          rsaFos.write(encryptedAesKey);
          rsaFos.flush();
      } catch (Exception e) {
          Log.e(LOG_TAG, "RSA encryption error", e);
      } finally {
          rsaFos.close();
      }
  </code></pre>
  
  <p>During decryption first decrypting AES key with RSA private key, from following code  </p>
  
  <pre><code>FileInputStream keyFis = new FileInputStream(encKeyFile);
  byte[] encKey = new byte[keyFis.available()];
  keyFis.read(encKey);
  keyFis.close();
  
  SecretKey key = null;
  PrivateKey privKey = readRsaPrivateKeyFromResource(context);
  Cipher cipher = null;
  
  try
  {
      // initialize the cipher...
      cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
      cipher.init(Cipher.DECRYPT_MODE, privKey);
      // generate the aes key!
      key = new SecretKeySpec (cipher.doFinal(encKey), "AES" );
      String stringKey = Base64.encodeToString(key.getEncoded(), Base64.DEFAULT);
      try {
          OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("aesDecrypted.key", Context.MODE_PRIVATE));
          outputStreamWriter.write(stringKey);
          outputStreamWriter.close();
      }
      catch (IOException e) {
          Log.e("Exception", "File write failed: " + e.toString());
      }
  </code></pre>
  
  <p>I am getting decrypted AES key for example "ah3ZWMieji6KtSav6gaayTvsEID2vpp589wdChTLmZs="</p>
  
  <p>After converting it into hex value it is as follows. "6A1DD958C89E8E2E8AB526AFEA069AC93BEC1080F6BE9A79F3DC1D0A14CB999B" </p>
  
  <p>Trying to decrypt text file with generated hex key in terminal 
  For e.g:</p>
  
  <blockquote>
    <p>openssl aes-256-cbc -d -a -iv 0 -in encrypt.txt -out decrypt.txt -K
    6A1DD958C89E8E2E8AB526AFEA069AC93BEC1080F6BE9A79F3DC1D0A14CB999B</p>
  </blockquote>
  
  <p>I am getting following error </p>
  
  <p>bad decrypt
  7560:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:evp_enc.c:518:</p>
  
  <p>Why would this be, and further, what am I doing incorrectly?</p>
  
  <p>If anybody could help, I'd be very thankful.</p>