title: AES Encryption in JAVA and InputStream
body: <p>Sorry, JAVA beginner here. I was trying out some encryption decryption examples. My methods were supposed to be returning an InputStream and were also supposed to take in an Inputstream as a parameter.
  The signature of the method looked like this,
  <code>public static InputStream encriptFile(InputStream inputFile)</code>.
  I researched a bit, and wrote some code confidently, but i don't think the code is properly encrypting a sample file because when I decrypt it and convert into string, it still shows me gibberish. I really don't know what's going wrong with encrypting and decrypting the InputStreams. The Java class looks like this,</p>
  <pre><code>    
      private static final String key = &quot;aesEncryptionKey&quot;;
      private static final String initVector = &quot;encryptionIntVec&quot;;
      
      /*
       * Getting a 128 bit key and iv for encryption
       */
      
      public static InputStream encriptFile(InputStream inputFile) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
          
          byte[] nonEncryptedByteArray = IOUtils.toByteArray(inputFile);
          
          IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(&quot;UTF-8&quot;));
          SecretKeySpec secretkey = new SecretKeySpec(key.getBytes(&quot;UTF-8&quot;), &quot;AES&quot;);
          
          Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5PADDING&quot;); //Cipher instance using AES encryption algorithm
          cipher.init(Cipher.ENCRYPT_MODE, secretkey, iv);
          byte[] encryptedByteArray = cipher.doFinal(nonEncryptedByteArray);
          
          /*
           * Used the cipher library to encrypt the stream to a byte array
           */
          InputStream encryptedInputStream = new ByteArrayInputStream(encryptedByteArray);
          
          /*
           * Back to streams, but this time encrypted
           */
          
          return encryptedInputStream;
      }
      
      public static InputStream decriptFile(InputStream inputFile) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
          
          byte[] encrytToDecryptByteArray = IOUtils.toByteArray(inputFile);
          
          IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(&quot;UTF-8&quot;));
          SecretKeySpec secretkey = new SecretKeySpec(key.getBytes(&quot;UTF-8&quot;), &quot;AES&quot;);
          
          Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5PADDING&quot;);
          cipher.init(Cipher.DECRYPT_MODE, secretkey, iv);
          byte[] decryptedByteArray = cipher.doFinal(encrytToDecryptByteArray);
          
          /*
           * dencrypted the encrypted data
           */
          
          InputStream decryptedInputStream = new ByteArrayInputStream(decryptedByteArray);
          
          return decryptedInputStream;
      }
  </code></pre>
  <p>The main method looks like this,</p>
  <pre><code>        
          File file = new File(&quot;test.txt&quot;);
          InputStream is = new FileInputStream(file);
          
          InputStream eis = encriptFile(is);
          
          StringWriter writer = new StringWriter();
          IOUtils.copy(eis, writer, &quot;UTF-8&quot;);
          String theString = writer.toString();
          
          System.out.print(theString);
  </code></pre>
  <p>The contents of the text file are &quot;Hello, file to be encrypted. Let's see if this works.&quot;.</p>
  <p>The output which should have printed out an encrypted output looks like this.
  <code>��T��� ���N�?]�7!2</code>. When I go ahead and decrypt it, it still shows me gibberish. Sorry for the really long question, any help is appreciated.</p>