Android文件Decrytion和Encrytion服用时间文件、时间、Android、Encrytion

2023-09-03 22:46:10 作者:抵不住思念苦

我在做Android的加密和解密时,文件下载 但我想,以提高时间性能,当文件解密。 我的问题是,当我下载任何文件,所以我必须添加加密过那里,但在这个阶段,我显示进度加载器,所以它看起来不错,但但是当文件完全下载并尝试打开该文件,那么它被解密的文件,这个时候它的服用太多的时间。 这看起来很糟糕,我如何减少解密时间请帮助我,如果有一个人有类似的经历,请与我分享这是我的code

加密code

 字节的数据[] =新的字节[1024];

            字符串种子=密码;

            byte []的rawKey = getRawKey(seed.getBytes());
            SecretKeySpec skeySpec =新SecretKeySpec(rawKey,AES);
            密密码= Cipher.getInstance(AES);
            cipher.init(Cipher.ENCRYPT_MODE,skeySpec);

            输出=新CipherOutputStream(输出,密码);


            总长= 0;

            而((计数= input.read(数据))!=  -  1){
                共有+ =计数;
                publishProgress(+(int)的((总* 100)/ lenghtOfFile));


                output.write(数据,0,计数);

            }
 

解密code以下: -

 字符串的新路径= sdCardPath +/+ dPdfName;
                文件F1 =新的文件(的新路径);
                如果(!f1.exists())
                    尝试 {
                        f1.createNewFile();
                    }赶上(IOException异常E1){
                        // TODO自动生成的catch块
                        e1.printStackTrace();
                    }

                尝试 {
                    InputStream的FIS =新的FileInputStream(F);
                    OutputStream的FOS =新的FileOutputStream(F1);
                    字符串种子=密码;
                    byte []的rawKey = getRawKey(seed.getBytes());
                    SecretKeySpec skeySpec =新SecretKeySpec(rawKey,
                            AES);
                    密密码= Cipher.getInstance(AES);
                    cipher.init(Cipher.DECRYPT_MODE,skeySpec);

                    FIS =新CipherInputStream(FIS,密码);
                    INT B:
                    byte []的数据=新的字节[4096];
                    而((二= fis.read(数据))!=  -  1){
                        // fos.write(cipher.doFinal(数据),0,B);
                        fos.write(数据,0,B);
                    }
                    fos.flush();
                    fos.close();
                    fis.close();

                }赶上(例外五){
                    // TODO:处理exceptionpri
                    e.printStackTrace();
                }
 

获取行主要方法: -

 私有静态的byte [] getRawKey(byte []的种子)抛出异常{
    的KeyGenerator kgen = KeyGenerator.getInstance(AES);
    SecureRandom的SR = SecureRandom.getInstance(SHA1PRNG);
    sr.setSeed(种子);
    kgen.init(128,SR);
    SecretKey的SKEY = kgen.generateKey();

    byte []的原料= skey.getEn codeD();
    回归原始;
}
 
virtualbox 不能把android x86 4.4 RC1这个系统安装到硬盘里, 按步骤第四项 instullation安装完了,但是一重启就又变成了这个画面,高手教教我该怎么办

解决方案

建议:

1)将解密到其他的AsyncTask并添加其他进度指示器。

2)准备金最后说,进度指示器,以解密的10%。这是我实际上做了一次,但我做的完整性检查(对一个MD5哈希IIRC),不解密。

3)将解密下载AsyncTask的,每个数据接收部分立即进行解密,因此躲在下载时间解密时间。

4)不知道,这将是任何更快,但你可能有两个服务线程:一个下载文件,而另一个解密。最好是没有用的AsyncTask在这里,因为他们的行为可能不同不同版本的Andr​​oid(包括对单个线程顺序执行,请参阅Is AsyncTask的真正概念上的缺陷还是我只是失去了一些东西?的讨论情况和我的笔记 http://stackoverflow.com/ A /七十五万五千八百零四分之一千四百六十○万二千四百八十六)

还要注意的是负责下载和解密线程所属的型号的(在 MVC 的意义上),并且不能被一个活动所拥有,这是一个的控制器的不能活得比屏幕依次为: http://stackoverflow.com/a/14603375/755804

如果您的下载需要很长的时间,你可能会感兴趣的恢复中断的下载,这是更好地去想它从一开始。它总是容易修改简单的解决方案,以及多线程解决方案是相当复杂的。如果传输的文件,可能发生其中一人被转移时破了,你可能要一个选项,仅重新传送文件(县),被打破。您可能还需要进行完整性检查。

I have done Encryption and Decryption in android when file downloading but i want to improve time performance when file decrypted. My problem is when i am downloading any file so i have add encryption over there but at this stage i am showing Progress loader so it looks good but but when file completely download and try to open that file then it is decrypted that file this time its taking to much time . which is look very bad i how i can reduce decryption Time please help me if some one has similar experience please share with me here is my code

Encryption Code

byte data[] = new byte[1024];

            String seed = "password";

            byte[] rawKey = getRawKey(seed.getBytes());
            SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

            output = new CipherOutputStream(output, cipher);


            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress("" + (int) ((total * 100) / lenghtOfFile));


                output.write(data, 0, count);

            }

Decryption Code Here :-

String newPath = sdCardPath + "/" + dPdfName;
                File f1 = new File(newPath);
                if (!f1.exists())
                    try {
                        f1.createNewFile();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                try {
                    InputStream fis = new FileInputStream(f);
                    OutputStream fos = new FileOutputStream(f1);
                    String seed = "password";
                    byte[] rawKey = getRawKey(seed.getBytes());
                    SecretKeySpec skeySpec = new SecretKeySpec(rawKey,
                            "AES");
                    Cipher cipher = Cipher.getInstance("AES");
                    cipher.init(Cipher.DECRYPT_MODE, skeySpec);

                    fis = new CipherInputStream(fis, cipher);
                    int b;
                    byte[] data = new byte[4096];
                    while ((b = fis.read(data)) != -1) {
                        // fos.write(cipher.doFinal(data), 0, b);
                        fos.write(data, 0, b);
                    }
                    fos.flush();
                    fos.close();
                    fis.close();

                } catch (Exception e) {
                    // TODO: handle exceptionpri
                    e.printStackTrace();
                }

Get Row Key Method:-

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(seed); 
    kgen.init(128, sr); 
    SecretKey skey = kgen.generateKey(); 

    byte[] raw = skey.getEncoded();
    return raw;
}

解决方案

Suggestions:

1) move the decryption to another asynctask and add another progress indicator.

2) reserve last, say, 10% of the progress indicator to decryption. This is what I actually did once, but I was doing an integrity check (against an MD5 hash IIRC), not decryption.

3) move the decryption to the downloading asynctask, decrypt each received portion of data immediately and so hide the decryption time behind the download time.

4) not sure this will be any faster, but you may have two service threads: one downloading file and another decrypting it. It's better no to use AsyncTask here, because they may behave differently on different versions of Android (including sequential execution on a single thread, see Is AsyncTask really conceptually flawed or am I just missing something? for the discussion, and my note http://stackoverflow.com/a/14602486/755804 )

Note also that the thread responsible for downloading and decryption belongs to Model (in MVC sense) and must not be owned by an Activity which is a Controller that cannot outlive a screen turn: http://stackoverflow.com/a/14603375/755804

If your download takes a long time, you may be interested in resuming interrupted downloads, and it's better to think about it from the very beginning. It's always easier to modify simple solutions, and multi-threaded solutions are rather complex. If you transfer a number of files, it may happen that one of them gets broken during transfer, and you may want an option to retransfer only the file(s) that are broken. You may also want an integrity check.