增加AES加密的Android速度 [英] Increase speed of encryption AES android
问题描述
我开发手机上的文件加密Android应用程序。通过搜索,我发现这个问题:<一href=\"http://stackoverflow.com/questions/10782187/how-to-encrypt-file-from-sd-card-using-aes-in-android\">How以文件从Android的使用AES SD卡加密?
该方法工作正常,但它是对文件进行加密很慢...
在这一行:字节[] D =新的字节[8];为什么只有8个字节?我们不能设定一个更高的价值?
另外,你知道的方式,以速度快加密文件?我听说加密++的本机code实现的,但我怎么能在我的应用程序中实现的JNI?
感谢您,
编辑:加密功能
公共无效加密(字符串RSAPrivateKey,串键,字节[]四,字符串zipname,ZipEncryptAsyncTask任务)抛出IOException异常,抛出:NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException将,抛出:InvalidAlgorithmParameterException
{
FIS的FileInputStream =新的FileInputStream(zipname +.temp);
FOS的FileOutputStream =新的FileOutputStream(zipname);
SecretKeySpec SKS =新SecretKeySpec(Base64.de code(密钥,Base64.DEFAULT),AES);
IvParameterSpec ivspec =新IvParameterSpec(IV);
密密码= Cipher.getInstance(AES / CBC / PKCS5Padding); fos.write(将String.valueOf(RSAPrivateKey.getBytes()长).getBytes());
fos.write(RSAPrivateKey.getBytes()); cipher.init(Cipher.ENCRYPT_MODE,SKS,ivspec);
CipherOutputStream COS =新CipherOutputStream(FOS,密码); 长大小= 0;
字节[] D =新的字节[8];
为(中间体B:(B = fis.read(D))= -1;!)
{
cos.write(D,0,B);
task.doProgress((大小+ = 8));
} cos.flush();
cos.close();
fis.close(); 新的文件(zipname +.temp),删除();
}
作为@ codesInChaos说,一个办法做到这一点,是增加缓冲区的大小。
现在我做一个基准,这决定了最佳尺寸为缓冲区和我用的最佳值。
I develop an android application for encrypt files on the phone. By searching, i found this topic : How to encrypt file from sd card using AES in Android?
The method works fine but it is very slow to encrypt files... At this line : byte[] d = new byte[8]; why only 8 bytes ? can't we set an higher value ?
Also, do you know a way to encrypt files fastly ? I heard of crypto++ for native code implementation but how can I implement JNI on my application ?
Thank you,
EDIT : Encryption function
public void encrypt (String RSAPrivateKey, String Key, byte[] iv, String zipname, ZipEncryptAsyncTask task) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException
{
FileInputStream fis = new FileInputStream (zipname + ".temp");
FileOutputStream fos = new FileOutputStream (zipname);
SecretKeySpec sks = new SecretKeySpec (Base64.decode(Key, Base64.DEFAULT), "AES");
IvParameterSpec ivspec = new IvParameterSpec (iv);
Cipher cipher = Cipher.getInstance ("AES/CBC/PKCS5Padding");
fos.write(String.valueOf(RSAPrivateKey.getBytes().length).getBytes());
fos.write(RSAPrivateKey.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, sks, ivspec);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
long size = 0;
byte[] d = new byte[8];
for(int b; (b = fis.read(d)) != -1; )
{
cos.write(d, 0, b);
task.doProgress((size += 8));
}
cos.flush();
cos.close();
fis.close();
new File(zipname + ".temp").delete();
}
as @CodesInChaos said, a way to do this, is to increase the size of the buffer. Now I do a benchmark which determines the best size for the buffer and I use the optimal value.
这篇关于增加AES加密的Android速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!