如何在android中使用AES减少解密时间

时间:2012-11-10 11:02:00

标签: java performance encryption

我现在正致力于音频文件加密和解密。它对我来说很好,但解密速度太低了。如何减少解密时间?它应该花费<6或7秒。我怎样才能做到这一点?请帮我一个简单的例子。我正在使用AES。

2 个答案:

答案 0 :(得分:0)

我猜这是一个javax.crypto.Cipher实例。它可以处理任意长度数组的update()调用,如果使用更长的数组,则需要减少开销。

尝试应该按照8192字节的块来处理数据(这是缓冲区的传统长度,它与CPU内部缓存相互作用相当好。)

CipherInputStream cis = new CipherInputStream(fis, encipher);
byte[] buffer = new byte[8192]; // Or larger (but use powers of 2)
int bytesRead;
while ((bytesRead = cis.read(buffer)) != -1)
{
    fos.write(buffer, 0, bytesRead);
}
fos.flush(); 

.... 不要忘记关闭finally块中的所有流。

答案 1 :(得分:0)

public class EncryptDecrypt {
    public Cipher encryptcipher, decryptCipher;
    String TAG = "EncryptDecrypt";
    // The default block size
    public static int blockSize = 16;

    // Buffer used to transport the bytes from one stream to another
    byte[] buf = new byte[blockSize]; // input buffer
    byte[] obuf = new byte[8192]; // output buffer

    // The key
    byte[] key = null;  

     byte[] IV = null;

    public EncryptDecrypt(String passwd) {

        key = passwd.getBytes();
        key = "SECRETSECRET_1SE".getBytes();

        // default IV value initialized with 0
        IV = new byte[blockSize];
        initCiphers();

    }

    public void initCiphers() {
        try {
            // 1. create the cipher using Bouncy Castle Provider
            encryptcipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
            // 2. create the key
            SecretKey keyValue = new SecretKeySpec(key, "AES");
            // 3. create the IV
            AlgorithmParameterSpec IVspec = new IvParameterSpec(IV);
            // 4. init the cipher
            encryptcipher.init(Cipher.ENCRYPT_MODE, keyValue, IVspec);
            encryptcipher.getOutputSize(8192);

            // 1 create the cipher
            decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
            // 2. the key is already created
            // 3. the IV is already created
            // 4. init the cipher
            decryptCipher.init(Cipher.DECRYPT_MODE, keyValue, IVspec);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String encryptData(URL inputFileName, String outputFileName) {
        File outFilename = new File(outputFileName);
        try {
            InputStream fis;
            OutputStream fos;
            fis = new BufferedInputStream(inputFileName.openStream(), 1000);
            fos = new BufferedOutputStream(new FileOutputStream(outFilename));
            Log.i(TAG, "Output path:" + outFilename);
             int bufferLength = (inputFileName.toString().length() > 10000000
             ? 10000000
             : 1000);
            byte[] buffer = new byte[8192];
            int noBytes = 0;
            byte[] cipherBlock = new byte[encryptcipher
                    .getOutputSize(buffer.length)];

            int cipherBytes;
            long startTime = System.currentTimeMillis();
            while ((noBytes = fis.read(buffer)) != -1) {
                cipherBytes = encryptcipher.update(buffer, 0, noBytes,
                        cipherBlock);
                fos.write(cipherBlock, 0, cipherBytes);
            }
            // always call doFinal
            cipherBytes = encryptcipher.doFinal(cipherBlock, 0);
            fos.write(cipherBlock, 0, cipherBytes);
            System.out
                    .println("encryption "
                            + " took "
                            + ((System.currentTimeMillis() - startTime) / 1000.0)
                            + "s");
            // close the files
            fos.close();
            fis.close();
            Log.i("encrypt", "done");
        }

        catch (Exception ex) {
            ex.printStackTrace();
        }
        return outFilename.toString().trim();
    }

    public String decryptData(String inputFileName, String outputFileName) {

        InputStream fis;
        OutputStream fos;
        try {
            fis = new BufferedInputStream(new FileInputStream(inputFileName));
            fos = new BufferedOutputStream(new FileOutputStream(outputFileName));
            byte[] buffer = new byte[8192];
            int noBytes = 0;
            byte[] cipherBlock = new byte[decryptCipher
                    .getOutputSize(buffer.length)];
            int cipherBytes;
            long startTime = System.currentTimeMillis();
            while ((noBytes = fis.read(buffer)) != -1) {
                cipherBytes = decryptCipher.update(buffer, 0, noBytes,
                        cipherBlock);
                Log.d("BufferRead", "" + buffer);
                fos.write(cipherBlock, 0, cipherBytes);
            }
            // allways call doFinal
            cipherBytes = decryptCipher.doFinal(cipherBlock, 0);
            fos.write(cipherBlock, 0, cipherBytes);
            System.out
                    .println("decryption "
                            + " took "
                            + ((System.currentTimeMillis() - startTime) / 1000.0)
                            + "s");
            // close the files
            fos.close();
            fis.close();
            Log.i("decrypt", "done");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return outputFileName.toString().trim();
    }
}

This is my code