android AES加密解密

android AES加密解密,第1张

概述我已设法编写用于执行文件加密/解密的功能.但它的速度非常慢,特别是随着文件大小的增加.例如,几MB长的音频/视频文件我已经完成了几乎所有的帖子来改进它,并尝试改变algorthms.如果有任何改变可以帮助我提高性能,请帮助我.publicclassDataEncryptDecrypt{publicCipherenc

我已设法编写用于执行文件加密/解密的功能.
但它的速度非常慢,特别是随着文件大小的增加.例如,几MB长的音频/视频文件

我已经完成了几乎所有的帖子来改进它,并尝试改变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;}

我一直在评论我试过的其他算法,但没有看到任何差异.
非常感谢您的帮助

解决方法:

描述你的代码,但你可能做了太多的IO.使您的缓冲区更大,它不必与密码块大小相同.至于实际的加密速度,它主要取决于cpu,所以你不能真正改变它.您可以尝试使用本机代码(OpenSSL等),看看是否有所作为.

总结

以上是内存溢出为你收集整理的android AES加密/解密全部内容,希望文章能够帮你解决android AES加密/解密所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1097724.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存