title: Java - Input byte has incorrect ending byte at 300
body: <p>I have written a JAVA based utility to store my data in excel encrypted (AES) and upload it to FTP Server and reverse mechanism for the same to download it and decrypt back to plain text.</p>
  
  <p>Now, the encryption side is working perfectly, but when I decrypt the same. It throws the below exception:</p>
  
  <pre><code>Error while decrypting: java.lang.IllegalArgumentException: Input byte array has incorrect ending byte at 300
  </code></pre>
  
  <p>I have tried to change the encoding scheme but nothing worked.</p>
  
  <p><strong>My Code</strong></p>
  
  <pre><code>
  public void downloadFileFromFTP () {
  
  
          int SFTPPORT = 22;
          String SFTPHOST = "";
          String SFTPUSER = "";
          String SFTPPASS = "";
          String SFTPWORKINGDIR = "";
  
          Session session = null;
          Channel channel = null;
          ChannelSftp channelSftp = null;
  
          try {
              JSch jsch = new JSch();
  
              session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
  
              session.setPassword(SFTPPASS);
  
              java.util.Properties config = new java.util.Properties();
  
              config.put("StrictHostKeyChecking", "no");
  
              session.setConfig(config);
  
              session.connect();
  
              channel = session.openChannel("sftp");
  
              channel.connect();
  
              channelSftp = (ChannelSftp) channel;
  
              channelSftp.cd(SFTPWORKINGDIR);
  
              byte[] buffer = new byte[1024];
  
              BufferedInputStream bis = new BufferedInputStream(channelSftp.get("test.csv"));
  
              File newFile = new File("D:\\FMS_Encryption\\dec2.csv");
  
              OutputStream os = new FileOutputStream(newFile);
  
              BufferedOutputStream bos = new BufferedOutputStream(os);
  
              int readCount;
  
              while ((readCount = bis.read(buffer)) &gt; 0) {
  
                  String encryptedRow = new String(buffer, StandardCharsets.UTF_8);
  
                  String decryptedRow = decrypt( encryptedRow.trim(), "FMS" );
  
                  bos.write(decryptedRow.getBytes(), 0, readCount);
              }
  
              bis.close();
  
              bos.close();
          } 
          catch (Exception ex) {
              ex.printStackTrace();
          }
  
      }
  
  </code></pre>
  
  <p><strong>ENCRYPTION/DECRYPTION CODE</strong></p>
  
  <pre><code>
  public static String encrypt(String strToEncrypt, String secret) 
      {
          try
          {
              setKey(secret);
              Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
              cipher.init(Cipher.ENCRYPT_MODE, secretKey);
              return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
          } 
          catch (Exception e) 
          {
              System.out.println("Error while encrypting: " + e.toString());
          }
          return null;
      }
      public static String decrypt(String strToDecrypt, String secret) 
      {
          try
          {
              setKey(secret);
              Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
              cipher.init(Cipher.DECRYPT_MODE, secretKey);
              return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
          } 
          catch (Exception e) 
          {
              System.out.println("Error while decrypting: " + e.toString());
          }
          return null;
      }
  
  </code></pre>
  
  <p>Now in download function, when I change the line <code>String decryptedRow = decrypt( encryptedRow, "FMS" );</code> to <code>String decryptedRow = decrypt( encryptedRow.trim(), "FMS" );</code> it throws:</p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException
      at java.lang.System.arraycopy(Native Method)
      at java.io.BufferedOutputStream.write(Unknown Source)
      at IbftFileEncryption.downloadFileFromFTP(IbftFileEncryption.java:426)
      at IbftFileEncryption.main(IbftFileEncryption.java:521)
  </code></pre>