簡體   English   中英

Android AES Decrpyt字符串花費的時間太長

[英]Android AES Decrpyt string takes too long

我在Android項目上使用AES解密來解密大型字符串對象(大於1 MB)。

我正在使用這種方法:

public static String decryptAES(String cryptedString, byte[] byteArrayAESKey) {

    try {
        IvParameterSpec ips = new IvParameterSpec(General.InitVector.getBytes("UTF-8"));
        SecretKey aesKey = new SecretKeySpec(byteArrayAESKey, "AES");
        byte[] TBCrypt = Base64.decode(cryptedString, Base64.DEFAULT);
        // Decryption cipher
        Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        // Initialize PBE Cipher with key and parameters
        decryptCipher.init(Cipher.DECRYPT_MODE, aesKey, ips);
        // Decrypt the cleartext
        byte[] deciphertext = decryptCipher.doFinal(TBCrypt); // this may take a long time depending on string input length
        return new String(deciphertext, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("AES", "Decrypt failed : " + e.getMessage());
        return "";
    }
    }

它可以很好地工作,但是在大的加密字符串上,在許多設備上花費很長時間。

有沒有辦法在Android設備上改進此方法? 我應該剪切加密的字符串以加快此過程嗎? 我應該使用SpongyCastle嗎?

byte[] deciphertext = decryptCipher.doFinal(TBCrypt); 不要那樣做! 而是考慮使用流,也許直接輸出文件流(如果需要)?

有沒有辦法在Android設備上改進此方法?

也許,您可以在這里看一看,但據說AES相當快。

我應該剪切加密的字符串以加快此過程嗎?

是的,這應該是問題所在。 通常,您只需要加密數據的關鍵部分。 重構可以解決問題。

我應該使用SpongyCastle嗎?

不知道,但是如果我在您的位置,我將首先查看加密的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM