如何以加密格式将大文件存储在本地android设备中

时间:2018-12-21 12:10:32

标签: java android encryption local-storage file-handling

我正在开发基于视频的android应用,该应用可以在离线模式下工作。为此,我们将视频存储在android设备中,   在设备中存储数据(视频文件)的最佳方法是什么,它将仅在我的App中打开并运行。

我试图加密视频文件并将其存储在设备中,但面临以下问题:-

视频大小超过300 Mb,并且加密和解密需要太多​​时间(大约超过1-2分钟)
    但我需要将此时间最小化到2-3秒。 最好的方法是什么

我使用以下两种加密和解密方式

1. whole file at one time  

 public static byte[] encode(SecretKey yourKey, byte[] fileData)
        throws Exception {
    byte[] data = yourKey.getEncoded();
    SecretKeySpec skeySpec = new SecretKeySpec(data, 0, data.length, EncoDecode.KEY_SPEC_ALGORITHM);
    Cipher cipher = Cipher.getInstance(EncoDecode.CIPHER_ALGORITHM);//, EncoDecode.PROVIDER
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
    return cipher.doFinal(fileData);


}

public static byte[] decode(SecretKey yourKey, byte[] fileData)
        throws Exception {
    byte[] decrypted;
    Cipher cipher = Cipher.getInstance(EncoDecode.CIPHER_ALGORITHM);//, EncoDecode.PROVIDER
    Log.d("", "decode() returned: " + cipher.getBlockSize());
    Utility.checkmemory(mContext);
    cipher.init(Cipher.DECRYPT_MODE, yourKey, new IvParameterSpec(new byte[cipher.getBlockSize()]));
    Utility.checkmemory(mContext);
    decrypted = cipher.doFinal(fileData);
    return decrypted;
}
 2. byte -to byto encrytion or decrytion
  /**
 * Encrypt and save to disk
 *
 * @return
 */
private boolean encrypt() {
    updateUI("Encrypting file...");
    pDialog.setMessage("Encrypt start..");
    pDialog.show();
    try {

        Log.e(TAG, "encrypt: starting.... " );
        Encryptor encryptor = new Encryptor(EncryptDecryptUtils.getInstance(this).getSecretKey(), CIPHER_ALGORITHM, 16);

        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(path);
            os = encryptor.wrapOutputStream(new FileOutputStream(pathEnc));
            byte[] buffer = new byte[8192];// change in low memory device 4096
            int nRead;
            while((nRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, nRead);
                Log.e(TAG, "encrypt in process.... " );
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pDialog.setMessage("Encrypt in process...");
                    }
                });
            }
            os.flush();
        } finally {
            pDialog.setMessage("Encrypt complete...");
            pDialog.dismiss();
            Log.e(TAG, "encrypt complete.... " );
            if(is != null) {
                is.close();
            }
            if(os != null) {
                os.close();
            }
        }

        return true;
    } catch (Exception e) {
        e.printStackTrace();

        updateUI("File Encryption failed.\nException: " + e.getMessage());
    }
    return false;
}

/**
 * Decrypt and return the decoded bytes
 *
 * @return
 */
private byte[] decrypt() {
    updateUI("Decrypting file...");
    pDialog.setMessage("Decrypt start..");
    pDialog.show();
    try {
  Log.e(TAG, "decrypt start !!! " );
        Encryptor encryptor = new Encryptor(EncryptDecryptUtils.getInstance(this).getSecretKey(), CIPHER_ALGORITHM, 16);
        InputStream is = null;
        OutputStream os = null;

        try {
            is = encryptor.wrapInputStream(new FileInputStream(pathEnc));
            os = new FileOutputStream(pathDec);
            byte[] buffer = new byte[8192];
            int nRead;
            while((nRead = is.read(buffer)) != -1) {
                Log.e(TAG, "decrypt in porocess.... " );
                os.write(buffer, 0, nRead);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pDialog.setMessage("Decrypt in process...");
                    }
                });
            }
            os.flush();
        } finally {
            pDialog.setMessage("Decrypt complete...");
            pDialog.dismiss();
            Log.e(TAG, "decrypt complete !!! " );
            if(is != null) {
                is.close();
            }
            if(os != null) {
                os.close();
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        updateUI("File Decryption failed.\nException: " + e.getMessage());
    }
    return null;
}

0 个答案:

没有答案