Android的AES加密/解密 [英] android AES encryption/ decryption

查看:219
本文介绍了Android的AES加密/解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法code中的功能进行文件加密/解密。 但其尤其作为文件大小的增加非常缓慢。对于如音频/介绍用户文件,其中有几MB长

I have managed to code the function for doing file encryption / decryption . But its very slow especially as the file size increase. for eg audio / vidoe file which are few MB long

我已经经历了几乎所有的岗位去改善它,并试图改变algorthms。 请帮我,如果有任何变动,可以帮助我提高性能。

I have gone through almost all post to improve it, and tried changing the algorthms. Please help me if there is any change that can help me improve performance.

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;
}

我一直在评论其他交易算法我曾经试过,但没有看到任何区别。 非常感谢您的帮助

i have kept in comments the other algos i had tried but didnt see any difference. Thanks a lot for the help

推荐答案

简介您code,但你可能做得太多IO。让你的缓存时,它不必是相同大小的密码块大小。至于实际的加密速度,这主要取决于CPU,所以你不能去改变。您可以尝试使用本地code(OpenSSL的,等等),看看是否有差别。

Profile your code, but you are probably doing too much IO. Make your buffer larger, it doesn't have to be the same size as the cipher block size. As for the actual encryption speed, it depends mostly on the CPU, so you can't really change that. You could try to use native code (OpenSSL, etc.) and see if that makes a difference.

这篇关于Android的AES加密/解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆