-2

I am using "AES" algorithm for Decryption of video, Which is stored on External Sd card in Encrypted format.While decrypting video its decrypt only 47% and get stop. please give solution

public void createDecryptedFile(File decryptedFileDir, File decryptedFile,
                                File encryptedFile) {
    try {
        if (!decryptedFileDir.exists()) {
            decryptedFileDir.mkdirs();
        }
        Cipher decipher;
        decryptedFile.createNewFile();
        deleteFile = decryptedFile;
                        FileInputStream encryptedFileInputstream = new FileInputStream(
                encryptedFile);
        FileOutputStream decryptedFileOutputstream = new FileOutputStream(
                decryptedFile);

        decipher = Cipher.getInstance("AES");
        Key key = generateKey();
        decipher.init(Cipher.DECRYPT_MODE, key);

        CipherOutputStream cos = new CipherOutputStream(
                decryptedFileOutputstream, decipher);

        byte data[] = new byte[10000 * 1024];

        int count;
        try {

            while ((count = encryptedFileInputstream.read(data)) != -1  && !stopConversion) {
                Log.d("#########", "##########");

                total += count;
                Log.e("convert count", total + "");

                cos.write(data, 0, count);

                final long l = encryptedFile.length();

                runOnUiThread(new Runnable() {
                    public void run() {

                        // Show percentage 
                        loadingpercent.setText("" + (int) (total * 100 / l) + "%");
                    }
                });

                Log.d("$$$$$$$$",""+encryptedFileInputstream.read(data));

            }
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Please edit your question to include a [mcve] of the issue. Also be aware that your use of AES in ECB mode makes the above encryption code completely insecure. – Luke Joshua Park Jul 20 '17 at 08:49
  • General advice: **Always use a fully qualified Cipher string.** `Cipher.getInstance("AES");` may result in different ciphers depending on the default security provider. It most likely results in `"AES/ECB/PKCS5Padding"`, but it doesn't have to be. If it changes, you'll lose compatibility between different JVMs. For reference: [Java default Crypto/AES behavior](https://stackoverflow.com/q/6258047/1816580) – Artjom B. Jul 20 '17 at 16:41
  • **Never use [ECB mode](https://crypto.stackexchange.com/q/14487/13022)**. It's deterministic and therefore not semantically secure. You should at the very least use a randomized mode like [CBC](https://crypto.stackexchange.com/q/22260/13022) or [CTR](https://crypto.stackexchange.com/a/2378/13022). It is better to authenticate your ciphertexts so that attacks like a [padding oracle attack](https://crypto.stackexchange.com/q/18185/13022) are not possible. This can be done with authenticated modes like GCM or EAX, or with an [encrypt-then-MAC](https://crypto.stackexchange.com/q/202/13022) scheme. – Artjom B. Jul 20 '17 at 16:41

1 Answers1

0
Replace  :-


CipherInputStream cis = new CipherInputStream(encryptedFileInputstream, cipher);


            int count;
            try {


                int b;
                byte[] d = new byte[10000 * 2048];
                while ((b = cis.read(d)) != -1 && !stopConversion) {


                    total += b;

                    final long l = encryptedFile.length();


                    decryptedFileOutputstream.write(d, 0, b);


                    runOnUiThread(new Runnable() {
                        public void run() {
                            loadingpercent.setText("" + (int) (total * 100 / l) + "%");
                        }
                    });


                }