Using Android NDK for encryption of data passed from normal android app
Is it possible and worth trying to develop some server application using android NDK which will encrypt data (or just use some built in Linux encryption library) passed to it from normal Java based application?
I tried using Cip开发者_运维百科her library, but it took almost a minute to encrypt 2MB file with AES. And blowfish is not available in Cipher until Android 2.3(?). And I doubt it will be much faster.
I was using blowfish for encryption on Symbian and it was much faster (under 5-10 seconds) so I think in android it is slower because of using Java virtual machine and I'd like to try native app for it.
Have someone done it before?
EDIT: Encrypting in NDK is so much faster. Do it there. There is a similar question with the same answer for AES: AES decryption on Android too slow to be usable. Will NDK be faster? Other ideas?
BouncyCastle in Android 2.2 is horribly slow with AES/CBC/PKCS5 while using stream decryption. CPU is going to 100% and throughput is 5kb/sec.
Using Chilkat is magnitudes faster and keeps CPU usage low (even in the Emulator). But Chilkat is not offering an InputStream to handle stream decryption and is buffering all the encrypted bytes internally (until a heap space error occurs). So you must manage stream decryption yourself (e.g. by initializing chilkat for every block...)
What version of Android are you testing against? Bear in mind that starting with Froyo, there's a tracing JIT which should work quite well for the math-intensive loops in a crypto library.
For older versions, you'd probably want to do it with the NDK, yes. I don't know why you need a server, though - simply compile any good/fast crypto library and make a wrapper around it using the NDK. Then, you can simply use the wrapper from your java-based app.
To answer your question, yes you could probably write something that would run with the NDK, but I don't see why you would need to.
If you just want to encrypt data that's going into the sql storage you can check out SQLCipher ( https://guardianproject.info/code/sqlcipher/ )
You might also try using some bouncy castle libraries ( http://www.bouncycastle.org/java.html ). They may be faster than the built in android one or they may have a blowfish library which you can use.
This is not open source but performance point of view Chillkat the best I found.
Of course it is possible to use external library to do crypto. You can use openssl for example https://github.com/guardianproject/openssl-android. The only question is how large will be the impact of JNI layer that exist between Java and C code. If you need to pass lot of date to C and back then benefit from having native library could be negated by JNI layer. It would be much better to move more functionality from Java to C in a way that data that should be encrypted could be processed in C only. For example network stack and encryption could be in C while user interface in Java. This is just proposal you should know better if this is possible or not.
精彩评论