0

I have an encrypted video with the key and I want to decrypt that video file with Key but I am not able to decrypt the video file. I try 2 3 methods but Non of these works for me.

Crypto.Class

public class Crypto {

    static void fileProcessor(int cipherMode,String key,File inputFile,File outputFile){
        try {

            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = new SecureRandom();
            secureRandom.setSeed(key.getBytes("UTF-8"));
            keyGenerator.init(128, secureRandom);
            SecretKey secretKey = keyGenerator.generateKey();
//            Key secretKey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(cipherMode, secretKey);

            FileInputStream inputStream = null;
            byte[] inputBytes = new byte[0];
            try {
                inputStream = new FileInputStream(inputFile);
                inputBytes = new byte[(int) inputFile.length()];
                inputStream.read(inputBytes);
            } catch (IOException e) {
                e.printStackTrace();
            }

            byte[] outputBytes = cipher.doFinal(inputBytes);

            FileOutputStream outputStream = new FileOutputStream(outputFile);
            outputStream.write(outputBytes);

            inputStream.close();
            outputStream.close();

        } catch (NoSuchPaddingException | NoSuchAlgorithmException
                | InvalidKeyException | BadPaddingException
                | IllegalBlockSizeException | IOException e) {
            e.printStackTrace();
        }
    }

    public static String Decrypt(String filePath) {
        String key = "my_encrypted_key";
//        File inputFile = new File("text.txt");
        File encryptedFile = new File(filePath);
        String decryptedFilePath="";
        //check special guid folder if not exist create

        //get the selected video file path  with settings path  if exist decrypt if not exist show message
       File decryptedFile = new File(decryptedFilePath);

        try {
            //Crypto.fileProcessor(Cipher.ENCRYPT_MODE,key,inputFile,encryptedFile);
            Crypto.fileProcessor(Cipher.DECRYPT_MODE,key,encryptedFile,decryptedFile);
            Log.d("success", "Decrypt: success");
            return decryptedFile.getAbsolutePath();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        return "";
    }

}

But this does not work for me and I try another one.

public static void TEST2(String orgFile) {
        try {
            String password = "estudies741852963@123";
            File outFile_dec = new File(Environment.getExternalStorageDirectory()+"/testVideo/abc.mp4");
            if(!outFile_dec.isDirectory()) {
                outFile_dec.mkdir();
            }
            if(!outFile_dec.exists()){
                outFile_dec.createNewFile();
            }

            // reading the salt
            // user should have secure mechanism to transfer the
            // salt, iv and password to the recipient
            FileInputStream saltFis = new FileInputStream(orgFile);
            byte[] salt = new byte[8];
            saltFis.read(salt);
            saltFis.close();

            // reading the iv
            FileInputStream ivFis = new FileInputStream(orgFile);
            byte[] iv = new byte[16];
            ivFis.read(iv);
            ivFis.close();

            SecretKeyFactory factory = SecretKeyFactory
                    .getInstance("PBKDF2WithHmacSHA1");
            KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 1000,
                    256);
            SecretKey tmp = factory.generateSecret(keySpec);
            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

            // file decryption
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
            InputStream fis = new FileInputStream(orgFile);
            OutputStream fos = new FileOutputStream(outFile_dec);
//        byte[] in = new byte[1024];
//        int read;
//        while ((read = fis.read(in)) != -1) {
//            byte[] output = cipher.update(in, 0, read);
//            if (output != null)
//                fos.write(output);
//        }
//
//        byte[] output = cipher.doFinal();
//        if (output != null)
//            fos.write(output);
//        fis.close();
//        fos.flush();
//        fos.close();
//        System.out.println("File Decrypted.");
            fos = new CipherOutputStream(fos, cipher);
            int count = 0;
            byte[] buffer = new byte[1024];
            while ((count = fis.read(buffer)) >= 0) {
                fos.write(buffer, 0, count);
            }
            fis.close();
            fos.flush();
            fos.close();
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

In this method, its always shows on exception saying no such directory. And I try another method also But none of these works for me. Tried this solution to decrypt my video

Java 256-bit AES Password-Based Encryption

How do i decrypt a file in Android with AES?

  • what line in what class do you get the exception? do you get the same exception or another with the 2nd method. Do you know for sure how the video was encrypted. Have you tried to encrypt and decrypt any file yourself? – mavriksc Sep 11 '18 at 16:38
  • Different Method gets a different exception. Some of theme are Out Of HeapMemory, no such directory, and others. Yeah, I know how the video was encrypted and it was encrypted in. .net by my senior developer. I haven't try encrypting and decrypting my file.@mavriksc – sanjay maharjan Sep 11 '18 at 16:46
  • i said how not who. you need the precise alg used and key. if you are running out of memory you may have to decrypt in chunks. but again need the LINE where the exception is and the details. you have provided 0 useful info and so it's hard to help – mavriksc Sep 11 '18 at 16:51
  • In line inputStream.read(inputBytes); it show Out of Memory exception. and in line if(!outFile_dec.exists()){ outFile_dec.createNewFile(); } i have no such directory exception. @mavriksc – sanjay maharjan Sep 11 '18 at 16:53
  • Trying different methods is not the right way to solve your problem. You must find out how the data was encrypted. Precise details including algorithm, mode, padding, key size and key derivation method. – President James K. Polk Sep 11 '18 at 17:21
  • so for the file path issue `.isDir()` doesn't check if the directory exists on the file system it returns if the path used to make the file is a dir or a file. you probably need to create the directory and then save the file. you can try to save in current dir and see if there are any issues. for the OOM. how big is the video. when reading the bytes you have 2 copies of the file in memory – mavriksc Sep 11 '18 at 17:21

0 Answers0