2012-05-16 156 views
1

我已經設法編寫用於文件加密/解密的功能。 但特別慢,尤其是隨着文件大小的增加。例如幾MB長的音頻/視頻文件android AES加密/解密

我已經通過幾乎所有的帖子來改善它,並嘗試改變algorthms。 如果有任何可以幫助我提高性能的變化,請幫助我。

public class DataEncryptDecrypt { 
public Cipher encryptcipher, decryptCipher; 
int blockSize = 16; 
String TAG = "DataEncryptDecrypt"; 
private static final String RANDOM_ALGORITHM = "SHA1PRNG"; 

public DataEncryptDecrypt(String passwd) { 
    final String CIPHERMODEPADDING = "AES/CBC/PKCS5Padding"; 
    //AES/CBC/PKCS7Padding 
    char[] humanPassphrase = passwd.toCharArray(); 
    byte[] salt = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 
      0xF }; // must save this! 
    //salt =generateSalt(); 
    final int HASH_ITERATIONS = 100; 
    final int KEY_LENGTH = 128; //256 
    PBEKeySpec mykeyspec = new PBEKeySpec(humanPassphrase, salt, 
      HASH_ITERATIONS, KEY_LENGTH); 
// final String KEY_GENERATION_ALG = "PBEWITHSHAANDTWOFISH-CBC"; 
    final String KEY_GENERATION_ALG="PBEWithMD5And128BitAES-CBC-OpenSSL"; 
    SecretKey sk; 
    try { 
     encryptcipher = Cipher.getInstance(CIPHERMODEPADDING); 
     SecretKeyFactory keyfactory = SecretKeyFactory 
       .getInstance(KEY_GENERATION_ALG); 
     sk = keyfactory.generateSecret(mykeyspec); 

     // step 1 - get an instance of the cipher, specifying the mode and 
     // padding 

     byte[] iv = { 0xA, 1, 0xB, 5, 4, 0xF, 7, 9, 0x17, 3, 1, 6, 8, 0xC, 
       0xD, 91 }; // must save this 
     //iv= generateIv(); 
     IvParameterSpec IV = new IvParameterSpec(iv); 
     encryptcipher = Cipher.getInstance(CIPHERMODEPADDING); 
     decryptCipher = Cipher.getInstance(CIPHERMODEPADDING); 

     // step 2 - initialize the cipher 
     encryptcipher.init(Cipher.ENCRYPT_MODE, sk, IV); 
     decryptCipher.init(Cipher.DECRYPT_MODE, sk, IV); 

    } catch (NoSuchAlgorithmException nsae) { 
     Log.e("AESdemo", 
       "no key factory support for PBEWITHSHAANDTWOFISH-CBC"); 
    } catch (InvalidKeySpecException ikse) { 
     Log.e("AESdemo", "invalid key spec for PBKDF2"); 
    } catch (Exception ex) { 

    } 
} 

public String encryptData(String inputFileName) { 
    String outFilename = null; 
    File inputFile = new File(inputFileName); 
    try { 

     // step 3 - not needed, as we have all the blocks on hand 

     // step 4 - call doFinal() 

     outFilename = ".".concat(CommonUtils.getHash(inputFile.getName())); 
     InputStream fis; 
     OutputStream fos; 
     fis = new BufferedInputStream(new FileInputStream(inputFileName)); 

     fos = new BufferedOutputStream(new FileOutputStream(
       inputFile.getParent() + "/" + outFilename)); 
     Log.i(TAG, "Output path:" + inputFile.getParent() + "/" + outFilename); 
     byte[] buffer = new byte[blockSize]; 
     int noBytes = 0; 
     byte[] cipherBlock = new byte[encryptcipher 
       .getOutputSize(buffer.length)]; 
     int cipherBytes; 
     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); 

     // close the files 
     fos.close(); 
     fis.close(); 
     Log.i("encrpty", "done"); 
     inputFile.delete(); 
    } 

    catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    return inputFile.getParent() + "/" + outFilename; 
} 

我一直在評論我曾嘗試的其他算法,但沒有看到任何區別。 非常感謝您的幫助

回答

2

剖析您的代碼,但您可能做了太多的IO。使您的buffer變大,它不必與密碼塊大小相同。至於實際的加密速度,它主要取決於CPU,所以你不能真的改變它。你可以嘗試使用本地代碼(OpenSSL等),看看是否有所作爲。

+1

謝謝。我試圖增加緩衝區大小,它沒有太大的區別。你能提出任何其他可以幫助的變化嗎?我嘗試使用其他應用程序對文件進行加密,並且速度非常快。我不知道他們在做什麼魔術:)。所以我不能責怪處理器 – png

+0

一個很好的例子可以在這裏找到:http://stackoverflow.com/a/33171612/475496 – StaticBR