1

我现在正在研究音频文件的加密和解密。它对我来说工作正常,但解密速度太低。如何减少解密时间?它应该需要 <6 或 7 秒。我怎样才能做到这一点?请帮我举一个简单的例子。我正在使用 AES。

4

2 回答 2

0

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

Try 应该按 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 块中关闭所有流..

于 2012-11-10T11:16:45.053 回答
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
于 2012-11-12T12:28:50.673 回答