title: Failed to decode data using encoding 'utf-8' in dart
body: <p>I am decoding a video file using AES decryption(ECB mode). I have implemented the below code, but get an exception:</p>
  <pre><code>FileSystemException (FileSystemException: Failed to decode data using encoding 'utf-8', path = '/data/user/0/bhaveshparakh.acadflip/app_flutter/sample1.ehb')
  
  </code></pre>
  <p>This is the code i implemented:</p>
  <pre><code>var dir =
                                      await getApplicationDocumentsDirectory();
  
                                  Dio dio = Dio();
                                  dio.download(videourl, '${dir.path}/sample1.ehb',
                                      onReceiveProgress:
                                          (actualbytes, totalbytes) {
                                    var percentage =
                                        actualbytes / totalbytes * 100;
                                    setState(() {
                                      downloadMessage =
                                          'Downloading ${percentage.floor()} %';
                                    });
                                  });
  decryptFile('${dir.path}/sample1.ehb');
  
  Future&lt;String&gt; decryptFile(filePath) async{ 
       //'filePath' contains the encrypted video file.
      var encodedKey = info;
      var encryptedBase64EncodedString = await new File(filePath).readAsString(encoding:utf8);
      var decoded = base64.decode(encryptedBase64EncodedString);
      final key1 = enc.Key.fromBase64(encodedKey);
      final encrypter = enc.Encrypter(enc.AES(key1, mode: enc.AESMode.ecb));
      final decrypted = encrypter.decryptBytes(enc.Encrypted(decoded));
      final filename = '${p.basenameWithoutExtension(filePath)}.mp4';
      final directoryName=p.dirname(filePath);
      final newFilePath=p.join(directoryName, filename);
      var newFile = new File(newFilePath);
      await newFile.writeAsBytes(decrypted);
      print(&quot;Successfull&quot;);
      return newFilePath;
    }
  
  </code></pre>
  <p>Though its been encrypted the file using utf8 in JAVA, i am getting this error. Is there something i am doing wrong?</p>
  <p>This is how they have encrypted the file in JAVA</p>
  <pre><code>public static void listFilesForFolder(final File folder) {
          for (final File fileEntry : folder.listFiles()) {
              if (fileEntry.isDirectory()) {
                  listFilesForFolder(fileEntry);
              } else {
                  System.out.println(fileEntry.getName());
                  try {
                      Cipher desCipher;
  
                      // Create the cipher
                      desCipher = Cipher.getInstance(&quot;AES&quot;);
  
                      byte[] key = encodekey.getBytes(&quot;UTF-8&quot;);
                      MessageDigest sha = MessageDigest.getInstance(&quot;SHA-1&quot;);
                      key = sha.digest(key);
                      key = Arrays.copyOf(key, 16); // use only first 128 bit
  
                      SecretKeySpec secretKeySpec = new SecretKeySpec(key, &quot;AES&quot;);
                  
  
                      desCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
  
                      byte[] text = readVideo(fileEntry.getName());
              
                      byte[] textEncrypted = desCipher.doFinal(text);
                      writeByte(textEncrypted, fileEntry.getName());
                      
                  } catch (Exception e) {
                      
                      e.printStackTrace();
                  }
  
              }
          }
          System.out.println(&quot;Done!!!&quot;);
      }
  
      public static byte[] readVideo(String name) {
  
          byte[] bytes = null;
          try {
              FileInputStream fis = new FileInputStream(new File(&quot;/home/raghib/java/source/&quot; + name));
              ByteArrayOutputStream bos = new ByteArrayOutputStream();
              byte[] b = new byte[1024];
  
              for (int readNum; (readNum = fis.read(b)) != -1;) {
                  bos.write(b, 0, readNum);
              }
  
              bytes = bos.toByteArray();
              fis.close();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          return bytes;
      }
      public static void writeByte(byte[] bytearray, String filename) {
          try {
              String replacedStr1 = filename.replaceAll(&quot;.mp4&quot;, &quot;.ehb&quot;);
              String replacedStr = replacedStr1.replaceAll(&quot;.pdf&quot;, &quot;.ehbp&quot;);
              FileOutputStream fileoutputstream = new FileOutputStream(
                      new File(&quot;/home/raghib/java/destination/&quot; + replacedStr), true);
              fileoutputstream.write(bytearray);
              fileoutputstream.close();
          } catch (Exception ex) {
              ex.printStackTrace();
          }
  
      }
  
  </code></pre>